CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Apr 2013
    Posts
    77

    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

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by manjut19 View Post
    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 View Post
    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.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2013
    Posts
    77

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by D_Drmmr View Post
    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;

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by manjut19 View Post
    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!
    Victor Nijegorodov

  5. #5
    Join Date
    Apr 2013
    Posts
    77

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by VictorN View Post
    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?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by manjut19 View Post
    ReleaseDC function to release the display device context,what i used.Isn't correct?
    From MSDN:
    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?
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 2013
    Posts
    77

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by VictorN View Post
    From MSDNo you call (and where?) either GetWindowDC function or the GetDC one?
    No sir

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by manjut19 View Post
    No sir
    Then remove this line:
    Quote Originally Posted by manjut19
    Code:
        ReleaseDC(hWnd,hdc);
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2013
    Posts
    77

    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?

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Continous sending of WM_PAINT message

    Well if you "have a doubt about WM_PAINT Message" why don't you read the documentation?
    From MSDN WM_PAINT message:
    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.
    ...
    Victor Nijegorodov

  11. #11
    Join Date
    Apr 2013
    Posts
    77

    Re: Continous sending of WM_PAINT message

    Sir please tell me what and all will be a reason for the failure of BeginPaint()function?

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by manjut19 View Post
    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!
    Victor Nijegorodov

  13. #13
    Join Date
    Apr 2013
    Posts
    77

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by VictorN View Post
    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
    Last edited by manjut19; September 30th, 2013 at 03:26 AM.

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    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!
    Victor Nijegorodov

  15. #15
    Join Date
    Apr 2013
    Posts
    77

    Re: Continous sending of WM_PAINT message

    Quote Originally Posted by VictorN View Post
    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;
    	
    }

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured