我正在尝试使用Windows C API制作自定义Windows 8样式按钮。然而,当我在重绘按钮后尝试在按钮上绘制文本时,没有任何可见的事情发生! 我甚至尝试创建一个静态窗口作为按钮的子窗口,并在重绘按钮时向其发送WS_ERASEBKGND消息,但是当我第一次重绘按钮时它就消失了! 这是我的自定义按钮的窗口过程:
LRESULT CALLBACK ButtonWindowProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//static HWND static_text_handle;
HDC hdc;
PAINTSTRUCT ps;
DWORD color;
HFONT font,holdFont;
static RECT rect;
wchar_t test[] = L"test";
TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT),TME_LEAVE,hwnd,HOVER_DEFAULT};
/*static*/ HBRUSH brush = CreateSolidBrush(RGB(20,30,40));
/*static*/ HBRUSH clicked_brush = CreateSolidBrush(RGB(40,50,60));
/*static*/ HBRUSH hover_brush = CreateSolidBrush(RGB(70,80,90));
//TrackMouseEvent(&tme);
switch(msg)
{
case WM_CREATE:
//static_text_handle = CreateWindowW(L"Static",L"test",WS_CHILD | WS_VISIBLE,0,0,20,20,hwnd,NULL,NULL,NULL);
//brush = ((struct custom_button_colors*)(lParam))->default_color;
//clicked_brush = ((struct custom_button_colors*)(lParam))->push_color;
//hover_brush = ((struct custom_button_colors*)(lParam))->hover_color;
break;
case WM_LBUTTONDOWN:
SendMessage(hwnd,WM_ERASEBKGND,(WPARAM)GetDC(hwnd),(LPARAM)LPARAM_L_DOWN);
break;
case WM_LBUTTONUP:
SendMessage(hwnd,WM_ERASEBKGND,(WPARAM)GetDC(hwnd),(LPARAM)LPARAM_L_UP);
break;
case WM_MOUSEMOVE:
//Beep(1000,1000);
TrackMouseEvent(&tme);
if (GetAsyncKeyState(VK_LBUTTON))
{
SendMessage(hwnd,WM_ERASEBKGND,(WPARAM)GetDC(hwnd),(LPARAM)LPARAM_L_DOWN);
}
else
{
SendMessage(hwnd,WM_ERASEBKGND,(WPARAM)GetDC(hwnd),(LPARAM)LPARAM_MOUSEHOVER);
}
break;
case WM_MOUSELEAVE:
SendMessage(hwnd,WM_ERASEBKGND,(WPARAM)GetDC(hwnd),(LPARAM)-1);
break;
case WM_ERASEBKGND:
GetClientRect(hwnd,&rect);
if (lParam == LPARAM_L_DOWN)
{
FillRect((HDC)wParam,&rect,clicked_brush);
}
else if (lParam == LPARAM_MOUSEHOVER)
{
FillRect((HDC)wParam,&rect,hover_brush);
break;
}
else
{
POINT mousepos;
GetCursorPos(&mousepos);
if (WindowFromPoint(mousepos) == hwnd)
{
FillRect((HDC)wParam,&rect,hover_brush);
}
else
{
FillRect((HDC)wParam,&rect,brush);
}
}
hdc = BeginPaint(hwnd,&ps);
color = GetSysColor(COLOR_BTNFACE);
SetBkColor(hdc,color);
font = CreateFontW(25, 0, 0, 0, FW_MEDIUM, 0, 0, 0, 0,0, 0, ANTIALIASED_QUALITY, 0, L"Tahoma");
holdFont = SelectObject(hdc, font);
TextOutW(hdc,200,200,test,lstrlenW(test));
SelectObject(hdc,holdFont);
DeleteObject(font);
EndPaint(hwnd,&ps);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
我是Windows编程的绝对初学者,如果我的问题很简单,那就很抱歉。
答案 0 :(得分:1)
1)不要在WM_ERASEBKGND中使用BeginPaint / EndPaint。将该代码移动到WM_PAINT处理程序中。
2)使用SetWindowLong和GWL_USERDATA索引存储按钮状态(UP或DOWN),并使用GetWindowLong检索当前状态。
3)使用SetCapture捕获鼠标,从WM_LBUTTONDOWN开始。根据鼠标位置
更新按钮的状态(向上或向下)4)当你需要绘画时使用RedrawWindow