Continous sending of WM_PAINT message
Hi all,
I have a win32 application,in that user can draw different shapes on the screen by selecting different tool bar buttons with mouse ie,user can select a toolbar button,then i will change the cursor shape based on button clicked,after that when user clicking on the client area,that shape will print there.
My problem is after printing(drawing) 15 or 20 shapes(same or different),application getting hang,I tried to trace out the problem,i found that after a drawing a 15 or 20 shapes,system continously sending WM_PAINT message.In my code for each printing time(Clicking on the screen)i am invalidating the client area,so every time (Clicking mouse on the client area)system will send WM_PAINT message and drawing the shape on client area.What may be the reason for a continous WM_PAINT message sending without getting clicked on the screen
Regards,
Manju
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
manjut19
In my code for each printing time(Clicking on the screen)i am invalidating the client area,so every time (Clicking mouse on the client area)system will send WM_PAINT message and drawing the shape on client area.
That's not correct. All that invalidating the client area does, is tell the window to repaint itself the next time the message queue becomes empty. It doesn't send a WM_PAINT message directly.
Quote:
Originally Posted by
manjut19
What may be the reason for a continous WM_PAINT message sending without getting clicked on the screen
Well, you did something wrong. Maybe you are sending WM_PAINT messages yourself or calling UpdateWindow or invalidating in your WM_PAINT message handler. Hard to tell without seeing your code.
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
D_Drmmr
Well, you did something wrong. Maybe you are sending WM_PAINT messages yourself or calling UpdateWindow or invalidating in your WM_PAINT message handler. Hard to tell without seeing your code.
sorry sir, I canot post the code,because its a very big one,any way iam posting the WM_PAINT handler
Code:
case WM_PAINT:
GetClientRect(hWnd, &rc);
hdc = BeginPaint(hWnd, &ps);
SelectObject( hdc,hpen2);
if(g_bFirstPass)//First time blank screen
{
pLadder->SelectElement(hWnd,hdc,0); //Default painting
}
else if((!g_bEdting)) //user clicked on the screen for drawing
{
pLadder->SelectElement(hWnd,hdc,pLadder->m_iLastSelectedIndex); //Modified painting
}
else
break;
ReleaseDC(hWnd,hdc);
EndPaint(hWnd, &ps);
break;
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
manjut19
Code:
case WM_PAINT:
GetClientRect(hWnd, &rc);
hdc = BeginPaint(hWnd, &ps);
SelectObject( hdc,hpen2);
if(g_bFirstPass)//First time blank screen
{
pLadder->SelectElement(hWnd,hdc,0); //Default painting
}
else if((!g_bEdting)) //user clicked on the screen for drawing
{
pLadder->SelectElement(hWnd,hdc,pLadder->m_iLastSelectedIndex); //Modified painting
}
else
break;
ReleaseDC(hWnd,hdc);
EndPaint(hWnd, &ps);
break;
1. You have to select the original pen after the drawing:
Code:
hdc = BeginPaint(hWnd, &ps);
HPEN hOldPen = SelectObject( hdc,hpen2);
...
SelectObject( hdc, hOldPen);
EndPaint(hWnd, &ps);
2. Why do you call ReleaseDC(hWnd,hdc);? You don't need it!
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
VictorN
2. Why do you call ReleaseDC(hWnd,hdc);? You don't need it!
ReleaseDC function to release the display device context,what i used.Isn't correct?
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
manjut19
ReleaseDC function to release the display device context,what i used.Isn't correct?
From MSDN:
Quote:
Remarks
The application must call the ReleaseDC function for each call to the GetWindowDC function and for each call to the GetDC function that retrieves a common DC.
Do you call (and where?) either GetWindowDC function or the GetDC one?
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
VictorN
From MSDN:Do you call (and where?) either GetWindowDC function or the GetDC one?
No sir
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
manjut19
No sir
Then remove this line:
Quote:
Originally Posted by manjut19
Code:
ReleaseDC(hWnd,hdc);
Re: Continous sending of WM_PAINT message
i removed that line now,But my problem not yet solved.I have a doubt about WM_PAINT Message.Whenever the application message que empty system will send WM_PAINT message or only if we did an invalidation\validation of client area?
Re: Continous sending of WM_PAINT message
Well if you "have a doubt about WM_PAINT Message" why don't you read the documentation? :confused:
From MSDN WM_PAINT message:
Quote:
The WM_PAINT message is sent when the system or another application makes a request to paint a portion of an application's window. The message is sent when the UpdateWindow or RedrawWindow function is called, or by the DispatchMessage function when the application obtains a WM_PAINT message by using the GetMessage or PeekMessage function.
...
Re: Continous sending of WM_PAINT message
Sir please tell me what and all will be a reason for the failure of BeginPaint()function?
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
manjut19
Sir please tell me what and all will be a reason for the failure of BeginPaint()function?
Do you mean the BeginPaint() function returns NULL?
In such a case only GetLastError can tell you the exact reason!
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
VictorN
Do you mean the BeginPaint() function returns NULL?
In such a case only GetLastError can tell you the exact reason!
The program runs fine for several hundreds of frames.Then randomly freezes. On failure, the call to BeginPaint() reurns NULL. Once it fails the first time, it never recovers,ie application hang (unless terminated and restarted). Calling GetLastError() immediately after BeginPaint fails return a invalid Dc handle
Re: Continous sending of WM_PAINT message
Are you sure it is BeginPaint that fails with error code ERROR_DC_NOT_FOUND (1425)?
Please, post your actual code for painting, including all the error checkings!
Re: Continous sending of WM_PAINT message
Quote:
Originally Posted by
VictorN
Are you sure it is BeginPaint that fails with error code ERROR_DC_NOT_FOUND (1425)?
Please, post your actual code for painting, including all the error checkings!
Ya iam getting the error code 1425.
Code:
LRESULT CALLBACK LadderWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc,hdc1;
RECT rc;
g_bFirstPass=true;
switch (message)
{
case WM_PAINT:
{
GetClientRect(hWnd, &rc);
hdc = BeginPaint(hWnd, &ps);
if(hdc==NULL)
{
DWORD dw=GetLastError();
break;
}
else
{
HPEN hOldPen=SelectObject( hdc,hpen2);
if(g_bFirstPass)
{
pLadder->SelectElement(hWnd,hdc,0); //Default painting
}
else if((!g_bEdting))
{
pLadder->SelectElement(hWnd,hdc,pLadder->m_iLastSelectedIndex); //Modified painting
}
SelectObject( hdc,hOldPen);
}
EndPaint(hWnd, &ps);
}
break;
case WM_COMMAND:
{
g_bFirstPass=false;
switch(LOWORD(wParam))
{
case ID_CIRCLE:
{
hdc1=GetDc(hWnd);
DrawCircle( hdc1);//calling a function to draw a circle
ReleaseDc(hWnd,hdc1);
}
break;
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}