Draw Line with custom Pen style

Posted on 2006-04-30
Last Modified: 2011-09-20
I want to draw a figure on a DC using LineTo or Polygon function with the help of a Pen, that the all lines will appeare as a solid line of width 10 with a border of different color.


Question by:jaydip786
    1 Comment
    LVL 5

    Accepted Solution


    A simple way of doing this would be to use the a pen of style solid  and one pixel width and use a brush to fill and use the polygon function. would also require to calculate tan inverse (atan) so would require to include "math.h".

    // assuming hDC is handle to device yopu already have
    HPEN hPen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0)); // border color
    HBRUSH hBrush = CreateSolidBrush (RGB(255, 0, 0)); // fill color

    HPEN hOldPen = (HPEN)SelectObject(hDC, hPen);
    HBRUSH hOldBrush = (HBRUSH)SelectObject(hDC, hBrush);

    // assuming x1, y1, x2 ,y2 being the start and end of lines

    long theta;
    long xoffset;
    long yoffset;

    if(x2 != x1)
          double theta = Math.atan((y2- y1)/ (x2 - x1));
          xoffset = 5 * Math.sin(theta);
          yoffset = 5 * Math.cos(theta);
          xoffset = 5;
          yoffset = 0;

    POINT pt[4];
    pt[0].x = x1 + xoffset;
    pt[0].y = y1 - yoffset;

    pt[1].x = x2 + xoffset;
    pt[1].y = y2 - yoffset;

    pt[2].x = x2 - xoffset;
    pt[2].y = y2 + yoffset;

    pt[3].x = x1 - xoffset;
    pt[3].y = y1 - yoffset;

    Polygon(hDC, pt, 4);

    SelectObject(hDC, hOldPen);
    SelectObject(hDC, hOldBrush);



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now