Search code examples
c++mfcpictureboxdrawrectangle

Allow user to draw a drag rectangle in CStatic C++ MFC App


I have a MFC application where I have a Picture Control in the dialog. Eventually, I want to allow a user to draw a resizeable rectangle via mouse drag in the picture control over an image that I loaded.

I defined my own picture control class as a sub class of CStatic and am working with the mouse down, mouse up, and mouse move events but I can't seem to figure out how to allow the user to draw a rectangle. Any guidance on this would be appreciated.

Most of the examples I've looked at show me how to draw a rectangle in a CView:CWnd, but I'm not too familiar with MFC yet so I'm a bit lost. Thanks.


Solution

  • The usual technique for drawing a drag rect on top of the window contents is illustrated here:

    http://msdn.microsoft.com/en-us/library/windows/desktop/dd145184(v=vs.85).aspx

    That is Win32 API coding instead of MFC coding but the differences are minimal. The basic idea is that by drawing with SetROP2(hdc, R2_NOTXORPEN); you invert the existing pixels, then drawing the same rect again re-inverts those pixels back to the original image.