windows 禁用的SysLink控件显示为“启用”外观

kpbwa7wx  于 2023-03-24  发布在  Windows
关注(0)|答案(1)|浏览(139)

显然,SysLink controls不能显示禁用。
我有一个包含以下控件的对话框(在此示例中坐标不准确):

CONTROL   "Foo",IDC_CHECK8 "Button",BS_AUTOCHECKBOX | WS_GROUP | WS_TABSTOP,
           12,192,256,28
 CONTROL   "<a href=""https://www.somewhere.com"">Bar</a>",IDC_STATIC4,
           "SysLink",0x0,22,219,144,9

当我使用EnableWindow()禁用IDC_CHECK8(复选框)和IDC_STATIC4(SysLink控件)时,只有复选框显示为“禁用”外观,SysLink控件正常显示,就像它没有被禁用一样:

问题不在我的代码中,因为SysLink * 实际上 * 被禁用(您无法单击它,这是预期的)。
是否有一种简单的方法来显示禁用的SysLink控件,有点像这样:

sigwle7e

sigwle7e1#

你可以定义一个全局标志来控制颜色的变化。你可以使用LM_SETITEM来设置Syslink,使用LIS_DEFAULTCOLORS来允许Syslink使用自定义颜色。然后你可以在WM_CTLCOLORSTATIC中使用SetTextColor来改变文本的颜色,这些都可以达到你想要的效果。

BOOL m_bSyslinkEnableFlag = TRUE;
case WM_INITDIALOG:
{
    HWND syslinkhwnd = GetDlgItem(hDlg, IDC_SYSLINK2);
    tagLITEM* pItem = new tagLITEM;
    pItem->iLink = 0;
    pItem->mask = LIF_ITEMINDEX | LIF_STATE;
    pItem->state = LIS_ENABLED | LIS_FOCUSED | LIS_DEFAULTCOLORS;
    pItem->stateMask = LIS_ENABLED | LIS_FOCUSED | LIS_DEFAULTCOLORS;
    //LIS_DEFAULTCOLORS can use custom color
    SendMessage(syslinkhwnd, LM_SETITEM, NULL, (LPARAM)pItem);

    return (INT_PTR)TRUE;
}
case WM_CTLCOLORSTATIC:
{
    HWND syslinkhwnd = GetDlgItem(hDlg, IDC_SYSLINK2);
    if (LOWORD(lParam) == LOWORD(syslinkhwnd) && (m_bSyslinkEnableFlag == TRUE))
    {
        //Set the text color blue
        SetTextColor((HDC)wParam, RGB(0, 35, 245));
    }
    else if (LOWORD(lParam) == LOWORD(syslinkhwnd) && (m_bSyslinkEnableFlag == FALSE))
    {
        //Set the text color black when disable
        SetTextColor((HDC)wParam, RGB(0, 0, 0));
    }
    SetBkMode(HDC(wParam), TRANSPARENT);

    //return default background color
    return (INT_PTR)(HBRUSH)CreateSolidBrush(RGB(240, 240, 240));
}

case WM_COMMAND:
if (LOWORD(wParam) == IDC_BUTTON1)
{
    HWND syslinkhwnd = GetDlgItem(hDlg, IDC_SYSLINK2);

    EnableWindow(syslinkhwnd, TRUE);
    //set flag,the clor will change in WM_CTLCOLORSTATIC
    m_bSyslinkEnableFlag = TRUE;
    //redraw control and raise WM_CTLCOLORSTATIC
    RedrawWindow(syslinkhwnd, NULL, NULL, RDW_INVALIDATE);

    return (INT_PTR)TRUE;
}

if (LOWORD(wParam) == IDC_BUTTON2)
{
    HWND syslinkhwnd = GetDlgItem(hDlg, IDC_SYSLINK2);

    EnableWindow(syslinkhwnd, FALSE);
    //set flag,the clor will change in WM_CTLCOLORSTATIC
    m_bSyslinkEnableFlag = FALSE;
    //redraw control and raise WM_CTLCOLORSTATIC
    RedrawWindow(syslinkhwnd, NULL, NULL, RDW_INVALIDATE);

    return (INT_PTR)TRUE;
}

相关问题