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.
How is this control i开发者_如何转开发d created? Can two applications in Windows can have same control ids? Is there any way to manually set Windows control ids?
The control ID is one of the parameters passed to the CreateWindow
function. If the control was created from a dialog template, then the dialog manager gets the control ID from the dialog template. It is quite common for two controls to have the same ID. For example, most Cancel
buttons will have the control 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. The only problem is that you won't be able to uniquely retrieve a control by its id (using 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. Just give it 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). GetDlgItem() retrieves the control of one given parent window.
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
That's not actually quite true: top-level windows - such as app windows, and dialogs, don't actually have a control ID at all. Only child windows can have control IDs.
(Top-level windows use that parameter of CreateWindow to indicate the HMENU for the window instead - so only top-level windows can have menubars.)
It's really up to the app developer to decide how to assign and use the IDs. 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. If a developer doesn't need to look up a control at runtime, it can give it any ID, traditionally -1 is used there.
Some frameworks don't use control IDs at all and just keep track of the HWNDs as they are created.
In addition to other answers:
Although control's ID can be the same, you'd better make it unique. Control reports events to their parent window with its id and 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.
精彩评论