我想在我的窗口中画一条白线:
case WM_PAINT:
{
hdc=GetDC(hWnd);
SelectObject(hdc, GetStockObject(WHITE_BRUSH));
MoveToEx(hdc, 0, 0, 0);
LineTo(hdc, 100, 100);
ReleaseDC(hWnd, hdc);
}
但颜色仍然是黑色。怎么了?
最佳答案
当您应该使用钢笔时,您正试图为您的线条设置画笔。画笔用于填充形状的内部,而钢笔用于绘制线条。
MSDN says关于笔:
A pen is a graphics tool that an application can use to draw lines and curves. Drawing applications use pens to draw freehand lines, straight lines, and curves.
和this关于画笔:
A brush is a graphics tool that applications use to paint the interior of polygons, ellipses, and paths.
您的代码需要更像这样:
case WM_PAINT:
{
PAINTSTRUCT ps;
hdc=BeginPaint(hWnd, &ps); // Used instead of GetDC in WM_PAINT
HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
HPEN hOldPen = SelectObject(hdc, hPen);
MoveToEx(hdc, 0, 0, 0);
LineTo(hdc, 100, 100);
SelectObject(hdc, hOldPen);
DeleteObject(hPen);
EndPaint(hWnd, &ps); // Used instead of ReleaseDC in WM_PAINT
}
关于线条的颜色没有变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11159233/