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

A Simple Dash-Line Class


Introduction

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.

Method

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};

Functions

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



Comments

  • thanks!!!

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

    very thank you!!I'm english very badly

    Reply
  • changing the width of the pen on the run

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

    Originally posted by: Naqvi

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

    Reply
  • changing the width of the pen on the run

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

    Originally posted by: Naqvi

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

    Reply
  • 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

    Reply
  • 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
    brush.GetLogBrush(&logBrush);
    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.
    pDC->SelectObject(pOldPen);
    pen.DeleteObject();

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

    Reply
  • 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)
    {
    MoveTo(x,y);
    LineTo(x2,y);
    LineTo(x2,y2);
    LineTo(x,y2);
    LineTo(x,y);
    }

    void CDashLine::Polygon(LPPOINT xp,int size)
    {
    MoveTo(xp[0].x,xp[0].y);
    for (int i = 1; i < size; i++) {
    LineTo(xp[i].x,xp[i].y);
    }
    }

    If anyone has similar for Arc I'd be interested

    Reply
  • 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.

    Reply
  • 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?

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

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 29, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this eSeminar, Gene Kim will discuss these survey findings and will share woeful tales of artifact management gone wrong! Gene will also share examples of how …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds