I created a custom button class CMyButton inherited from CButton, then I am using the DrawItem to customize the button.
Using the custtom button, I created 3button on dialog.
The issue is that the TAB key for the button is not working. If I remove drawitem then there is no issue. Can anyone please help on this?
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
pDC->DrawFrameControl(rect, DFC_BUTTON , DFCS_BUTTONPUSH | DFCS );
pDC->FillSolidRect(rect, RGB(24, 72, 76));
pDC->SetTextColor(RGB(255, 255, 255));
CString strText;
GetWindowText(strText);
pDC->DrawText(strText,rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
I doubt it. If you make a button owner draw, then you are responsible for drawing it all the time. This includes the focused state, etc. See the documentation for DRAWITEMSTRUCT.itemstate.
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775802%28v=vs.85%29.aspx
The focus rectangle is automatically rendered for standard controls on a dialog. If you subscribe to owner-drawing, responsibility to render visual cues is shifted to the custom implementation. While TABbing still works, keyboard focus remains invisible unless the implementation explicitly accounts for it.
You can use the DRAWITEMSTRUCT passed to your DrawItem method to query the item's state. If itemState contains the ODS_FOCUS flag, the control being rendered has the keyboard focus, and should produce the desired visual cue.
As a simple illustration, replace your call to FillSolidRect with the following code:
if ( state & ODS_FOCUS )
// Control has keyboard focus -> render it green
pDC->FillSolidRect( rect, RGB( 0, 255, 0 ) );
else
// Control doesn't have keyboard focus -> render it red
pDC->FillSolidRect( rect, RGB( 255, 0, 0 ) );
This allows you to see, that the TAB key does work as expected: The button control with keyboard focus is rendered green, whereas all other buttons are red.
If you are looking for a more standard appearance you can call CDC::DrawFocusRect (or DrawFocusRect) instead.
Related
I have created an Owner-Drawn button . I use the WM_CTLCOLORBTN message in order to paint it :
//get the text of the button
wchar_t buttonText[20];
int textLength = SendMessage((HWND)lParam,WM_GETTEXT,20,(LPARAM)buttonText);
Font FootlightMTLight(L"Footlight MT Light",20,0,false,false,false,L"Black");
SelectObject((HDC)wParam,FootlightMTLight.getWindowHandle());
TextOut((HDC)wParam,30,15,buttonText,textLength);
SetTextColor((HDC)wParam, RGB(0,0,0));
SetBkColor((HDC)wParam, RGB(229,255,229));
PatBlt((HDC)wParam,0,0,1,50,BLACKNESS); //x,y,width,height
PatBlt((HDC)wParam,269/2-1,0,1,50,BLACKNESS);
PatBlt((HDC)wParam,0,49,269/2,1,BLACKNESS);
PatBlt((HDC)wParam,0,0,269/2,1,BLACKNESS);
static HBRUSH handleToButtonBrush = CreateSolidBrush(RGB(229,255,229));
return (INT_PTR)handleToButtonBrush;
"Font" is an object I created (I wrapped HFONT handle and CreateFont function with class etc. getWindowHandle() basically returns HFONT ).
the button renders nicely , yet pressing on it make the text-background turn white.
I search the net for a reason and a solution yet I didn't find a concrete one.
thanks in advanced!
You need to call those lines before the actual text drawing occurs
SetTextColor((HDC)wParam, RGB(0,0,0));
SetBkColor((HDC)wParam, RGB(229,255,229));
TextOut((HDC)wParam,30,15,buttonText,textLength);
I have create tab
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = (DWORD)ICC_TAB_CLASSES;
InitCommonControlsEx(&icc);
icc.dwICC = (DWORD)ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icc);
hwndTab = CreateWindow(WC_TABCONTROL,L"",WS_CHILD|WS_VISIBLE|WS_DLGFRAME|WS_CLIPSIBLINGS
,10,10,780,271,hwnd,(HMENU)3,hInstance,NULL);
TCITEM tcitem; tcitem.mask = TCIF_TEXT;
tcitem.pszText = L"Tab1";
TabCtrl_InsertItem(hwndTab,0,&tcitem);
and put a static control into the tab
CreateWindow(L"STATIC",L"Static control 1",
WS_CHILD|WS_VISIBLE,50,30,65,24,hwndTab,(HMENU)NULL,hInstance,NULL);
But the Static control 1 have the grey background. How can I give it a color.
Thanks
Because you've made the static control a child of the tab it will be sending WM_CTLCOLORSTATIC messages to the tab control. If you sub-class the tab control you can catch this message and modify its colours. Note however that with visual styles enabled the tab control isn't a flat single colour - it's more of a gradient, so even returning a matching solid colour won't necessarily look that great.
Tab controls are not meant to be parents. For each tab page, you should create a new modeless dialog as a sibling to the tab and place your static control in it. If you obey the following rules:
reference Common Controls v6 in your manifest
call InitCommonControlsEx() on startup
no WS_CLIPCHILDREN in your main window
no handling of WM_ERASEBACKGROUND
call EnableThemeDialogTexture() in the WM_INITDIALOG handler of the modeless dialog
call SetWindowPos(tab, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE) after creating the tab page (or it will be on the wrong end of the Z order)
… then your static control will be drawn with the correct color gradient, as will all other controls (checkboxes, sliders, …) in the modeless dialog, on all Windows version from XP to 10.
We have a series of dialogs in our application for which the dialog template defines 4 buttons along the bottom of the screen. However (depending which version of our hardware the application is running on) we sometimes create 2 additional buttons and then line up the 6 buttons (4 from the template, 2 created by calling CButton::Create()) along the bottom.
The problem I have is that usually the user can move the focus between these buttons using the left / right arrow keys (there is no mouse or touchscreen, just a restricted keyboard). This follows the control TAB-order as you would expect for the 4 buttons from the template. However the 2 dynamically create controls appear to be inserted at the beginning of the TAB-order, and that means (because they are put at the right-hand side of the screen) that they are in the "wrong" order as far as the cursor keys goes. In other words, when the focus gets to the left-hand button (TAB order 1) pressing the left arrow jumps the focus to the button on the right-hand side, which is just plain confusing...
There appears to be some link between Z-order (which I can affect with SetWindowPos()) and TAB-order but it does not seem to be a simple 1-to-1: by changing the Z-order I can move the sequence around so that the buttons are completely in the wrong order, so I can change the Z-order, but I can't work out how to get them in the right order.
Can anyone give a concise explanation of how TAB-order works, and how to control the sequencing of controls at runtime?
Edit:
kol suggested below using SetWindowPos() to set the Z-order. This I had tried before but it does not let the cursor keys control the focus as expected.
However, by rigging this up so I can use TAB (as a test -- this isn't practical for the end-user solution) I find that kol's solution does resolve the TAB-order. The problem I have is that this is not the same as the order used by the cursor keys!
So, revised question: how can I specify the order in which left / right cursor keys move focus between controls?
Solution:
With assistance from kol and MarkRansom's input, I have got this working now.
I used SetWindowPos() as suggested by kol to put my new buttons after the existing buttons in the TAB order, and then (as Mark suggested) made the first button WS_GROUP | WS_TABSTOP but cleared those flags from the other buttons.
However this was not enough to solve the problem, with the 2 new buttons still appearing to come before the first rather than after the second when using arrow keys (not TAB) to move around.
I looked at my dialog template, and it was like this:
IDD_QUERY DIALOG 0, 0, 156, 34
STYLE DS_SETFONT | WS_POPUP | WS_CAPTION
FONT 8, "MS Sans Serif"
BEGIN
PUSHBUTTON "+++Skey1+++",IDC_SKEY_1,1,21,36,12
PUSHBUTTON "+++Skey2+++",IDC_SKEY_2,40,21,37,12
PUSHBUTTON "+++Skey3+++",IDC_SKEY_3,79,21,36,12
PUSHBUTTON "+++Skey4+++",IDC_SKEY_4,118,21,36,12
LTEXT "Static",IDC_QUERY_MSG,2,1,153,15
END
So the static IDC_QUERY_MSG, which is used to display information to the user, was coming after the 4th button in the template. To resolve the problem, I moved IDC_QUERY_MSG before the first button (IDC_SKEY_1): this means the 6 buttons are not split up by a static inbetween, and has solved the problem.
Thanks to everyone for their assistance!
Use the SetWindowPos member of your buttons. Calling it on button A and setting its first parameter to button B, puts button A after button B in the TAB order. If you want to set the order of two controls, you have to know the controls before and after them in the TAB order - this example shows how to do this (its not MFC but pure WinAPI, but it's easy to understand).
== UPDATE ==
I created a dialog template with four buttons at the bottom and an editbox at the top, with TAB order button1 -> button2 -> button3 -> button4 -> editbox -> button1 -> ... In OnInitDialog, I added two additional buttons at runtime, and inserted them into the existing TAB order between button4 and editbox using SetWindowPos and GetNextWindow. Pressing TAB repeatedly shows that the TAB order is correct: button1 -> button2 -> button3 -> button4 -> button5 -> button6 -> editbox -> button1 -> ...
class CTestDlg : public CDialogEx
{
public:
CTestDlg() : CDialogEx(CTestDlg::IDD) {}
enum { IDD = IDD_TESTDIALOG };
protected:
CButton button5;
CButton button6;
virtual BOOL OnInitDialog();
};
BOOL CTestDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
CButton* button4 = (CButton*)GetDlgItem(IDBUTTON4);
CWnd* next = button4->GetNextWindow(GW_HWNDNEXT);
button5.Create("Button5", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON, CRect(340, 172, 415, 195), this, 1005);
button5.SetWindowPos(button4, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
button6.Create("Button6", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_PUSHBUTTON, CRect(422, 172, 497, 195), this, 1006);
button6.SetWindowPos(&button5, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
if (next != NULL)
next->SetWindowPos(&button6, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
return TRUE;
}
void CDynamicMfcButtonTestApp::OnTestRun()
{
CTestDlg testDlg;
testDlg.DoModal();
}
== UPDATE2 ==
The order of controls can be set using SetWindowPos and GetNextWindow, as above. The "tab order" and the "arrow order" can be set by setting the WS_TABSTOP and WS_GROUP styles of the controls:
The WS_TABSTOP style specifies the controls to which the user can
move by pressing the TAB key or SHIFT+TAB keys.
The WS_GROUP style
marks the beginning of a group of controls. If a control in the group
has the input focus when the user begins pressing direction keys, the
focus remains in the group. In general, the first control in a group
must have the WS_GROUP style and all other controls in the group must
not have this style. All controls in the group must be
contiguous—that is, they must have been created consecutively with no
intervening controls.
More details can be found on MSDN, here.
Try this:
Create a vector of dlg ids and populate it in the order you want the tab order to be:
typedef std::vector<int> TABLIST;
TABLIST lst;
lst.push_back( IDC_CONTROL1 );
lst.push_back( IDC_CONTROL2 );
lst.push_back( IDC_CONTROL3 );
Then call the setTabOrder method with the list:
void setTabOrder( TABLIST * plstTabs)
{
// Iterate through the list and put each item at the top of the tab order.
// Remember to do this backwards so the last item is actually the first item
// in the tab order
HDWP hDefer = BeginDeferWindowPos( (int)plstTabs->size() );
for ( TABLIST::reverse_iterator itTab = plstTabs->rbegin(); itTab != plstTabs->rend(); ++itTab )
{
DeferWindowPos( hDefer, GetDlgItem( *itTab )->m_hWnd, HWND_TOP, 0,0,0,0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOREPOSITION );
}
EndDeferWindowPos( hDefer );
}
Modified version of #snowduded's code that makes sure the WS_TABSTOP style is in also.
// Build your order in a vector.
std::vector<WORD> vCtrlIds;
vCtrlIds.push_back(IDC_CONTROL1);
vCtrlIds.push_back(IDC_CONTROL2);
vCtrlIds.push_back(IDC_CONTROL3);
// ... keep doing it!
// Iterate through the list and put each item at the top of the tab order.
// Remember to do this backwards so the last item is actually the first item
// in the tab order.
HDWP vDefer = BeginDeferWindowPos(vCtrlIds.size());
for(auto vCtrlId(vCtrlIds.rbegin()); vCtrlId != vCtrlIds.rend(); ++vCtrlId){
HWND vCtrl(GetDlgItem(*vCtrlId)); // Grab the handle.
SetWindowLongPtr(vCtrl, GWL_STYLE, // Make sure we have WS_TABSTOP.
GetWindowLongPtr(vCtrl, GWL_STYLE) | WS_TABSTOP);
DeferWindowPos(vDefer, vCtrl, HWND_TOP, 0, 0, 0, 0, // Reorder.
SWP_NOSIZE | SWP_NOMOVE | SWP_NOREPOSITION );
}
EndDeferWindowPos(vDefer);
Props to #snowdude for an excellent reusable solution.
If you are talking about a tab order of different controls over a dialog:
Try this
Open the dlg in resource and press Ctrl+D
Now you can set order by selecting controls one after another.
You can't change the tab order in runtime. What you can do is to put these two buttons in your dialog resource (with the right tab order), and make them invisible. Then you show/arrange the buttons as soon as you need them.
I've created two instances of Fred Acker's CHoverButtonEx class with a slight modification to include a disabled state.
These buttons exist on modeless dialog which contains the following properties:
IDD_MY_DIALOG DIALOGEX 0, 0, 162, 27
STYLE DS_SETFONT | WS_POPUP
EXSTYLE WS_EX_TOPMOST | WS_EX_TOOLWINDOW
FONT 9, "Arial", 400, 0, 0x0
BEGIN
CONTROL 146,IDC_STATIC_BKGND,"Static",SS_BITMAP,0,0,162,27
LTEXT "",IDC_STATIC_1,6,4,101,9,SS_WORDELLIPSIS
LTEXT "",IDC_STATIC_2,6,15,101,9
CONTROL "",IDC_BUTTON_1,"Button",BS_OWNERDRAW | WS_TABSTOP,108,4,24,19
CONTROL "",IDC_BUTTON_2,"Button",BS_OWNERDRAW | WS_TABSTOP,134,4,24,19
END
Everything is working well with the buttons except that now I need to implement a focus state but the behavior is strange and unexpected.
In my DrawItem message handler, I have the following code which functions pretty much exactly the same as the original minus some stuff I cleaned out which wasn't needed:
void CHoverButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// Do other stuff above and now find the state and draw the bitmap
if(lpDrawItemStruct->itemState & ODS_SELECTED)
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,m_ButtonSize.cx,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,m_ButtonSize.cx,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
else
{
if(m_bHover)
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,m_ButtonSize.cx*2,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,m_ButtonSize.cx*2,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
else
{
if (IsWindowEnabled())
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,0,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,0,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
else
{
//mydc->BitBlt(0,0,m_ButtonSize.cx,m_ButtonSize.cy,pMemDC,m_ButtonSize.cx*3,0,SRCCOPY);
mydc->StretchBlt(0,0, lpDrawItemStruct->rcItem.right-lpDrawItemStruct->rcItem.left,
lpDrawItemStruct->rcItem.bottom-lpDrawItemStruct->rcItem.top,
pMemDC,m_ButtonSize.cx*3,0, m_ButtonSize.cx,m_ButtonSize.cy, SRCCOPY );
}
}
}
if (lpDrawItemStruct->itemAction & ODA_FOCUS)
{
RECT rcFocus;
int iChange = 3;
rcFocus.top = lpDrawItemStruct->rcItem.top + iChange;
rcFocus.left = lpDrawItemStruct->rcItem.left + iChange;
rcFocus.right = lpDrawItemStruct->rcItem.right - iChange;
rcFocus.bottom = lpDrawItemStruct->rcItem.bottom - iChange;
pMemDC->DrawFocusRect(&rcFocus);
}
// clean up
pMemDC -> SelectObject(pOldBitmap);
delete pMemDC;
}
What is occurring is that when the dialog is the active window and I press tab once, the focus box jumps to the second button, even though I can confirm through the button's click handler that the first button has the real focus. Then when I press tab again the focus box jumps to include both buttons. Then another tab press moves the focus box to the other button and finally another tab press removes the focus box entirely. This sequence keeps occurring. Even holding Shift-Tab won't affect it.
I sniffed out the windows messages using spy++ and it looks pretty normal. I get a WM_DRAWITEM message for both button controls and they are successfully handled.
I'll mention one last thing; in my dialog code, when I'm initializing the buttons, I'm forced to place the buttons in the bottom of the z-order or else the IDC_STATIC_BKGND would draw over the buttons. This didn't seem normal to me because they should already be at the bottom of the z-order. (Just adding it in case it's a part of the cause to my problem).
m_button1.SetHorizontal(true);
m_button1.SetMoveable(FALSE);
m_button1.LoadBitmap(IDB_BUTTON_1);
m_button1.SetToolTipText(_T("Some Text1"));
// Draws the button after the background is drawn
m_button1.SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
m_button2.SetHorizontal(true);
m_button2.SetMoveable(FALSE);
m_button2.LoadBitmap(IDB_BUTTON_2);
m_button2.SetToolTipText(_T("Some Text2"));
// Draws the button after the background is drawn
m_button2.SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
Does anyone know how to correctly add a focus box for my situation?
Thanks.
Update:
After trying BrendanMcK's suggestion and not resolving the issue, I dug some more in the captured messages in spy++ and noticed something that seems like odd behavior. The following messages represent a single tab press in the dialog.
<00283> 00870794 R WM_CTLCOLORBTN hBrush:01900015
<00284> 00870794 S WM_DRAWITEM idCtl:112 lpdis:002AEF2C
<00285> 00870794 R WM_DRAWITEM fProcessed:True
<00286> 00870794 S DM_GETDEFID
<00287> 00870794 R DM_GETDEFID wHasDef:DC_HASDEFID wDefID:0001
<00288> 00870794 S WM_CTLCOLORBTN hdcButton:110114A7 hwndButton:01090502
<00289> 00870794 R WM_CTLCOLORBTN hBrush:01900015
<00290> 00870794 S WM_DRAWITEM idCtl:113 lpdis:002AF2A0
<00291> 00870794 R WM_DRAWITEM fProcessed:True
As evident there are two separate WM_DRAWITEM messages delivered. The details for each message is:
Message #284: action= ODA_FOCUS, state= 0x0110 (ODS_FOCUS = 0x0010)
Message #290: action= ODA_DRAWENTIRE, state= ODS_NOACCEL
I would have expected that in message #290, the action would again be ODA_FOCUS to allow the other button to "un-draw" the focus box.
I'm not sure why I'm even receiving an ODS_NOACCEL state as I'm using Win7. Is there something I've forgot to disable?
From MSDN on DRAWITEMSTATE:
ODA_FOCUS
The control has lost or gained the keyboard focus. The itemState member should be checked to determine whether the control has the focus.
Because you're redrawing the control, you should only be drawing the focus rect if the itemState indicates that the control has focus. Instead, you're drawing it in all cases, regardles of whether the control gains or loses focus. Add a check to see if itemState & ODS_FOCUS and you should be good.
Does anybody has a hint for the following problem?
I have a derived class from CPaneDialog, it contains just one button. I want to show a tooltip if the mouse is over it. For this I use CMFCToolTipCtrl:
// Create the ToolTip control.
m_ToolTip.Create(this, TTS_ALWAYSTIP | TTS_NOPREFIX);
m_ToolTip.Activate(TRUE);
CMFCToolTipInfo params;
params.m_bVislManagerTheme = TRUE;
m_ToolTip.SetParams(¶ms);
m_ToolTip.AddTool(GetDlgItem(IDC_BUTTON1), _T("Here is the text of my tooltip message."));
The m_ToolTip.RelayEvent(pMsg) I call from PreTranslateMessage().
If i compile and run the application, it looks like on the image below: The tooltip appears in the background of my pane!
ToolTipOnPane http://img268.imageshack.us/img268/9926/tooltiponpanedialog.png
set the topmost property.
m_ToolTip.SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);