win32: TextOut not being displayed

Posted by KaiserJohaan on Stack Overflow See other posts from Stack Overflow or by KaiserJohaan
Published on 2010-12-23T14:50:02Z Indexed on 2010/12/23 14:54 UTC
Read the original article Hit count: 209

Filed under:
|
|

Hello again, I recently having my mainwindow write text by using WM_PAINT, but now I realise it was maybe not the best message to do so in, so I'm trying another version;

The mainwindow contains a menu, upon clicing a menu item the ID_FILE_PID msg is sent and it builds the 4 new windows aswell as displays text in the mainwindow (paintEditSigns function). The 4 windows works fine but the text dosn't work at all, unless I do it in the main() function as shown... what on earth is this? O_O

BTW: I still have no clue why the code-display on StackOverflow keeps looking so wierd when I post, why is this?

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
                paintEditSigns();      --    does not do anything here!

                // 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);


                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);   
    UpdateWindow(hWnd);
}

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;

}

© Stack Overflow or respective owner

Related posts about c++

Related posts about winapi