gpt4 book ai didi

c++ - WINAPI:静态对话框项的彩色边框

转载 作者:可可西里 更新时间:2023-11-01 10:05:35 30 4
gpt4 key购买 nike

我在 win32 api 中有一个对话框。我有一个创建如下的静态元素。

lpw = lpwAlign(lpw);     // Align DLGITEMTEMPLATE on DWORD boundary
lpdit = (LPDLGITEMTEMPLATE)lpw;
lpdit->x = 12;
lpdit->y = 36;
lpdit->cx = 75;
lpdit->cy = 7;
lpdit->id = ID_ERROR_MSG;
lpdit->style = WS_CHILD | SS_BITMAP | WS_VISIBLE ;

lpw = (LPWORD)(lpdit + 1);
*lpw++ = 0xFFFF;
*lpw++ = 0x0082; // Static class

lpwsz = (LPWSTR)lpw;
nchar = MultiByteToWideChar(CP_ACP, 0, errorMsg.c_str(), -1, lpwsz, 50);
lpw += nchar;
*lpw++ = 0;

在对话框过程中,我使用 WM_PAINT 和以下代码处理它。

HWND ErrorMessage = GetDlgItem(hwndDlg, ID_ERROR_MSG);

hdc = BeginPaint(ErrorMessage, &ps_Confirm);
hFont = CreateFont(18,0,0,0,FW_BOLD,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,
CLIP_DEFAULT_PRECIS,CLEARTYPE_QUALITY, VARIABLE_PITCH,TEXT("Arial"));
SelectObject(hdc, hFont);

wchar_t sCaption[100];
GetWindowText(ErrorMessage,sCaption, 100);

//Sets the coordinates for the rectangle in which the text is to be formatted.
SetRect(&rect, 0,0,75,7);
SetTextColor(hdc, RGB(0,0,0));
SetBkColor(hdc, RGB(255,255,255));
//SetBkMode(hdc, TRANSPARENT);
DrawText(hdc, sCaption, -1,&rect, DT_NOCLIP);
DeleteObject(hFont);
EndPaint(ErrorMessage, &ps_Confirm);

我想在它周围画上红色边框。我尝试使用 DrawEdge() 但边缘仅为黑色。另外,它有点3D效果。我想做类似的事情:

red border

我可以做的一个技巧是绘制一个深红色的空白静态元素,然后再绘制一个白色的静态元素。但这不是一个很好的解决方案。

有人可以帮忙吗?

最佳答案

我要解决这个问题的方法是子类化 静态控件。这样做意味着您只需专注于实现您想要的不同行为,而不是控件的整个行为/逻辑。

首先,您需要告诉 Windows 您正在执行此操作。接下来需要为窗口实现一个WndProc(实际上是一个SUBCLASSPROC)函数。最后,您需要进行绘画。

enter image description here

这是一个粗略的例子:

#define _WIN32_WINNT 0x0501  // min version for subclassing funcs
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include "resource.h"

HINSTANCE hInst;

const int errorPanelSubclassId = 1;

LRESULT paintErrorPanel(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);

RECT mRect;
GetClientRect(hWnd, &mRect);

HBRUSH redBrush = CreateSolidBrush( RGB(255,0,0) );
FrameRect(hdc, &mRect, redBrush);
DeleteObject(redBrush);

EndPaint(hWnd, &ps);
return 0;
}

LRESULT CALLBACK subclassedWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_PAINT)
{
paintErrorPanel(hWnd, wParam, lParam);
return 0;
}

else
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}


BOOL CALLBACK DlgMain(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
{
HWND errorPanel = GetDlgItem(hwndDlg, IDC_ERROR_STATIC);
SetWindowSubclass(errorPanel, subclassedWinProc, errorPanelSubclassId, NULL);
}
return TRUE;

case WM_CLOSE:
{
EndDialog(hwndDlg, 0);
}
return TRUE;

case WM_DESTROY:
{
RemoveWindowSubclass(errorPanel, subclassedWinProc, errorPanelSubclassId);
}
return 0;

case WM_COMMAND:
{
switch(LOWORD(wParam))
{
}
}
return TRUE;
}
return FALSE;
}



int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst=hInstance;
InitCommonControls();
return DialogBox(hInst, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DlgMain);
}

关于c++ - WINAPI:静态对话框项的彩色边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28738658/

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