I am trying to draw the line using C/C++ code and Win API functions. For the specific reason I need to have exactly 1 pixel line width, but what I am getting on the screen is completely different. Depending on the ways of Pen/Brush settings it could be either 2 or 3 pixels wide line and never 1. Just for the test I’ve tried to draw 3 pixels line and as a result getting 5 pixels. The same story with other kind of drawing primitives: rounded rectangles, rectangles, ellipses etc. Is there any way to disable such shadow bounding around drawn lines? Code sample of drawing function is below.
void DisplayDrawings (HDC hDC, HWND hWnd)
{
LOGBRUSH stLogBrush;
RECT rRect = {0,0,0,0};
HBITMAP hBitmap = NULL;
HBRUSH hBrush = NULL;
HGDIOBJ hOldBrush = NULL;
HGDIOBJ hOldPen = NULL;
HPEN hPen = NULL;
BOOL bB_Result = FALSE;
UINT bBrushByBits [8];
int iRightPos = 0;
int iLeftPos = 30;
int iLineY = 30;
int iDrawStep = 10;
int i = 0;
#ifndef RGBA
#define RGBA(r,g,b,a) ((COLORREF)( (((DWORD)(BYTE)(a))<<24) | RGB(r,g,b) ))
#endif
SetBkMode (hDC, OPAQUE);
bB_Result = GetClientRect(hWnd, &rRect);
if (bB_Result == FALSE)
{
retu;
}
iRightPos = rRect.right - 30 - 1;
// Line 1. Draw 1 pixel Line using standard brush/pen.
hPen = CreatePen (PS_SOLID,1, RGB(255, 0,0));
hOldPen = SelectObject (hDC, hPen);
if (hOldPen == NULL)
{
retu;
}
MoveToEx (hDC, iLeftPos,iLineY,NULL);
LineTo (hDC,iRightPos,iLineY);
SelectObject (hDC, hOldPen);
DeleteObject (hPen);
iLineY += iDrawStep;
// Result - line with 2 pixels width
// Line 2. Draw 1 pixel Line using standard brush/pen, but setting pen width to "0". CreatePen API says: "If nWidth is zero, the pen is a single pixel wide, regardless of the current transformation"
hPen = CreatePen (PS_SOLID,0, RGB(255, 0,0));
hOldPen = SelectObject (hDC, hPen);
if (hOldPen == NULL)
{
retu;
}
MoveToEx (hDC, iLeftPos,iLineY,NULL);
LineTo (hDC,iRightPos,iLineY);
SelectObject (hDC, hOldPen);
DeleteObject (hPen);
iLineY += iDrawStep;
// Result - line with 3 pixels width
// Line 3. Draw 1 pixel as above, but set current brush to NULL
hOldBrush = SelectObject (hDC, GetStockObject(NULL_BRUSH));
if (hOldBrush == NULL)
{
retu;
}
hPen = CreatePen (PS_SOLID,0, RGB(255, 0,0));
hOldPen = SelectObject (hDC, hPen);
if (hOldPen == NULL)
{
retu;
}
MoveToEx (hDC, iLeftPos,iLineY,NULL);
LineTo (hDC,iRightPos,iLineY);
SelectObject (hDC, hOldPen);
DeleteObject (hPen);
SelectObject (hDC, hOldBrush);
iLineY += iDrawStep;
// Result - line with 2 pixels width
// Line 4. Draw 3 pixel Line using standard brush/pen
hPen = CreatePen (PS_SOLID,3, RGB(255, 0,0));
hOldPen = SelectObject (hDC, hPen);
if (hOldPen == NULL)
{
retu;
}
MoveToEx (hDC, iLeftPos,iLineY,NULL);
LineTo (hDC,iRightPos,iLineY);
SelectObject (hDC, hOldPen);
DeleteObject (hPen);
iLineY += iDrawStep;
// Result - line with 5 pixels width
// Line 5. Draw 3 pixel as above, but set current brush to NULL
hOldBrush = SelectObject (hDC, GetStockObject(NULL_BRUSH));
if (hOldBrush == NULL)
{
retu;
}
hPen = CreatePen (PS_SOLID,3, RGB(255, 0,0));
hOldPen = SelectObject (hDC, hPen);
if (hOldPen == NULL)
{
retu;
}
MoveToEx (hDC, iLeftPos,iLineY,NULL);
LineTo (hDC,iRightPos,iLineY);
SelectObject (hDC, hOldPen);
DeleteObject (hPen);
SelectObject (hDC, hOldBrush);
iLineY += iDrawStep;
// Result - line with 5 pixels width
// Line 6. Draw 1 pixel line by creating own pen with monochrome 8x8 brush. Result is 3
int k = 0;
for(i = 0; i < 8; i++)
{
bBrushByBits [i] = 0x00;
}
hBitmap = CreateBitmap(8, 8, 1, 1, (LPBYTE)bBrushByBits);
if (hBitmap == NULL)
{
retu;
}
stLogBrush.lbColor = RGBA(255,0,0,0);
stLogBrush.lbHatch = (ULONG_PTR) hBitmap;
stLogBrush.lbStyle = BS_PATTERN;
hPen = ExtCreatePen (PS_GEOMETRIC, 3, &stLogBrush, 0, NULL);
hOldPen = SelectObject (hDC, hPen);
if (hOldPen == NULL)
{
retu;
}
MoveToEx (hDC, iLeftPos,iLineY,NULL);
LineTo (hDC,iRightPos,iLineY);
SelectObject (hDC, hOldPen);
DeleteObject (hPen);
DeleteObject (hBitmap);
// Result - line with 2 pixels width
}
Thanks in advance
