0

我想在我的窗口中画一条白线:

    case WM_PAINT:
    {
        hdc=GetDC(hWnd);
        SelectObject(hdc, GetStockObject(WHITE_BRUSH)); 
        MoveToEx(hdc, 0, 0, 0);
        LineTo(hdc, 100, 100);
        ReleaseDC(hWnd, hdc);
    }

但颜色仍然是黑色。怎么了?

4

1 回答 1

3

You are trying to set a brush for your line when you should be using a pen. A brush is used to fill the interior of a shape while a pen is used to draw the lines.

MSDN says this about pens:

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.

And this about brushes:

A brush is a graphics tool that applications use to paint the interior of polygons, ellipses, and paths.

Your code would need to be something more like this:

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
}
于 2012-06-22T16:32:40.427 回答