Click here to Skip to main content
12,757,882 members (38,467 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 23 Jun 2003

Using GDI and GDI+ mixed drawing rubber band lines

, 23 Jun 2003
Rate this:
Please Sign up or sign in to vote.
Using GDI and GDI+mixed,Rubber Band Lines in GDI+
<!-- Article image -->

<!-- Add the rest of your HTML here -->


As every programmer knows. the easiest and fastest method to draw self erasing lines is drawing line with XOR mode. But in .NET I could not see any method that makes self erasing lines, so it forced me to use GDI and GDI+ in mixed mode. I think, using GDI+ there is no way to draw self erasing lines. I've seen some methods invented to draw self erasing lines but about all of them are using BitBlting or pushing the whole screen to buffer, and afterwards popping the buffer to erase them.      

The Code

/* to be example to the xoring problem 
 * i ve written a simple namespace to referance to standat GDI.
 * But the GDIDraw namespace 
 * doesnt referance to whole methots such as drawing 
 * rectangles or other shapes
 * but can easily be enhanged if necessary */
namespace GDIDraw
    public enum PenStyles

        ,PS_ENDCAP_ROUND=     0x00000000
        ,PS_ENDCAP_SQUARE=    0x00000100
        ,PS_ENDCAP_FLAT=      0x00000200
        ,PS_ENDCAP_MASK =     0x00000F00
        ,PS_JOIN_ROUND=       0x00000000
        ,PS_JOIN_BEVEL=       0x00001000
        ,PS_JOIN_MITER=       0x00002000
        ,PS_JOIN_MASK=        0x0000F000

        ,PS_COSMETIC=         0x00000000
        ,PS_GEOMETRIC=        0x00010000
        ,PS_TYPE_MASK=        0x000F0000
    public enum drawingMode
        R2_BLACK=            1   /*  0       */
        ,R2_NOTMERGEPEN=      2   /* DPon     */
        ,R2_MASKNOTPEN=       3   /* DPna     */
        ,R2_NOTCOPYPEN=       4   /* PN       */
        ,R2_MASKPENNOT=       5   /* PDna     */
        ,R2_NOT=              6   /* Dn       */
        ,R2_XORPEN=           7   /* DPx      */
        ,R2_NOTMASKPEN=       8   /* DPan     */
        ,R2_MASKPEN=          9   /* DPa      */
        ,R2_NOTXORPEN=        10  /* DPxn     */
        ,R2_NOP=              11  /* D        */
        ,R2_MERGENOTPEN=      12  /* DPno     */
        ,R2_COPYPEN=          13  /* P        */
        ,R2_MERGEPENNOT=      14  /* PDno     */
        ,R2_MERGEPEN=         15  /* DPo      */
        ,R2_WHITE=            16  /*  1       */
        ,R2_LAST=             16

    public class GDI
        private IntPtr hdc;
        private System.Drawing.Graphics grp;
        public void BeginGDI(System.Drawing.Graphics g)

        public void EndGDI()

        public IntPtr CreatePEN(
            PenStyles fnPenStyle,    // pen style
            int nWidth,        // pen width
            int crColor   // pen color
           return CreatePen(fnPenStyle,nWidth,crColor);

        public bool DeleteOBJECT(IntPtr hObject)
          return DeleteObject(hObject);

        public IntPtr SelectObject(
            IntPtr hgdiobj   // handle to object
             return SelectObject(hdc,hgdiobj);

        public void MoveTo(int X,int Y)

        public void LineTo(int X,int Y)

        public int SetROP2(drawingMode fnDrawMode)
           return SetROP2(hdc,fnDrawMode);

        private static extern int SetROP2(
            IntPtr hdc,         // handle of device context
            drawingMode fnDrawMode      // drawing mode
        private static extern bool MoveToEx(
            IntPtr hdc,          // handle to device context
            int X,            // x-coordinate of new current position
            int Y,            // y-coordinate of new current position
            int oldp// pointer to old current position

        private static extern bool LineTo(
            IntPtr hdc,    // device context handle
            int nXEnd,  // x-coordinate of line's ending point
            int nYEnd   // y-coordinate of line's ending point

        private static extern IntPtr CreatePen(
            PenStyles fnPenStyle,    // pen style
            int nWidth,        // pen width
            int crColor   // pen color

        private static extern bool DeleteObject(IntPtr hObject   
                                               // handle to graphic object

        private static extern IntPtr SelectObject(
            IntPtr hdc,          // handle to device context
            IntPtr hgdiobj   // handle to object

        public static int RGB(int R,int G,int B)
            return (R|(G<<8)|(B<<16));         


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Engineer Üç yıldız otomotiv
Turkey Turkey
i m an electrics engineer
programming is my hobby

You may also be interested in...


Comments and Discussions

QuestionLinestyle DashDotDot Pin
Gwannoes28-Jun-07 3:01
memberGwannoes28-Jun-07 3:01 
Generalmanaged c++ port Pin
_eymre_5-Feb-07 13:57
member_eymre_5-Feb-07 13:57 
GeneralGoruntuyu gray level a cevirmek Pin
kkirtac15-Dec-05 23:49
memberkkirtac15-Dec-05 23:49 
GeneralDDJ article Pin
DavidCrow29-Jun-05 4:05
memberDavidCrow29-Jun-05 4:05 
GeneralReg. Restoring the original pen. Pin
P. G. Prakash12-Apr-05 23:00
memberP. G. Prakash12-Apr-05 23:00 
GeneralRe: Reg. Restoring the original pen. Pin
sedatkurt19-Apr-05 23:22
membersedatkurt19-Apr-05 23:22 
GeneralDelete the Bitmap object Pin
Alex Evans6-Feb-05 15:57
memberAlex Evans6-Feb-05 15:57 
GeneralThe Best Example, Thanks! Pin
OICU81224-Dec-03 7:38
memberOICU81224-Dec-03 7:38 
GeneralInteresting mixing :-) Pin
Nishant S18-Sep-03 1:54
editorNishant S18-Sep-03 1:54 
GeneralControlPaint class Pin
Anonymous1-Jul-03 4:02
sussAnonymous1-Jul-03 4:02 
GeneralRe: ControlPaint class Pin
cthomas4-Sep-03 19:04
membercthomas4-Sep-03 19:04 
GeneralRe: ControlPaint class Pin
Anonymous7-Oct-03 19:54
sussAnonymous7-Oct-03 19:54 
GeneralRe: ControlPaint class Pin
cthomas8-Oct-03 15:34
membercthomas8-Oct-03 15:34 
GeneralRe: ControlPaint class Pin
Anonymous26-Dec-03 2:57
sussAnonymous26-Dec-03 2:57 
GeneralRe: ControlPaint class Pin
sedatkurt26-Dec-03 3:21
membersedatkurt26-Dec-03 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 24 Jun 2003
Article Copyright 2003 by sedatkurt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid