ROP Codes, Rubber Bands, Clip Regions '& Coordinate Transforms

Environment: VC6, 95/98/NT

Skill Level: Beginner, Intermediate  

This sample shows how to:

1. track the mouse using CWnd messages

  •     WM_LBUTTONDOWN -- use this message to start tracking the mouse motions, and save the starting point for drawing a rubber band rectangle
  •     WM_LBUTTONUP -- use this message to end mouse tracking and end drawing
  •     WM_MOUSEMOVE -- use this message to draw a rectangle and update the x-y coordinate readout

2. draw a "rubber band" rectangle

On occasion you will need to draw a selection rectangle to select a group of drawing objects. This example project shows one way to do this efficiently in the WM_MOUSEMOVE handler. As the rectangle is stretched with the mouse, the old rectangle is erased and new one drawn using a special ROP2 code for the device context.

3. use ROP2 drawing codes

There are several ROP2 device context drawing codes available, and you can experiment with each code by selecting from the Mode menu. The most interesting codes for rubber band rectangles are R2_NOT and R2_NOTXORPEN. Play with all of them to see the results of the logical operations combining new and existing pixels.

4. draw in logical units
5. reorient the coordinate system and logical origin

Occasionally you may want to plot data using you own coordinate system. The sample changes the device context mapping mode to a 1st quadrant cartesian coordinate system. It's a useful technique if you want to plot x-y graph data in user units. MM_ISOTROPIC is used so that the drawings aspect ratio is preserved as the window is resized.


void CRopFunView::SetDCProperties(CDC *pDC)
{
   CRect r;
   GetClientRect(r);

   pDC->SetMapMode(MM_ISOTROPIC );
   pDC->SetWindowExt( XEXTENTS_LOGICALUNITS, -YEXTENTS_LOGICALUNITS );
   pDC->SetViewportExt( r.Width(), r.Height() );
   pDC->SetViewportOrg( 0, r.Height() );
   pDC->SetWindowOrg( -XORIGIN_LOGICALUNITS, -YORIGIN_LOGICALUNITS );
} 

6. clip drawing outside a window region  

Some of the MFC classes and methods demonstrated are:

CDC:

  • SetMapMode
  • WindowExt
  • SetViewportExt
  • SetViewportOrg
  • SetWindowOrg
  • MoveTo
  • LineTo
  • Rectangle
  • SetBkMode
  • TextOut
  • SelectObject
  • DPtoLP
  • GetClipBox

CRgn:

  • CreateRectRgn
  • CreateRectRgnIndirect

Running the sample: Use the left mouse button to drag a rectangle inside the gray rectangle. Select different ROP2 codes from the menu.  

Download demo project - 21 Kb

Download source - 9 Kb



Comments

  • How to produce Rubber band effect in Win32 SDK based Apps

    Posted by Legacy on 01/09/2002 12:00am

    Originally posted by: Pankaj

    Can anybody tell me that how to create a rubber band operation in Win32 application dilaog at runtime.
    As i am creating a Win32 Dialog box at runtime where i m creating a Grid using Win32 APIs of GDI.
    Now i want to creat a rubber band effect on these cells.
    How to achieve it
    thanks in advance
    PANKAJ

    Reply
  • Problems in Transformation

    Posted by Legacy on 12/06/2001 12:00am

    Originally posted by: Abhijit Deshkar

    Give some coding on transformations. I want to move line,rectangle,circle,freehand etc & also it should be
    resizable.

    Reply
  • Resource leak

    Posted by Legacy on 10/24/2001 12:00am

    Originally posted by: Max

    You are using lots of line such as

    CDC *pDC = GetDC();

    These should have a matching ReleaseDC( CDC* pDC ); else you will get a resource leak

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: May 18, 2015 While the idea of using facial and/or gesture recognition to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development efforts. There are many different ways in which Intel and Unity Technologies have been working together to help speed the development of games with the Intel&reag; RealSense™ SDK (software developer kit). Check out this webcast to join a panel of experts as they …

  • On-Demand eSeminar DevOps and Cloud are all the rage in IT, but the two terms relating process and computing aren't mutually exclusive. Join us to see how your movement into cloud changes the way you develop, deploy, test and manage, and how DevOps can actually be a good thing when coupled with cloud.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date