[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 381
  • Last Modified:

Draw Line with custom Pen style

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.


1 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);



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now