![]() The final part of the rubber band rectangle drawing occurs in the Rectangle is then redrawn through a call to DrawXORRectangle() with oneĬorner at the new mouse coordinates. If the mouse is down and moving, a rectangle has alreadyīeen drawn and must be erased with a call to DrawXORRectangle(). This prevents an attempt at drawing when the mouse is simply Drawing will actually only occur if the mouseīutton is down. The stage is now set for the actual drawing, which takes place in the (clear the haveRect flag and call Invalidate() To initiate an operation to clear the rubber band rectangle from the screen The Left mouse button that is being pressed. Mouse down event is stored in XDown and YDown. The rubber banding operation is initiated in MainForm_MouseDown where theįlag is set to indicate that the mouse button is down. ![]() The rectangle drawing functionality is nearly the same as in sedatkurt's code. Is set) so that the dotted rectangle is also then redrawn. However, I've also added a call to DrawXORRectangle() ifĪ rubber band rectangle was present (the flag haveRect With a number of rectangles painted on its client area in the MainForm_PaintĮvent handler. The demo application is in the file MainForm.cs. Theĭevice context is released and the function is finished. Stock resources do not need to be deleted since they're only borrowed anyway. The drawing is now performed, the oldīrush and pen put back into the device context, and the new pen deleted. Stock NULL_BRUSH is created and simultaneously selected into the device context,Īgain saving the old brush handle for later. Replacement later (always clean up resources when you're finished with them). The new pen selected into the device context the old pen's handle is saved for The ROP drawing mode is then set to XOR and A blackĭotted pen is created one pixel wide. The process of drawing the XORed rectangle begins with extracting the Win32 GDIĭevice context from the GDI+ Graphics object passed to the function. The pen color isįixed internally in the class as a predefined BLACK_PEN in theĪlthough I kept sedatkurt's RGB conversion macro in case. Not originally going to include a capability to reset this value, but I relentedĪnd made the pen style a property with get/set capability. The default constructor sets the pen style to the default value of PS_DOT. Public void DrawXORRectangle( Graphics grp, Visible), and a brush style to NOT fill in the rectangle ( NULL_BRUSH) so the The pen ( R2_XORPEN), a pen width of 1 pixel (doesn't have to be pretty, just To draw the rectangle border as a dotted line (PS_DOT), an XOR drawing mode for Namespace in the file RubberbandRects.cs. Is concentrated into the RubberbandRectangle class in the Consequently, all and only the rubber band rectangle related code Primarily interested in developing a strictly rubber band rectangle drawingĬapability. While I adopted the basic concept that sedatkurt showed in his code, I was NET Framework Team did not include ROP drawing control in GDI+. Restores the original pixel colors, effectively erasing the rectangle. Redrawing in XOR mode along the same rectangle with the same pen automatically If the drawing mode is set toĪn XOR raster operation (ROP), the rectangle is usually completely visible sinceĪll bits of the pixels are XORed with the color of the drawing pen. Until the final version is drawn at mouse button up. Along the way, the rectangle is therefore drawn and erased many times The mouse position at each move event is the second corner of the drawn ![]() Starting corner with the left mouse button depressed until the button is In historical Windows®Īpplications, this selection rectangle is normally drawn by the mouse from a To act on only a portion of the displayed image. I was sure it could be extended to rectangles as well.Īnyone who has used a graphics program has probably also used a selection rectangle When I saw the article "Using GDI and GDI+ mixed drawing rubber band lines"īy sedatkurt in the MFC/C++ > GDI > Unedited Reader Contributions in TheĬodeProject. NET Framework context, but they were fairly complex. I’ve seen severalĪpproaches to solve this problem that have, for the most part, stayed within Needed to outline a selected Region of Interest in a graphic. I have been frustrated, like many others apparently, by the lack of XOR drawingĬapabilities in the.
0 Comments
Leave a Reply. |