Drawing dotted lines

3 steps to show how to draw vertical and horizontal dotted lines(1 pixel on 2) using GDI calls. This code works under all Win32 platforms.

Step 1 (initialization)

A patterned brush is created during program (or view) initialization. This brush will be used to draw the dotted lines :
{
	...

	// Create a dotted monochrome bitmap
	WORD b[8] = { 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA, 0x5555, 0xAAAA,
	0x5555 };
	BITMAP bm;
	bm.bmType = 0;
	bm.bmWidth = 16;
	bm.bmHeight = 8;
	bm.bmWidthBytes = 2;
	bm.bmPlanes = 1;
	bm.bmBitsPixel = 1;
	bm.bmBits = b;
	HBITMAP hbm = CreateBitmapIndirect(&bm);

	// Create the brush from the bitmap bits
	HBRUSH hPatternBrush = CreatePatternBrush(hbm);

	// Delete the useless bitmap
	DeleteObject(hbm);

	...
}

Step 2 (drawing)

Dotted lines are drawn using PatBlt() :
{
	...

	// Select the patterned brush into the DC
	HBRUSH oldBrush = (HBRUSH)SelectObject(hDC, hPatternBrush);

	// Draw an horizontal line
	PatBlt(hDC, 10, 10, 300, 1, PATCOPY);

	// Invert a vertical line 2 pixels wide
	PatBlt(hDC, 10, 10, 2, 300, PATINVERT);

	// Clean up
	SelectObject(hDC, oldBrush);

	...
}

If drawing occurs in a "scrollable view", don't forget to align the brush origin into the DC BEFORE to select the brush :

{
	...

	// We are in a CScrollView, the patterned brush must be aligned
	UnrealizeObject(hPatternBrush);
	CPoint sp = GetDeviceScrollPosition();
	SetBrushOrgEx(hDC, -sp.x & 7, -sp.y & 7, NULL);

	// Select the patterned brush into the DC
	HBRUSH oldBrush = (HBRUSH)SelectObject(hDC, hPatternBrush);

	...
}

Step 3 (destruction)

When the patterned brush is useless, it must be destroyed :
{
	...

	// Delete the patterned brush
	DeleteObject(hPatternBrush);

	...
}



Comments

  • drawing a grid dand placing bitmaps in any cell

    Posted by Legacy on 11/12/2003 12:00am

    Originally posted by: luyaz

    how to draw a grid and place a bitmap in any cell of the grid.

    Reply
  • Why not use CPen

    Posted by Legacy on 12/10/2002 12:00am

    Originally posted by: graber

    There is already a CPen Class that can create dashed lines. Why not use that.

    Reply
  • How to draw moving vertical lines?

    Posted by Legacy on 03/01/2000 12:00am

    Originally posted by: Jeff stryker

    Hi

    any one know how to draw moving vertical lines?

    many thanks!

    Reply
  • Draw --->

    Posted by Legacy on 05/05/1999 12:00am

    Originally posted by: Kapil

    How to draw a arrowed line ??

    regards,
    Kapil

    Reply
  • --o--o--o--o--o--o line type

    Posted by Legacy on 02/26/1999 12:00am

    Originally posted by: Tonio

    Hi,
    
    Is it possible to create my own line type or pattern
    like --o--o--o--o--o--o-- ?
    How and where should I start doing this?
    Thanks.

    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 …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds