Drawing dotted lines
Posted
by Jean-Edouard Lachand-Robert
on December 6th, 1998
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:00amOriginally posted by: luyaz
how to draw a grid and place a bitmap in any cell of the grid.
ReplyWhy not use CPen
Posted by Legacy on 12/10/2002 12:00amOriginally posted by: graber
There is already a CPen Class that can create dashed lines. Why not use that.
ReplyHow to draw moving vertical lines?
Posted by Legacy on 03/01/2000 12:00amOriginally posted by: Jeff stryker
Hi
any one know how to draw moving vertical lines?
many thanks!
ReplyDraw --->
Posted by Legacy on 05/05/1999 12:00amOriginally posted by: Kapil
How to draw a arrowed line ??
regards,
ReplyKapil
--o--o--o--o--o--o line type
Posted by Legacy on 02/26/1999 12:00amOriginally posted by: Tonio
Reply