Click to See Complete Forum and Search --> : GDI - Repaint


Win32_student
November 10th, 2005, 06:43 AM
Hi everyone,
While I was using GDI, i noticed that when the window is minimized, resized or has a child window that covers it, the drawing disappears. Is there a way to solve this problam, redraw the painting?

Thanks in advance.

Marc G
November 10th, 2005, 06:59 AM
Windows will send a WM_PAINT message to a window when it needs to repaint itself. In Windows (prior to Windows Vista), the screen is represented by 1 big pixel buffer. When you move a window on top of another window, the pixels of the background window are lost. When you make the background window visible again, Windows will send a WM_PAINT message to the window so it can redraw itself.

That is the reason why all drawing should be done in your WM_PAINT handler and not somewhere else.

Win32_student
November 10th, 2005, 07:12 AM
Windows will send a WM_PAINT message to a window when it needs to repaint itself. In Windows (prior to Windows Vista), the screen is represented by 1 big pixel buffer. When you move a window on top of another window, the pixels of the background window are lost. When you make the background window visible again, Windows will send a WM_PAINT message to the window so it can redraw itself.

That is the reason why all drawing should be done in your WM_PAINT handler and not somewhere else.

It would have been fine, but in my program I have buttons with a dynamic text. How do I trace if they're getting a WM_PAINT message?

Marc G
November 10th, 2005, 07:44 AM
Are you drawing anything yourself? If so, where do you do your drawing? Can you post some code?

Win32_student
November 10th, 2005, 10:28 AM
Sure.

I Put this code, to draw all my buttons (I'm making a sudoku solving program) in the WM_CREATE of the main window:

for (count = 1 ; count <= 81 ; count++)
{
button[count].hButton = CreateWindowEx(WS_EX_CLIENTEDGE
, "BUTTON", "", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_OWNERDRAW,
(i * 55 - 55), (j * 55 - 55), //xy position, not very importent
52, 52, ProcWnd, (HMENU)(500 + count), GetModuleHandle(NULL), NULL);

//assign HDC
button[count].hdc = GetDC(sudoku.button[count].hButton);
}

Marc G
November 10th, 2005, 12:14 PM
Why are you storing all those DC's?
What do you do with them?

Win32_student
November 10th, 2005, 12:19 PM
Why are you storing all those DC's?
What do you do with them?

I send them to a function that draws them every change occures, It's like would should be in the WM_PAINT message, but since I can't track it, the drawing funciton is called every time a change is made in the button's information (According to the program's calculations, I don't think it's relevent to go into it).

Win32_student
November 10th, 2005, 12:30 PM
I tried to insert this code in the main message loop:

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
MainLoop(); //do the main loop of the program

TranslateMessage(&Msg); //translate the message so that WndProc can understand
/////////////////Supposed to take care of the button drawing issue
for (i = 1 ; i <= 81 ; i++)
if ( (Msg.hwnd == button[i].hButton) && (Msg.message == WM_PAINT))
drawButton(button[i].hdc, i);
/////////////////
DispatchMessage(&Msg); //send the message to WndProc
}


But it still doesn't draw it. Perhaps it just doesn't really send the WM_PAINT message to the buttons.

Marc G
November 10th, 2005, 12:35 PM
You are not reading my replies.

All drawing in Windows should happen in response to WM_PAINT.

So, if you want to redraw a button, you simply call InvalidateRect which will indirectly call your WM_PAINT handler.

Win32_student
November 10th, 2005, 12:45 PM
I finally Got it! Thank you very much for your help!