text on custom control
I have read and read, trying to find how to put text on a custom control. I have found stuff, but none of it has been clean and simple.
So how do I draw text on a custom control? here is code...
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam);
LRESULT 开发者_如何学GoCALLBACK CustProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam) ;
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
wchar_t * windowname = L"window Class";
wchar_t * cust = L"custctrl";
WNDCLASS wc = {0};
wc.lpszClassName = windowname;
wc.lpfnWndProc = WindowProc;
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
windowname,
L"app",
WS_VISIBLE | WS_THICKFRAME| WS_OVERLAPPEDWINDOW ,
50, 50,
500, 500,
NULL,
NULL,
hInstance,
NULL
);
WNDCLASS button = {0};
button.lpfnWndProc = CustProc;
button.lpszClassName = cust;
button.hInstance = hInstance;
button.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
button.hCursor = LoadCursor(NULL, IDC_HAND);
RegisterClass(&button);
HWND click = CreateWindowEx(
WS_EX_CLIENTEDGE,
cust,
L"Custom Control", //doesnt show up on the window, not to my suprise
WS_VISIBLE | WS_CHILD ,
0, 0,
500, 500,
hwnd,
NULL,
hInstance,
NULL
);
//all the rest...
}
LRESULT CALLBACK CustProc(HWND hwnd, UINT uMsg, WPARAM wparam, LPARAM lparam) {
switch(uMsg) {
case WM_CREATE:
SetWindowText(hwnd, L"button"); //also doesn't work, also not to my suprise
case WM_LBUTTONDOWN: {
MessageBox(hwnd, L"you clicked the custom button", L"cool", 0); // works fine
break;
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wparam, lparam);
}
You can catch the WM_PAINT
message in your CustProc
function and draw the text yourself.
You can get a drawing context by calling BeginPaint
, draw the text and close the drawing context by calling EndPaint
. You can draw text with the TextOut
function. Here is an example from MSDN:
LRESULT APIENTRY WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 0, 0, "Hello, Windows!", 15);
EndPaint(hwnd, &ps);
return 0L;
// Process other messages.
}
}
Full example here.
精彩评论