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


How to Help Your Business Become an AI Early Adopter

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

  • IDC estimates that more than 70% of organizations have a cloud-first application deployment strategy to align with digital transformation initiatives. As such, many organizations are transitioning to Microsoft Office 365 and utilizing public clouds such as Microsoft Azure. However, IT leaders are discovering that default data protection schemes are not as robust as they desire. As a result, many are turning to Veeam® to provide the necessary data management and protection policies demanded by the …

  • Cloud has the potential to offer many benefits that can enable great success within your business. However, there are still many myths floating around about backing up to the cloud. In this eBook, you'll discover the truth about five of the most common cloud myths, including myths about security, maintaining regulatory compliance and more. Get to the truth, so you can backup to the cloud with confidence.

Most Popular Programming Stories

More for Developers

RSS Feeds

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