gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:42 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com