[英]How to disable Edit control's focus on Dialog first launch?
Hello everybody reading this. 大家好,读这个。 Thanks in advance for your time.
在此先感谢您的时间。
One thing before question: I DO NOT use neither MFC nor Windows Forms, just WinApi in C++. 有问题的一件事:我既不使用MFC也不使用Windows Forms,只使用C ++中的WinApi。
Well, I am making a polynomial calculator in Visual C++. 好吧,我在Visual C ++中制作一个多项式计算器。 I added a Dialog to it, which was created in resources (
.rc
file) using drag'n'drop method. 我添加了一个Dialog,它是使用drag'n'drop方法在资源(
.rc
文件)中创建的。 I suppose there would be no such a problem if i created my Dialog with CreateWindowEx
(but I don't want to). 我想如果我用
CreateWindowEx
创建我的Dialog会有这样的问题(但我不想)。
My Dialog has a few of Edit Controls. 我的对话框有一些编辑控件。 Everything is fine except that when the Dialog is launched, one of Edit controls takes focus to be ready to take keyboard input.
一切都很好,除了在启动Dialog时, 其中一个Edit控件将焦点准备好接受键盘输入。
I have included management of EN_KILLFOCUS
(Edit sends it to parent when loses focus due to selecting another control). 我已经包含了
EN_KILLFOCUS
管理(由于选择了另一个控件,编辑在失去焦点时将其发送给父级)。
Here I read from control to wstring
(string of wide characters - _UNICODE
is defined), use some kind of parser to verify this wstring
and remove bad characters, and then put correct string into the same edit control. 这里我从控件读取到
wstring
(宽字符串 - 定义了_UNICODE
),使用某种解析器来验证这个wstring
并删除坏字符,然后将正确的字符串放入同一个编辑控件中。 It works fine, but here is the source of my problem : 它工作正常,但这是我的问题的根源 :
When there was no input, parser returns string "0"
(not the NULL
, string is just set to "0"), as if control had focus and then lost it even before I clicked anything in Dialog. 当没有输入时,解析器返回
string "0"
(不是NULL
,字符串只是设置为“0”),就像控件有焦点一样,然后在我单击Dialog中的任何内容之前丢失它。
Due to that, and something else (this is what I have to figure out), at the Dialog launch parser puts this string "0"
to edit. 由于这个,以及其他东西 (这是我必须弄清楚的),在Dialog启动解析器将此
string "0"
编辑。
I want to make my edit not be able to take input from keyboard until i click one of the Edits (including this one). 我想让我的编辑无法从键盘输入,直到我点击其中一个编辑(包括这一个)。 If it is not possible, I want to clear the whole text at the beginning of dialog (being able to take input is not a problem, I just want to prevent parser from entering
string "0"
at the beginning) My code: 如果不可能,我想在对话框的开头清除整个文本(能够输入输入不是问题,我只是想阻止解析器在开头输入
string "0"
)我的代码:
In DlgProc I have: 在DlgProc我有:
//up here is switch to manage all controls
case MyEditID: // here is ID of one of my edits from resources
switch (HIWORD(wParam))
{
case EN_KILLFOCUS: // edit lost focus - another control selected
if (LOWORD(wParam)==MyEditID) //necessary to determine if
// one of allowed Edits sent this message
// because I have also other Edits
{
GetDlgItemText(hPanel, LOWORD(wParam), MyTempWcharArray, 100);
MyTempString.assign(MyTempWcharArray);
w1 = polynomial(MyTempWcharArray); // parser takes the string
// and removes bad chars in constructor
// polynomial is my class - you don't have to care of it
// w1 is declared before as object of polynomial class
MyTempString = w1.ConversionToString();
SetDlgItemText(hDialog, LOWORD(wParam), sw1);
}
break;
}
break;
does it matter what integer number is set to Edit's ID? 编辑ID的整数设置是否重要?
I know SetFocus()
, and WM_SETFOCUS
message. 我知道
SetFocus()
和WM_SETFOCUS
消息。 In this case I just can't get this working. 在这种情况下,我无法让这个工作。
If i haven't included something important to make you see my point please let me know. 如果我没有包含一些重要内容让你明白我的观点,请告诉我。 I'm sorry I'm just a newbie in
WinAPI
world. 对不起,我只是
WinAPI
世界的新手。
EDIT: For those with a similar problem: Do not do this: I made an workaround with global variable ProcessKillFocus set to false indicating that instructions in message management should not be processed, except that at the end (just before break;) I am changing it to true, so next time and later it will be processed: 编辑:对于那些有类似问题的人: 不要这样做:我做了一个解决方法,将全局变量ProcessKillFocus设置为false,表明不应该处理消息管理中的指令,除了最后(就在休息之前;)我正在改变它是真的,所以下次和以后它将被处理:
case EN_KILLFOCUS:
if (ProcessKillFocus && LOWORD(wParam)==MyEditID)
{
// first time global ProcessKillFocus is false so all this is skipped
// 2nd time and later do all the stuff
}
ProcessKillFocus = true;
break;
Huge thanx to Sheyros Adikari for making my question easy to understand!!! 巨大的感谢Sheyros Adikari使我的问题易于理解! Huge thanx to patriiice for simple answer on a huge messing question!!!
对于一个巨大的混乱问题的简单回答,巨大的tox to patriiice!
ANSWER: BTW: patriiice, I tried this: 答案: BTW:patriiice,我试过这个:
case WM_INITDIALOG:
SetFocus(GetDlgItem(hDialog, Desired_Control_ID));
return (INT_PTR)FALSE;
break;
IT JUST WORKS!!! 它只是工作!!!
您必须返回FALSE到WM_INITDIALOG消息并自己设置正确的焦点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.