Disabled SysLink controls appear with "enabled" look

96 views Asked by At

Apparently SysLink controls cannot be displayed disabled.

I have a dialog containing the following controls (coordinates are not accurate in this sample):

 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

When I disable both controls IDC_CHECK8 (checkbox) and IDC_STATIC4 (SysLink control) using EnableWindow(), only the checkbox is displayed with the "disabled" look, the SysLink control is displayed normally as if it were not disabled:

image

The problem is not in my code because the SysLink is actually disabled (you cannot click on it, which is expected).

Is there a simple way to display the SysLink control disabled, somewhat like this:

image

1

There are 1 answers

0
Roy Li - MSFT On

You can define a global flag to control the color changes. you can use LM_SETITEM to set Syslink and use LIS_DEFAULTCOLORS to allow Syslinks to use custom colors. Then you can change the text color with SetTextColor in WM_CTLCOLORSTATIC, these can achieve the desired effect.  

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;
}