Solved

# Draw Line with custom Pen style

Posted on 2006-04-30
368 Views
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.

__________________________________
__________________________________

0
Question by:jaydip786
1 Comment

LVL 5

Accepted Solution

Hi,

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);
}
else
{
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);

DeleteObject(hPen);
DeleteObject(hBrush);

-wings
0

## Featured Post

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.