线条的颜色没有变化?

标签 c windows winapi graphics

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

    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/

相关文章:

c - 返回的字符串与 c 函数中的字符串不同

c - 使用数组和 malloc 完成的基本平方函数

c - 在 C 中运行时解析 float 的标准方法是什么?

c++ - 如何将 Win32 GUI 应用程序作为后台进程启动?

c++ - 在 C++ 中进行 native win32 gui 编程还是选择 wxWidgets?

结构体的C动态 vector

Windows 7 上的 Cygwin Control+D 不给出 EOF 信号

windows - 运行带有基座的 Composer 更新会返回内存不足

javascript - Facebook 与 javascript FB Graph api 集成 在墙上发布 Firefox 中的未知错误

c++ - LONG_PTR 的迭代器