What are Command Links?
Among other new controls supported in Windows Vista and newer Windows versions, we can find Command Link controls, that contain:
- an icon;
- a label text;
- an additional explanatory text.
In fact, it is a common Windows button control (of class “Button“) that has BS_COMMANDLINK or BS_DEFCOMMANDLINK type style set.
We’ll further show how to deal with such type of controls in our own aplications.
How to add a Command Link Button
If working with Microsoft Visual Studio 2008 or newer, then we can find “Command Button Control” in the resource editor toolbox.
So first, we just have to drag it from toolbox in the resource dialog template.
The label text can be set at design time by changing the Caption property; it can be also changed at run-time by calling CWnd::SetWindowText.
The additional explanatory text has to be set at run-time by calling CButton::SetNote.
The default arrow icon can be changed at run-time using CButton::SetIcon.
Here is an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
BOOL CDemoDlg::OnInitDialog() { CDialog::OnInitDialog(); CButton* pButton = (CButton*)GetDlgItem(IDC_COMMAND_LOGO); ASSERT_VALID(pButton); // Asure it's a Command Link Button ASSERT((pButton->GetStyle() & BS_TYPEMASK) == BS_COMMANDLINK); // Set label text pButton->SetWindowTextW(L"Visit CODEXPERT.RO"); // Set additional explanatory text pButton->SetNote(L"A Romanian Visual C++ on-line community"); // Change the default icon VERIFY(m_hLogoIcon = AfxGetApp()->LoadIcon(IDI_LOGO)); pButton->SetIcon(m_hLogoIcon); // ... return TRUE; // return TRUE unless you set the focus to a control } |
Further, we can work like with any other classic push button, as for example handling BN_CLICKED notification.
Additional notes
- Command Link Button controls are available in Windows Vista and newer.
- For for additional environment info, see “Build Requirements for Windows Vista Common Controls” link below.
- In raw-WinAPI applications (not using MFC), can use the corresponding Windows SDK functions/macros instead of of CWnd/CButton methods.
Example:
123456789101112HWND hWndButton = ::GetDlgItem(hWndDlg, IDC_COMMAND_LOGO);// Assure it's a Command Link ButtonASSERT((::GetWindowLongPtr(hWndButton, GWL_STYLE) & BS_TYPEMASK) == BS_COMMANDLINK);// Set label text::SetWindowTextW(hWndButton, L"Visit CODEXPERT.RO");// Set additional explanatory text (send BCM_SETNOTE message or call Button_SetNote macro).::SendMessage(hWndButton, BCM_SETNOTE, 0, (LPARAM)L"A Romanian Visual C++ on-line community");// Button_SetNote(hWndButton, L"A Romanian Visual C++ on-line community");// Change the default iconHINSTANCE hInstance = ::GetModuleHandleW(NULL);HICON hIcon = ::LoadIconW(hInstance, MAKEINTRESOURCEW(IDI_LOGO));::SendMessageW(hWndButton, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)hIcon); - Command Button Control is available in the resource editor, beginning with Visual Studio 2008. However, you can still use this kind of control in older Visual Studio versions. For this purpose you can:
- Add to dialog template a regular push button then manually edit the resource script (.rc) file or, a little bit better…
- …add to dialog template a Custom Control.
As shown in the image, you must set “BUTTON” class property and “0x5001000e” style property (WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_COMMANDLINK).
Also, in the source code you have to send BCM_SETNOTE message instead of calling CButton::SetNote. For more details, see the attached VS 2005 demo project.
References
- MSDN: CButton Class
- MSDN: Command Links
- MSDN: Build Requirements for Windows Vista Common Controls
- Windows Dev Center: How to Create a Command Link
Downloads
- Visual Studio 2008 – Demo Project (970)
- Visual Studio 2005 – Demo Project (816)