使用C / C ++和WinApi精确绘制1像素宽度线

时间:2016-03-04 00:25:17

标签: winapi draw gdi

我正在尝试使用C / C ++代码和Win API函数绘制线条。由于特定的原因,我需要有1个像素的线宽,但我在屏幕上得到的是完全不同的。根据笔/画笔设置的方式,它可以是2或3像素宽的线,而不是1。 仅仅为了测试,我试图绘制3像素线,结果得到5像素。 与其他类型的绘图基元相同的故事:圆角矩形,矩形,椭圆等。 有没有办法在绘制的线条周围禁用这种阴影边界? 绘图功能的代码示例如下。

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)
    {
     return;
    }
 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)
    {
     return;
    }
 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)
    {
     return;
    }
 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)
    {
     return;
    }
 hPen                   = CreatePen (PS_SOLID,0, RGB(255, 0,0));
 hOldPen                    = SelectObject (hDC, hPen);
 if (hOldPen == NULL)
    {
     return;
    }
 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)
    {
     return;
    }
 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)
    {
     return;
    }
 hPen                   = CreatePen (PS_SOLID,3, RGB(255, 0,0));
 hOldPen                    = SelectObject (hDC, hPen);
 if (hOldPen == NULL)
    {
     return;
    }
 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)
    {
     return;
    }
 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)
    {
     return;
    }
 MoveToEx (hDC, iLeftPos,iLineY,NULL);
 LineTo (hDC,iRightPos,iLineY);
 SelectObject (hDC, hOldPen);
 DeleteObject (hPen);
 DeleteObject (hBitmap);
 // Result - line with 2 pixels width

}

提前致谢

1 个答案:

答案 0 :(得分:0)

tl;博士:DPI意识。

Windows会为高DPI显示器缩放图形,除非您明确告诉它您的程序了解如何使用实际的DPI。

您可以在应用程序的早期使用清单或系统调用执行此操作。 API的原始版本是SetProcessDPIAware。较新的版本是SetProcessDPIAwareness,它可以让你告诉Windows,即使在监视器具有不同DPI的多监视器系统上你也知道你在做什么。

相关问题