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

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • Protect Your Business against Today's Rapidly Growing Threats Your business, employees, partners, and customers are depending more and more on network connectivity, and protecting data and infrastructure has moved to the top of the IT agenda. You can't arm yourself against today's rapidly increasing threats unless you understand how they work. Read this ebook to learn more about the top 10 DNS attacks that can target your external and internal DNS infrastructure, the impact they can have on the DNS server …

Most Popular Programming Stories

More for Developers

RSS Feeds

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