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

标签 c++ windows winapi

我在 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/

相关文章:

c++ - 如何使用 CPP 程序将文本插入窗口?

c++ - 静态/动态运行时链接

windows - 如何以编程方式确定用户帐户是否是 Windows 中特定组的成员?

c - 如何等待线程启动?

c++ - 特定功能的访问冲突

c++ - 我可以编写什么程序来卡住/挂起我的程序来测试看门狗定时器?

C++ printf 带 %f 但已针对用户所在国家/地区进行了本地化

c++ - 比较没有 RTTI 的抽象类?

c++ - 井字游戏 : Winapi painting

internet-explorer - IWebBrowser2 Ctrl+C等快捷键支持