c - 笔连接样式未应用于形状的所有角

标签 c winapi gdi

Line joins

形状是用 Polyline() 函数绘制的。 相关代码在这里:

void DoDrawing(HWND hwnd) {

    LOGBRUSH brush;
    COLORREF col = RGB(0, 0, 0);
    DWORD pen_style = PS_SOLID | PS_JOIN_MITER | PS_GEOMETRIC;

    brush.lbStyle = BS_SOLID;
    brush.lbColor = col;
    brush.lbHatch = 0;       

    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(hwnd, &ps);

    HPEN hPen1 = ExtCreatePen(pen_style, 8, &brush, 0, NULL);
    HPEN holdPen = SelectObject(hdc, hPen1);

    POINT points[5] = { { 10, 30 }, { 100, 30 }, { 100, 100 }, { 10, 100 }, {10, 30}};
    Polyline(hdc, points, 5);
    DeleteObject(hPen1);

    SelectObject(hdc, holdPen);

    EndPaint(hwnd, &ps);  
}

PS_JOIN_MITER 应用于三个角,但不应用于左上角。在那个角落使用默认的 PS_JOIN_ROUND。如何解决这个问题?

下面是一个完整的工作示例:

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void DoDrawing(HWND);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    PWSTR lpCmdLine, int nCmdShow) {

    MSG  msg;
    WNDCLASSW wc = {0};

    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpszClassName = L"Pens";
    wc.hInstance     = hInstance;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpfnWndProc   = WndProc;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);

    RegisterClassW(&wc);
    CreateWindowW(wc.lpszClassName, L"Line joins",
          WS_OVERLAPPEDWINDOW | WS_VISIBLE,
          100, 100, 250, 180, NULL, NULL, hInstance, NULL);

    while (GetMessage(&msg, NULL, 0, 0)) {

        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

  return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    switch(msg) {

        case WM_PAINT:

            DoDrawing(hwnd);
            break;

        case WM_DESTROY:

            PostQuitMessage(0);
            return 0;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

void DoDrawing(HWND hwnd) {

    LOGBRUSH brush;
    COLORREF col = RGB(0, 0, 0);
    DWORD pen_style = PS_SOLID | PS_JOIN_MITER | PS_GEOMETRIC;

    brush.lbStyle = BS_SOLID;
    brush.lbColor = col;
    brush.lbHatch = 0;       

    PAINTSTRUCT ps;

    HDC hdc = BeginPaint(hwnd, &ps);

    HPEN hPen1 = ExtCreatePen(pen_style, 8, &brush, 0, NULL);
    HPEN holdPen = SelectObject(hdc, hPen1);

    POINT points[5] = { { 10, 30 }, { 100, 30 }, { 100, 100 }, { 10, 100 }, {10, 30}};
    Polyline(hdc, points, 5);
    DeleteObject(hPen1);

    SelectObject(hdc, holdPen);

    EndPaint(hwnd, &ps);  
}

最佳答案

Polyline 不连接第一个点和最后一个点。

使用 Polygon(hdc, points, 5) 而不是 Polyline

此外,在删除现有笔之前,选择 oldPen 到 DC,顺序如下:

SelectObject(hdc, holdPen);
DeleteObject(hPen1);

(如果您没有按正确的顺序执行,Windows 会原谅您)

关于c - 笔连接样式未应用于形状的所有角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36015492/

相关文章:

c++ - 在 C++ (Win32) 中快速获取 Windows 应用程序屏幕截图的方法?

c - JPEG 通过 Grid32 在 C 中通过 Win32 API 加载/保存到/从 rgba

c# - 自定义控件边框不显示是否调用 Update(),但显示是否调用 Refresh()

c - 处理部分 : does a declaration add also something to . 文本?如果是,它添加了什么?

C函数使用指针计算错误

Char 和 Int 打印两个不同的值

c++ - 编辑 EDIT 导致程序崩溃

android - 使用android studio在子进程中 fork 后无法打印日志或命中断点

winapi - 在进程间使用事件对象

c - PE文件的内存映射IAT中u1.AddressOfData开头的2个字节是什么?