我是 winapi 和 c ++ 的新手,所以我试图尝试制作绘图项目。我用3个单选按钮创建了GUI来选择颜色,4个用于获取尺寸的文本字段和一个用于绘制任何对象的按钮(ex.Rectangle)。但是我无法找到一种在点击按钮后绘制的方法,只有当我把代码放到 &#34; WM_PAINT&#34; <时才能绘制。 /强>
案例WM_CREATE:
label1 = CreateWindow(L"STATIC", L"Title",
WS_VISIBLE | WS_CHILD ,
200, 10, 60, 20,
hwnd, NULL, NULL, NULL);
//X,Y,WIDHT,HIGHT
label2 = CreateWindow(L"STATIC", L"Select color",
WS_VISIBLE | WS_CHILD,
190, 40, 60, 20,
hwnd, NULL, NULL, NULL);
HWND radiobutton1, radiobutton2, radiobutton3;
radiobutton1 = CreateWindow(L"BUTTON", L"Red",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
50, 70, 100, 50,
hwnd, (HMENU)801,
GetModuleHandle(NULL), 0);
radiobutton2 = CreateWindow(L"BUTTON", L"Blue",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
150, 70, 100, 50,
hwnd, (HMENU)802,
GetModuleHandle(NULL), 0);
radiobutton3 = CreateWindow(L"BUTTON", L"Green",
WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON,
250, 70, 100, 50,
hwnd, (HMENU)803,
GetModuleHandle(NULL), 0);
textbox1 = CreateWindow(L"EDIT",
L"X0", WS_BORDER | WS_CHILD | WS_VISIBLE,
50, 120, 50, 20,
hwnd, NULL, NULL, NULL);
textbox2 = CreateWindow(L"EDIT",
L"Y0", WS_BORDER | WS_CHILD | WS_VISIBLE,
110, 120, 50, 20,
hwnd, NULL, NULL, NULL);
textbox3 = CreateWindow(L"EDIT",
L"X", WS_BORDER | WS_CHILD | WS_VISIBLE,
170, 120, 50, 20,
hwnd, NULL, NULL, NULL);
textbox4 = CreateWindow(L"EDIT",
L"Y", WS_BORDER | WS_CHILD | WS_VISIBLE,
230, 120, 50, 20,
hwnd, NULL, NULL, NULL);
CreateWindowExA(0, "Button", "Random text", WS_CHILD | WS_VISIBLE |
WS_CLIPSIBLINGS | BS_AUTOCHECKBOX | BS_TEXT | WS_GROUP |
WS_TABSTOP, 100, 300, 300, 48, hwnd, (HMENU)811, NULL, 0);
button = CreateWindow(L"Button",
L"Print", WS_VISIBLE | WS_CHILD | WS_BORDER,
150, 480, 100, 30, hwnd, (HMENU)111, NULL, NULL);
AddMenus(hwnd);
break;
我试着这样做:
案例WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hwnd, &ps);
if (wmId == 801)
{
if (wmEvent == BN_CLICKED)
{
LRESULT chkState = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0);
if (chkState == BST_CHECKED)
qEllipseColor = RGB(255, 0, 0);
}
break;
};
if (wmId == 802)
{
if (wmEvent == BN_CLICKED)
{
LRESULT chkState = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0);
if (chkState == BST_CHECKED)
qEllipseColor = RGB(0, 0, 255);
}
break;
};
if (wmId == 803)
{
if (wmEvent == BN_CLICKED)
{
LRESULT chkState = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0);
if (chkState == BST_CHECKED)
qEllipseColor = RGB(0, 255, 0);
}
break;
};
switch (LOWORD(wParam)) {
case 111:{
HPEN hEllipsePen;
hEllipsePen = CreatePen(PS_SOLID, 3, qEllipseColor);// qEllipseColor is global which gets its RGB value from radio buttons
hPenOld = (HPEN)SelectObject(hdc, hEllipsePen);
Arc(hdc, 100, 100, 500, 250, 0, 0, 0, 0);
SelectObject(hdc, hPenOld);
DeleteObject(hEllipsePen);
}break;