WINAPI:静态对话框项目的彩色边框

时间:2015-02-26 09:28:45

标签: 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;             

在对话框proc中,我使用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

我可以做的一件事就是画一个带有深红色的空白静态,然后是另一个带有白色的静态元素。但这不是一个很好的解决方案。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

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

首先,您需要告诉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);
}