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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Agile development principles have gone from something used only by cutting-edge teams to a mainstream approach used by teams large and small. If you're not using agile methods already though, or if you've only been exposed to agile on small projects here and there, you may wonder if agile can ever work in your environment. Read this eBook to learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds