#include <windows.h>
#include <stdio.h>
#pragma comment(lib,"Msimg32.lib")
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst;
HBITMAP hBkBmp;
HBRUSH hBkBrush;
#ifdef _DEBUG
int main()
#else
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
#endif
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
#ifdef _DEBUG
hInst = GetModuleHandleA(NULL);
#else
hInst = hInstance;
#endif
hBkBmp = (HBITMAP)LoadImageA(NULL,"background.bmp",IMAGE_BITMAP,0,0, LR_LOADFROMFILE);
hBkBrush = CreatePatternBrush(hBkBmp);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInst;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = hBkBrush;// (HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("This program requires Windows NT!"),
szAppName, MB_ICONERROR);
return 0;
}
wndclass.hbrBackground = NULL;// (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszClassName = "ChildWnd";
wndclass.lpfnWndProc = ChildWndProc;
RegisterClass(&wndclass);
hwnd = CreateWindow(szAppName, // window class name
TEXT("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInst, // program instance handle
NULL); // creation parameters
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
//RECT rc;
switch (message)
{
case WM_CREATE:
CreateWindow("ChildWnd", // window class name
NULL, // window caption
WS_CHILD|WS_VISIBLE, // window style
10, // initial x position
10, // initial y position
300, // initial x size
200, // initial y size
hwnd, // parent window handle
NULL, // window menu handle
hInst, // program instance handle
NULL); // creation parameters
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
//GetClientRect(hwnd, &rc);
//FillRect(hdc, &rc, hBkBrush);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
bool IsMouseInside(HWND hwnd)
{
POINT pt;
RECT rc;
GetCursorPos(&pt);
GetWindowRect(hwnd, &rc);
if (pt.x >= rc.left && pt.x <= rc.right &&
pt.y >= rc.top && pt.y <= rc.bottom)
return true;
return false;
}
RECT GetChildRectInParent(HWND hwndParent,HWND hChild)
{
RECT rec;
GetClientRect(hChild, &rec);
MapWindowPoints(hChild, hwndParent, (POINT*)&rec, 2);
return rec;
}
void DrawHover(HWND hwnd)
{
HWND hParentWnd = GetParent(hwnd);
RECT rcInParent = GetChildRectInParent(hParentWnd, hwnd);
RECT rcParent;
GetClientRect(hParentWnd, &rcParent);
HDC hdc, hdcMem;
HBITMAP hBmp;
RECT rc;
GetClientRect(hwnd, &rc);
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBmp = CreateCompatibleBitmap(hdc, rcParent.right, rcParent.bottom);
SelectObject(hdcMem, hBmp);
FillRect(hdcMem, &rcParent, hBkBrush);
//HDC hdcParent = GetDC(hParentWnd);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, rcInParent.left, rcInParent.top,
SRCCOPY);
//ReleaseDC(hParentWnd, hdcParent);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(255,0,0));
char* str = "鼠标悬停";
TextOutA(hdc, 0, 0, str, strlen(str));
//TransparentBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, rc.right, rc.bottom,
// RGB(255, 255, 255));
ReleaseDC(hwnd,hdc);
DeleteObject(hdcMem);
DeleteObject(hBmp);
}
void DrawPressed(HWND hwnd)
{
HWND hParentWnd = GetParent(hwnd);
RECT rcInParent = GetChildRectInParent(hParentWnd, hwnd);
RECT rcParent;
GetClientRect(hParentWnd, &rcParent);
HDC hdc, hdcMem;
HBITMAP hBmp;
RECT rc;
GetClientRect(hwnd, &rc);
hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBmp = CreateCompatibleBitmap(hdc, rcParent.right, rcParent.bottom);
SelectObject(hdcMem, hBmp);
FillRect(hdcMem, &rcParent, hBkBrush);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, rcInParent.left, rcInParent.top,
SRCCOPY);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(255, 0, 0));
char* str = "鼠标按下";
TextOutA(hdc, 0, 0, str, strlen(str));
ReleaseDC(hwnd, hdc);
DeleteObject(hdcMem);
DeleteObject(hBmp);
}
void OnPaint(HWND hwnd, HDC hdc)
{
HWND hParentWnd = GetParent(hwnd);
RECT rcInParent = GetChildRectInParent(hParentWnd, hwnd);
RECT rcParent;
GetClientRect(hParentWnd, &rcParent);
HDC hdcMem;
HBITMAP hBmp;
RECT rc;
GetClientRect(hwnd, &rc);
//hdc = GetDC(hwnd);
hdcMem = CreateCompatibleDC(hdc);
hBmp = CreateCompatibleBitmap(hdc, rcParent.right, rcParent.bottom);
SelectObject(hdcMem, hBmp);
FillRect(hdcMem, &rcParent, hBkBrush);
BitBlt(hdc, 0, 0, rc.right, rc.bottom, hdcMem, rcInParent.left, rcInParent.top,
SRCCOPY);
SetBkMode(hdc, TRANSPARENT);
SetTextColor(hdc, RGB(255, 0, 0));
char* str = "默认样式";
TextOutA(hdc, 0, 0, str, strlen(str));
DeleteObject(hdcMem);
DeleteObject(hBmp);
}
LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TRACKMOUSEEVENT tme;
//避免因频繁触发WM_MOUSEMOVE而导致频繁绘图
static bool bEnter;
static bool bLeaveWithPressed;//按着离开
HDC hdcMem;
static HBITMAP hBmp;
RECT rc;
switch (message)
{
case WM_CREATE:
bEnter = false;
bLeaveWithPressed = false;
return 0;
//case WM_SIZE:
// return 0;
case WM_LBUTTONDOWN:
SetCapture(hwnd);
bLeaveWithPressed = false;
//画按下的状态
DrawPressed(hwnd);
return 0;
case WM_LBUTTONUP:
if (GetCapture() == hwnd)
{
if (IsMouseInside(hwnd))
{
//执行事件响应
//画鼠标悬停
DrawHover(hwnd);
}
ReleaseCapture();
}
return 0;
case WM_MOUSEMOVE:
if (GetCapture() == hwnd)
{
if (IsMouseInside(hwnd))
{
if (!bLeaveWithPressed)
{
bLeaveWithPressed = true;
//绘制按下的状态
DrawPressed(hwnd);
#ifdef _DEBUG
printf("带着焦点在窗口内移动\n");
#endif
}
}
else
{
if (bLeaveWithPressed)
{
bLeaveWithPressed = false;
//恢复默认样式
InvalidateRect(hwnd, NULL, FALSE);
UpdateWindow(hwnd);
#ifdef _DEBUG
printf("带着焦点在窗口外移动\n");
#endif
}
}
}
else
{
if (!bEnter)
{
bEnter = true;
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hwnd;
TrackMouseEvent(&tme);
DrawHover(hwnd);
#ifdef _DEBUG
printf("不带焦点在窗口内移动\n");
#endif
}
}
return 0;
case WM_MOUSELEAVE:
bEnter = false;
InvalidateRect(hwnd, NULL, FALSE);
UpdateWindow(hwnd);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
OnPaint(hwnd, hdc);
EndPaint(hwnd, &ps);
return 0;
case WM_ERASEBKGND:
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
Win32按钮设计思路
于 2022-04-09 01:02:55 首次发布