Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. FillRect C++

FillRect C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharpvisual-studiographicsdebugging
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arthur89kim
    wrote on last edited by
    #1

    Hi all, I am using Visual Studio 2005 with MFC application. I have create a transparent window with following codes:

    	HINSTANCE hInst = ::GetModuleHandle(NULL);
    
    	WNDCLASSEX wc;
    	wc.cbSize=sizeof(WNDCLASSEX);
    	wc.cbClsExtra = 0; 
    	wc.cbWndExtra = 0; 
    	wc.hbrBackground = (HBRUSH)GetStockObject( NULL\_BRUSH );
    	wc.hCursor = LoadCursor( NULL, IDC\_CROSS );  
    	wc.hIcon = LoadIcon( NULL, IDI\_APPLICATION );
    	wc.hInstance = hInst;
    	wc.lpfnWndProc = (WNDPROC) CCaptureDlg::WndProc;
    	wc.lpszClassName = L"Test";  
    	wc.lpszMenuName = 0;
    	wc.style = CS\_HREDRAW | CS\_VREDRAW;
    	wc.hIconSm = NULL;
    
                m\_hWnd=::CreateWindowEx(WS\_EX\_TRANSPARENT|WS\_EX\_TOPMOST,L"Test",0, WS\_VISIBLE|WS\_POPUP,0,0, m\_ComputerResolutionX, m\_ComputerResolutionY,0,0,0,this);
    

    In that Window "Test", I can drag and draw a rectangle but when I keep dragging and moving the mouse around the screen, it does not clear off my previous drawing, is there a way to clear off my previous drawing? I have tried FillRect as following but still does not work:

           case WM\_MOUSEMOVE:
                {
    		if (pClass->m\_MouseDown==1)  //to indicate mouse is pressed before drawing
    		{
                        HDC hdc;
    		hdc = ::GetWindowDC(hwnd);
                        RECT re;
    		SetRect(&re, 0,0,pClass->m\_ComputerResolutionX,pClass->m\_ComputerResolutionY);
    
    		HBRUSH brush =(HBRUSH)::GetStockObject(NULL\_BRUSH);
    		if (pClass->m\_tempPoint.x!=p.x && pClass->m\_tempPoint.y!=p.y)    //if mouse move into different position
    		{
    			static int count = 0;
    			TRACE(L"Test %d\\n", count++);
    			FillRect(hdc,&re,brush);
    			pClass->m\_tempPoint=p;
    		}
    
    		CPen mypen(PS\_DOT, 1, RGB(0,0,0));
    		HPEN pen = (HPEN)mypen;
    
    		SelectObject(hdc, pen);
    		SelectObject(hdc, brush);
                        ::Rectangle( hdc, pClass->m\_InitPoint.x, pClass->m\_InitPoint.y, p.x , p.y);
                        }
    		break;
                }
    

    Or is there another way to remove my previous drawing on this transparent window other than FillRect? I would be pleased to learn about it. TQ~

    Teaching and learning is a cycle... Learning something new enable to teach others; Teaching others enable self learning.

    L E 2 Replies Last reply
    0
    • A arthur89kim

      Hi all, I am using Visual Studio 2005 with MFC application. I have create a transparent window with following codes:

      	HINSTANCE hInst = ::GetModuleHandle(NULL);
      
      	WNDCLASSEX wc;
      	wc.cbSize=sizeof(WNDCLASSEX);
      	wc.cbClsExtra = 0; 
      	wc.cbWndExtra = 0; 
      	wc.hbrBackground = (HBRUSH)GetStockObject( NULL\_BRUSH );
      	wc.hCursor = LoadCursor( NULL, IDC\_CROSS );  
      	wc.hIcon = LoadIcon( NULL, IDI\_APPLICATION );
      	wc.hInstance = hInst;
      	wc.lpfnWndProc = (WNDPROC) CCaptureDlg::WndProc;
      	wc.lpszClassName = L"Test";  
      	wc.lpszMenuName = 0;
      	wc.style = CS\_HREDRAW | CS\_VREDRAW;
      	wc.hIconSm = NULL;
      
                  m\_hWnd=::CreateWindowEx(WS\_EX\_TRANSPARENT|WS\_EX\_TOPMOST,L"Test",0, WS\_VISIBLE|WS\_POPUP,0,0, m\_ComputerResolutionX, m\_ComputerResolutionY,0,0,0,this);
      

      In that Window "Test", I can drag and draw a rectangle but when I keep dragging and moving the mouse around the screen, it does not clear off my previous drawing, is there a way to clear off my previous drawing? I have tried FillRect as following but still does not work:

             case WM\_MOUSEMOVE:
                  {
      		if (pClass->m\_MouseDown==1)  //to indicate mouse is pressed before drawing
      		{
                          HDC hdc;
      		hdc = ::GetWindowDC(hwnd);
                          RECT re;
      		SetRect(&re, 0,0,pClass->m\_ComputerResolutionX,pClass->m\_ComputerResolutionY);
      
      		HBRUSH brush =(HBRUSH)::GetStockObject(NULL\_BRUSH);
      		if (pClass->m\_tempPoint.x!=p.x && pClass->m\_tempPoint.y!=p.y)    //if mouse move into different position
      		{
      			static int count = 0;
      			TRACE(L"Test %d\\n", count++);
      			FillRect(hdc,&re,brush);
      			pClass->m\_tempPoint=p;
      		}
      
      		CPen mypen(PS\_DOT, 1, RGB(0,0,0));
      		HPEN pen = (HPEN)mypen;
      
      		SelectObject(hdc, pen);
      		SelectObject(hdc, brush);
                          ::Rectangle( hdc, pClass->m\_InitPoint.x, pClass->m\_InitPoint.y, p.x , p.y);
                          }
      		break;
                  }
      

      Or is there another way to remove my previous drawing on this transparent window other than FillRect? I would be pleased to learn about it. TQ~

      Teaching and learning is a cycle... Learning something new enable to teach others; Teaching others enable self learning.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You should not be drawing to your window in the message handler for the mousemove. The mouse move code should set the limits and other details of your shape, but the actual drawing should be done in your OnDraw() or OnPaint() handler. There are lots of samples on MSDN[^].

      It's time for a new signature.

      M 1 Reply Last reply
      0
      • A arthur89kim

        Hi all, I am using Visual Studio 2005 with MFC application. I have create a transparent window with following codes:

        	HINSTANCE hInst = ::GetModuleHandle(NULL);
        
        	WNDCLASSEX wc;
        	wc.cbSize=sizeof(WNDCLASSEX);
        	wc.cbClsExtra = 0; 
        	wc.cbWndExtra = 0; 
        	wc.hbrBackground = (HBRUSH)GetStockObject( NULL\_BRUSH );
        	wc.hCursor = LoadCursor( NULL, IDC\_CROSS );  
        	wc.hIcon = LoadIcon( NULL, IDI\_APPLICATION );
        	wc.hInstance = hInst;
        	wc.lpfnWndProc = (WNDPROC) CCaptureDlg::WndProc;
        	wc.lpszClassName = L"Test";  
        	wc.lpszMenuName = 0;
        	wc.style = CS\_HREDRAW | CS\_VREDRAW;
        	wc.hIconSm = NULL;
        
                    m\_hWnd=::CreateWindowEx(WS\_EX\_TRANSPARENT|WS\_EX\_TOPMOST,L"Test",0, WS\_VISIBLE|WS\_POPUP,0,0, m\_ComputerResolutionX, m\_ComputerResolutionY,0,0,0,this);
        

        In that Window "Test", I can drag and draw a rectangle but when I keep dragging and moving the mouse around the screen, it does not clear off my previous drawing, is there a way to clear off my previous drawing? I have tried FillRect as following but still does not work:

               case WM\_MOUSEMOVE:
                    {
        		if (pClass->m\_MouseDown==1)  //to indicate mouse is pressed before drawing
        		{
                            HDC hdc;
        		hdc = ::GetWindowDC(hwnd);
                            RECT re;
        		SetRect(&re, 0,0,pClass->m\_ComputerResolutionX,pClass->m\_ComputerResolutionY);
        
        		HBRUSH brush =(HBRUSH)::GetStockObject(NULL\_BRUSH);
        		if (pClass->m\_tempPoint.x!=p.x && pClass->m\_tempPoint.y!=p.y)    //if mouse move into different position
        		{
        			static int count = 0;
        			TRACE(L"Test %d\\n", count++);
        			FillRect(hdc,&re,brush);
        			pClass->m\_tempPoint=p;
        		}
        
        		CPen mypen(PS\_DOT, 1, RGB(0,0,0));
        		HPEN pen = (HPEN)mypen;
        
        		SelectObject(hdc, pen);
        		SelectObject(hdc, brush);
                            ::Rectangle( hdc, pClass->m\_InitPoint.x, pClass->m\_InitPoint.y, p.x , p.y);
                            }
        		break;
                    }
        

        Or is there another way to remove my previous drawing on this transparent window other than FillRect? I would be pleased to learn about it. TQ~

        Teaching and learning is a cycle... Learning something new enable to teach others; Teaching others enable self learning.

        E Offline
        E Offline
        Eugen Podsypalnikov
        wrote on last edited by
        #3

        Additional to the words of Richard this "dotted rectangle"[^] could be used... :)

        virtual void BeHappy() = 0;

        1 Reply Last reply
        0
        • L Lost User

          You should not be drawing to your window in the message handler for the mousemove. The mouse move code should set the limits and other details of your shape, but the actual drawing should be done in your OnDraw() or OnPaint() handler. There are lots of samples on MSDN[^].

          It's time for a new signature.

          M Offline
          M Offline
          malcombold
          wrote on last edited by
          #4

          what if i use bitblt to draw the rectangle? However, there is a problem i faced, after i've bitblt, the background turns to be black color. I just don't why? "BitBlt(Hdc, 0, 0, pClass->m_ComputerResolutionX, pClass->m_ComputerResolutionY, pClass->m_MemDC, 0, 0, SRCCOPY);" The bitblt was done in WM_PAINT. Anyone has any ideas?

          L 1 Reply Last reply
          0
          • M malcombold

            what if i use bitblt to draw the rectangle? However, there is a problem i faced, after i've bitblt, the background turns to be black color. I just don't why? "BitBlt(Hdc, 0, 0, pClass->m_ComputerResolutionX, pClass->m_ComputerResolutionY, pClass->m_MemDC, 0, 0, SRCCOPY);" The bitblt was done in WM_PAINT. Anyone has any ideas?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I'm not sure why this question was posted to me.

            It's time for a new signature.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups