I want to make program in WINApi -it is simple window with one textbox - if you will press key 'A',it jumps to textbox(like when you click on it,and then you will be able to write to it). Only thing I dont know is if in WINApi is a function which forces program to make textbox active without I had to click to it?

Dani AI

Generated

Quick summary and context: was right that the keyboard-focus API is the correct direction, and ’s final flow (retrieve the edit control, give it keyboard focus, then make the caret visible) is exactly what you need for the behavior you described. The API that actually sets keyboard focus is documented here. (learn.microsoft.com)

Why a plain call like “SetFocus(hEdit)” can appear to do nothing: common causes are passing an invalid HWND, calling before the control exists or is visible, calling from a different thread than the control, or the control being disabled. Get or keep the child HWND reliably (for example with GetDlgItem or the HWND returned by CreateWindow/Ex) and call focus functions only after the control is created and shown. (learn.microsoft.com)

Windows focus rules: the system prevents apps from “stealing” foreground focus, so your top-level window often has to be the foreground/active window before a child control will accept typed characters. If the top-level window is backgrounded, setting the child focus alone may not let keys reach it. Make the top window active (SetForegroundWindow / SetActiveWindow) or ensure your process has recent input before setting the control focus. The platform docs explain these restrictions and how the foreground APIs behave. (learn.microsoft.com)

Advanced and practical tips: when you need the caret to appear and input to flow immediately, explicitly place or select the edit’s text (the edit control’s “select” message shows the caret and positions it). For cross-thread focus workarounds, AttachThreadInput can be used to share input state briefly, but it has side effects (keyboard state reset, desktop limits) so use it only when necessary. Finally, if you handle key messages in the parent window, avoid swallowing WM_CHAR/WM_KEYDOWN before the edit gets them. (learn.microsoft.com)

Recommended Answers

All 7 Replies

call win32 api function SetFocus()

I tried SetFocus(hEdit); - (hEdit is hwnd of my textbox) - but it does nothing. Is there some another way?

Change of problem - now I used
SendDlgItemMessage(hwnd,IDB_TEXTBOX1,WM_SETFOCUS,NULL,NULL);
(IDB_TEXTBOX1 is command of my textbox)
,it is better cause it moves me to textbox,i have cursor here,but when I want to write , it doesn't write. When I post WM_KILLFOCUS then and move to it my mouse click ,then it works ok,but I want to click 'A' and then be able to write.I tried even another last two parameters like 0,1,IDB_TEXTBOX1 and 20 but nothing helps.
Could you tell me how to use this function properly?

...finally I solved the problem but it was pain in ass.If somebody is interested it is

SetFocus(GetDlgItem(hwnd, IDB_TEXTBOX1));
SendDlgItemMessage(hwnd, IDB_TEXTBOX1, EM_SETSEL, 0, -1);

Hello, i would like to ask a question.

How do you get the "command" for your textbox "IDB_TEXTBOX1" ?
What do you mean with "command" ? Because textboxes have no
property like "command" only property like "name" or "id"...

Thank you 4 answering my question.

->amart:
I just have that textbox like hwnd, not like textbox.

HWND hEdit;

...

then in winproc:

case WM_CREATE:
hEdit = CreateWindowEx(NULL, "EDIT", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE, 160,ytextbox,100,38,hwnd,(HMENU)IDB_TEXTBOX1, NULL,NULL);

...
break;

Oh okay, thank you for your help

it works wonderful ;)

have a nice day!
amart

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.