0

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • You should be able to use custom draw to change the text color; so when handling `CDDS_ITEMPREPAINT`, you could set your text color to `GetSysColor(COLOR_GRAYTEXT)`. – Jonathan Potter Mar 17 '23 at 20:17
  • 1
    IIRC you can enable and disable individual links in the control. Did you try sending a `LM_SETITEM` message to the control, clearing the `LIS_ENABLED` state on the relevant link(s)? – TheNextman Mar 19 '23 at 02:26

1 Answers1

2

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;
}
Roy Li - MSFT
  • 8,043
  • 1
  • 7
  • 13