English 中文(简体)
海关控制案文
原标题:text on custom control
  • 时间:2011-05-15 18:04:44
  •  标签:
  • c++
  • winapi

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 CALLBACK 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.   
    } 
} 

详情见here

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签