Click here to Skip to main content
13,049,814 members (74,347 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hello All.
I need to set the focus by pressing "TAB" manually.
I have a problem when manually switching control focus using function SetFocus.
"Focus Rect" does't show.
If I use the default function then the "Focus Rect" is drawn.

What should be done to make the "Focus Rect" was painted?
Posted 23-Sep-12 21:08pm
Jochen Arndt 25-Sep-12 2:45am
You may call RedrawWindow() after SetFocus().
Daniele Rota Nodari 8-Oct-12 4:08am
Hi. Your question is not clear to me.
- You say that You need to set focus by pressing "TAB" manually: do you mean that the final user will simply hit the TAB key?
- You say that SetFocus does not work as expected, but the "default function" is working. What is this default function?
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I know how to do this for the user control.
I want to draw "Focus rect" on any standard control(Button, ComboBox etc.) without reimplementing draw function.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

HWND SetFocus(HWND hWnd) function set the focus at window, and DRAWITEMSTRUCT struct has member variable for item state.
typedef struct tagDRAWITEMSTRUCT {
    UINT CtlType;
    UINT CtlID;
    UINT itemID;
    UINT itemAction;
    UINT itemState;
    HWND hwndItem;
    HDC hDC;
    RECT rcItem;
    ULONG_PTR itemData;

void CMyControl::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
	BOOL bIsFocus = (lpDIS->itemState & ODS_FOCUS);

Rate this: bad
Please Sign up or sign in to vote.

Solution 3

If you need to draw focus rect then you just have to google for drawfocusrect and open the first link
Member 2685505 26-Sep-12 3:34am
Sorry my bad English.
I need, that a button has painted the "Focus Rect" itself.
armagedescu 26-Sep-12 6:06am
Ok, sorry for my bad french, but there is a very useful site: All you can is to play with button messages, styles and states. You can either call WM_SETFOCUS by SendMessage/SendDlgItemMessage, or call SetFocus. If window handle or dialog item identifier is correct, then the button will draw the focus rect automatically. Any controls which draws focus rect will draw it. You can also call control specific messages, for acheive the same thing, like BN_SETFOCUS, EN_SETFOCUS, HCBT_SETFOCUS, LBN_SETFOCUS and so on. Also see, maybe your button is ownerdraw.
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

I did it.
All controls necessary to send the WM_UPDATEUISTATE message, before calling SetFocus function.
The focus frame will be displayed correctly.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100