简体   繁体   English

如何创建Windows GUI控件ID?

[英]How are Windows GUI control ids created?

In Windows, for each and every control like (for every dialog, window, textbox and checkbox etc) a control id will be given. 在Windows中,对于每个控件(对于每个对话框,窗口,文本框和复选框等),将给出控件ID。

How is this control id created? 如何创建此控件ID? Can two applications in Windows can have same control ids? Windows中的两个应用程序可以具有相同的控件ID吗? Is there any way to manually set Windows control ids? 有没有办法手动设置Windows控件ID?

The control ID is one of the parameters passed to the CreateWindow function. 控件ID是传递给CreateWindow函数的参数之一。 If the control was created from a dialog template, then the dialog manager gets the control ID from the dialog template. 如果控件是从对话框模板创建的,则对话框管理器从对话框模板中获取控件ID。 It is quite common for two controls to have the same ID. 两个控件具有相同的ID是很常见的。 For example, most Cancel buttons will have the control ID IDCANCEL . 例如,大多数“ Cancel按钮将具有控件ID IDCANCEL

In addition to what Raymond wrote: 除了雷蒙德所写的内容:

It's perfectly legal for a window to create to child windows (aka controls) and give them the same ControlId. 一个窗口创建到子窗口(也就是控件)并给它们相同的ControlId是完全合法的。 The only problem is that you won't be able to uniquely retrieve a control by its id (using GetDlgItem() ). 唯一的问题是您将无法通过其id唯一地检索控件(使用GetDlgItem() )。 If you are not interested in manipulating a control at runtime (such as a static label), you don't have to care giving it a unique control id. 如果您对在运行时操作控件(例如静态标签)不感兴趣,则不必关心为其提供唯一的控件ID。 Just give it 0xFFFF). 只要给它0xFFFF)。

An it's certainly legal (and usual) the same control id for different controls/child windows in different application or parent windows (eg IDCANCEL or IDOK for buttons). 对于不同应用程序或父窗口中的不同控件/子窗口(例如IDCANCEL或按钮IDOK),它肯定是合法的(和通常的)相同的控件ID。 GetDlgItem() retrieves the control of one given parent window. GetDlgItem()检索一个给定父窗口的控件。

In addition to the information in the other answers: 除了其他答案中的信息:

In windows for each and every control like (for every dialog,window,textbox and checkbox etc) a control id will be given 在每个控件的窗口中(对于每个对话框,窗口,文本框和复选框等),将给出一个控件ID

That's not actually quite true: top-level windows - such as app windows, and dialogs, don't actually have a control ID at all. 实际上并非如此:顶级窗口(例如应用程序窗口和对话框)实际上根本没有控件ID。 Only child windows can have control IDs. 只有子窗口可以有控件ID。

(Top-level windows use that parameter of CreateWindow to indicate the HMENU for the window instead - so only top-level windows can have menubars.) (顶级窗口使用CreateWindow的参数来指示窗口的HMENU - 所以只有顶级窗口才能有菜单栏。)

It's really up to the app developer to decide how to assign and use the IDs. 这取决于应用程序开发人员决定如何分配和使用ID。 Usually they are used with GetDlgItem(), which looks for a HWND with a given ID with a parent HWND, so in that case, the IDs only need to be unique within that parent. 通常它们与GetDlgItem()一起使用,GetDlgItem()查找具有父HWND的给定ID的HWND,因此在这种情况下,ID只需要在该父项中是唯一的。 If a developer doesn't need to look up a control at runtime, it can give it any ID, traditionally -1 is used there. 如果开发人员不需要在运行时查找控件,它可以为其提供任何ID,传统上在那里使用-1。

Some frameworks don't use control IDs at all and just keep track of the HWNDs as they are created. 有些框架根本不使用控制ID,只是在创建HWND时跟踪它们。

In addition to other answers: 除了其他答案:

Although control's ID can be the same, you'd better make it unique. 虽然控件的ID可以相同,但最好让它独一无二。 Control reports events to their parent window with its id and hwnd. 控件使用其id和hwnd将事件报告给其父窗口。 In the parent's message loop we generally use id to identify the control, in this case, if you want different event handling, use different id for each control. 在父的消息循环中,我们通常使用id来标识控件,在这种情况下,如果您想要不同的事件处理,请为每个控件使用不同的id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM