win32:TextOut未显示

时间:2010-12-23 14:50:02

标签: c++ winapi textout

我最近让我的主窗口使用WM_PAINT写文本,但现在我意识到它可能不是最好的信息,所以我正在尝试另一个版本;

主窗口包含一个菜单,在陈述菜单项时会发送ID_FILE_PID消息,并构建4个新窗口以及在主窗口中显示文本(paintEditSigns函数)。 4个窗口工作正常,但文本根本不起作用,除非我在main()函数中执行它,如图所示......这到底是什么? O_O

BTW:我仍然不知道为什么我发布时StackOverflow上的代码显示看起来如此奇怪,为什么会这样?

switch(message)
   {
   case WM_COMMAND:
     switch (LOWORD(wParam))
       {
            case ID_FILE_PID:
            {
                HWND hWndButton;    
                HWND hWndEdit;
                HWND hWndEdit2;
                HWND hWndDisplay;

                // drawing the text in mainwindow
                trigger=true;

                // adding new windows in the mainwindow
                hWndButton = CreateWindowEx(0,TEXT("BUTTON"),"Modify",WS_CHILD | WS_VISIBLE |
                BS_DEFPUSHBUTTON, 170,56,80,30,hWnd,(HMENU)ID_BUTTON,hThisInstance,NULL);   
                hWndEdit = CreateWindowEx(0,RICHEDIT_CLASS,TEXT(""),WS_CHILD | WS_VISIBLE | WS_BORDER,
                120,30,80,25,hWnd,(HMENU)ID_EDIT,hThisInstance,NULL);
                hWndEdit2 = CreateWindowEx(0,RICHEDIT_CLASS,TEXT(""),WS_CHILD | WS_VISIBLE | WS_BORDER,
                220,30,80,25,hWnd,(HMENU)ID_EDIT2,hThisInstance,NULL);
                hWndDisplay = CreateWindowEx(0,TEXT("STATIC"),NULL,WS_CHILD | WS_VISIBLE | WS_BORDER,
                0,100,450,140,hWnd,(HMENU)ID_DISPLAY,hThisInstance,NULL);


                            UpdateWindow(hWnd);

                break;
            }

.....

case WM_PAINT:
     {
         if (trigger) {
             paintEditSigns()
         }
         break;

     }

//
// Main function
// 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
               LPSTR lpCmdLine, int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;
    ZeroMemory(&wc, sizeof(WNDCLASSEX));
    hThisInstance = hInstance;
    LoadLibrary("Riched20.dll");

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszMenuName = MAKEINTRESOURCE(IDR_MYMENU);
    if(!(wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MYICON)))) {
        HRESULT res = GetLastError();

    }
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = TEXT("testcpp");
    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL, 
                            wc.lpszClassName,
                            TEXT("test"),
                            WS_OVERLAPPEDWINDOW,
                            300,
                            200,
                            450,
                            300,
                            NULL,
                            NULL,
                            hInstance,
                            NULL);
    ShowWindow(hWnd,nCmdShow);
        //paintEditSigns() -- here it works, but not when in the message part

    MSG msg;
    while (GetMessage(&msg, NULL,0,0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

    }


    return msg.wParam;
}



void paintEditSigns() {
    HFONT hf = createFont();
    PAINTSTRUCT ps;
    HWND hWnd = FindWindow(TEXT("testcpp"),TEXT("test"));
    HBRUSH hbruzh = CreateSolidBrush(RGB(0,0,0));
    HDC hdz = BeginPaint(hWnd,&ps); 
    string s = "Memory Address";

    SelectBrush(hdz,hbruzh);
    SelectFont(hdz,hf);
    TextOut(hdz,0,100,s.c_str(),s.length());
    EndPaint(hWnd,&ps);

    DeleteObject(hbruzh);   

}

HFONT createFont() {
    HDC hdc;
    long lfHeight;

    hdc = GetDC(NULL);
    lfHeight = -MulDiv(12, GetDeviceCaps(hdc, LOGPIXELSY), 72);
    ReleaseDC(NULL, hdc);

    HFONT hf = CreateFont(lfHeight, 0, 0, 0, 0, TRUE, 0, 0, 0, 0, 0, 0, 0, "MS Sans Serif");
    return hf;

}

1 个答案:

答案 0 :(得分:1)

您只能 使用BeginPaint / EndPaint来回复WM_PAINT。而WM_PAINT是适合这样做的地方。

当窗口的一部分“无效”时,Windows调用WM_PAINT。例如,如果您恢复窗口,或者在移开窗口或调整窗口大小后窗口的一部分变为可见。

当您需要手动更新显示时,通过调用InvalidateRect“无效”需要重绘的区域(这会告诉Windows窗口中需要重绘的区域)。

常见的是使整个窗口无效,而不是计算你想要绘制的区域的实际像素完美边界。