1

I'm kinda new to this area, so.. I made the following code, as far as i understand, in WM_PAINT i'm creating a line, however i can't see this line, because the window isn't displaying at all. i have a thread that calls WM_PAINT every 2 seconds, but every time nothing is displayed once running the program.

thanks in advanced.

#include <windows.h>
#include <stdio.h>

DWORD WINAPI StartThread1(LPVOID LPElm); 

//---------------------------------------------------------------------------
HWND hWnd;
LPCTSTR ClsName = L"WndMsg";
LPCTSTR WindowCaption = L"Windows and Controls Messages";
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
    MSG         Msg;
    WNDCLASSEX  WndClsEx;

    WndClsEx.cbSize        = sizeof(WNDCLASSEX);
    WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
    WndClsEx.lpfnWndProc   = WndProc;
    WndClsEx.cbClsExtra    = NULL;
    WndClsEx.cbWndExtra    = NULL;
    WndClsEx.hInstance     = hInstance;
    WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
    WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClsEx.lpszMenuName  = NULL;
    WndClsEx.lpszClassName = ClsName;
    WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    RegisterClassEx(&WndClsEx);

    hWnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                          ClsName,
                          WindowCaption,
                          WS_OVERLAPPEDWINDOW,
                          100,
                          120,
                          640,
                          480,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    DWORD ThreadId1, ThreadId2;
    HANDLE HandleThread1 = CreateThread(0,0,StartThread1,0,0,&ThreadId1);

    while( GetMessage(&Msg, NULL, 0, 0) )
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    return Msg.wParam;
}

//---------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
        HDC hDC;
    PAINTSTRUCT Ps;
    switch(Msg)
    {
    case WM_CREATE:
    //MessageBox(NULL, L"The window is being created", WindowCaption, MB_OK);
    break;
    case WM_SHOWWINDOW:
        break;

    case WM_PAINT:
    hDC = BeginPaint(hWnd, &Ps);
    MoveToEx(hDC, 0, 0, NULL);
    LineTo(hDC, 10, 10);
    EndPaint(hWnd, &Ps);

    break;

    case WM_DESTROY:
        PostQuitMessage(WM_QUIT);
        break;
    default:
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    return 0;
}
//---------------------------------------------------------------------------


DWORD WINAPI StartThread1(LPVOID LPElm)
{
    int n = 5000,i,j;

    for (i=0; i<n; i++)
    {
        SendMessage(hWnd,WM_PAINT,NULL,NULL);
        Sleep(2000);
        /*for (j=0;j<10;j++)
            a[j] = (rand() % 100);
        printf("\n");*/
    }

    return 0;
}
dusm
  • 1,207
  • 4
  • 18
  • 24
  • Long time since I did Win32, but shouldn't you load a brush into the HDC? Who's to say it's not drawing the line, but the line is white. And shouldn't there be a `+ 1` somewhere on that `hbrBackground` line... – ta.speot.is Sep 11 '11 at 10:01

2 Answers2

2

You should not send WM_PAINT yourself. You need to use InvalidateRect. You also need to be drawing with something - you need to use SelectObject to select a valid pen object into the DC.

tenfour
  • 36,141
  • 15
  • 83
  • 142
  • i try to write the function startthread1 like so, but still, when i try to debug it doesn't reach the WM_PAINT. got no idea why DWORD WINAPI StartThread1(LPVOID LPElm) { InvalidateRect(hWnd,NULL,TRUE); return 0; } – dusm Sep 11 '11 at 11:15
  • even if i try to put a rectangle struct in the function, and then call updatewindow, it doesn't call WM_PAINT. i don't understand why. is something wrong with my window ?? – dusm Sep 11 '11 at 11:55
  • worked it out, just used updatewindow instead of incalidaterect. – dusm Sep 11 '11 at 13:02
0

WM_PAINT message is sent by the system when the window is redrawn, so you don't need to send the message yourself. If you want to redraw the window use InvalidateRect. If you use Thread in your program, you should exit the thread with ExitProcess(ThreadID), then the following code is been executed. The default color for the pen is black, so you can see it. In your case, your thread didn't exit, so the program stops there. The window is never going to paint. You can use MessageBox() in your program to test where is running now.

Esteban Herrera
  • 2,263
  • 2
  • 23
  • 32
mafiatc
  • 1
  • 1