Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 id created? Can two applications in Windows can have same control ids? Is there any way to manually set Windows control ids?

like image 725
Dungeon Hunter Avatar asked Sep 14 '11 18:09

Dungeon Hunter


3 Answers

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.

like image 170
Raymond Chen Avatar answered Oct 04 '22 01:10

Raymond Chen


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.

like image 24
Serge Wautier Avatar answered Oct 04 '22 00:10

Serge Wautier


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.

like image 26
BrendanMcK Avatar answered Oct 04 '22 00:10

BrendanMcK