Dashed and dotted lines with a pen width greater than 1 logical unit

A Simple Dash-Line Class


With the Windows API, you can only create a dash or dot pen when the pen width is 1. This class permits you to work with virtual dash or dot pen with width bigger than 1.


You give the constructor the current dc, two different colors, the width of the pen and a line type array of four integers. The constructor creates two PS_SOLID pens and draws a line using the line type array.

 Type[4] = {20, 10, 10, 10};


Once you have created the CDashLine class, you can use the function MoveTo(x, y) and LineTo(x, y) to draw a line between two points. The MoveTo() function resets the internal type position and color index. The LineTo() function starts with the current type position and color. Do not forget to save the current pen and restore it after using CDashLine. Do not destroy the dc and use CDashLine after this!

Download source - 41 KB


  • thanks!!!

    Posted by mwwt on 12/01/2005 09:44pm

    very thank you!!I'm english very badly

  • changing the width of the pen on the run

    Posted by Legacy on 04/27/2001 12:00am

    Originally posted by: Naqvi

    Basically i dont want to create pen everytime i just want to change the width of the pen during the execution of the programe.

  • changing the width of the pen on the run

    Posted by Legacy on 04/27/2001 12:00am

    Originally posted by: Naqvi

    Basically i dont want to create pen everytime i just want to change the width of the pen during the execution of the programe.

  • I have some code to draw parallel lines

    Posted by Legacy on 04/18/2001 12:00am

    Originally posted by: Raven deathwalker

    Well i have a chunck of codes that can draw two parallel line and the starting points of both lines are always porpotion of each other by using trigronometri.. just drop me a mail and i will sent it to ya

  • Dash or Dot pen when the pen width is > 1.

    Posted by Legacy on 09/03/2000 12:00am

    Originally posted by: Shanmugam.D.

    It is possible to create dotted or dashed style lines using Windows API instead of writing a ur own class for the same.To do is
    // 1. Choose the other CPen constructors instead of default ones, so that u r able to specify geometric styles and give a brush (may contain a pattern).

    // 2. Do the normal line drawing code.

    // 3. Do the clean-up code.

    This will work for line, polyline and polygon also...
    In OnDraw() or OnPaint() or MyPaint()...
    int nPenWidth = 7;
    COLORREF crPenColor = RGB(0,0,0);
    int nPenStyle = PS_DOT;
    int nEndStyle = PS_ENDCAP_ROUND; // PS_ENDCAP_FLAT;

    CPoint pt[2];
    pt[0].x = 100; pt[0].y = 100;
    pt[1].x = 150; pt[1].y = 150;

    // STEP1: Create a LOGBRUSH and create a geometric pen.
    CBrush brush;
    LOGBRUSH logBrush;

    brush.CreateSolidBrush(crPenColor); //ur line color
    CPen pen(nPenStyle | PS_GEOMETRIC | nEndStyle, nPenWidth, &logBrush, 0, NULL);

    // STEP2: Select the geometric pen and starting drawing line.
    CPen *pOldPen = (CPen*) pDC->SelectObject(&pen);

    pDC->MoveTo(pt[0].x, pt[0].y);
    pDC->LineTo(pt[1].x, pt[1].y);

    // STEP3: Clean Up.

    PS: Instead of creating and deleting the GDI pen, it can be encapsulated inside a class or kept globally (depends on the requirement.)

  • Simple Rectangle & Polygon code!

    Posted by Legacy on 07/08/1999 12:00am

    Originally posted by: Paul Hoad

    Thought I'd contribute this not that its much

    void CDashLine::Rectangle(int x,int y,int x2,int y2)

    void CDashLine::Polygon(LPPOINT xp,int size)
    for (int i = 1; i < size; i++) {

    If anyone has similar for Arc I'd be interested

  • How does one delete a line that is drawn ??

    Posted by Legacy on 06/11/1999 12:00am

    Originally posted by: Radhika Chopra

    Hi Guys,
    Your sample is very useful.I have a specific requirement for
    my application.A user can draw a bunch of lines, and can
    also choose to delete a few of them .
    How do you delete a line ??

    Hope to get some help on this soon.

  • How about Polyline?

    Posted by Legacy on 03/16/1999 12:00am

    Originally posted by: Ray White

    I need a general purpose "wide line" package that can do LineTo and Polyline. Know of any?

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

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

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