通过调用创建子控件时CreateWindowEx https://msdn.microsoft.com/en-us/library/windows/desktop/ms632680.aspx,你必须分配一个控件ID(通过重载hMenu范围)。控件 ID 稍后可用于引用控件,而无需存储动态创建的控件HWND
(例如,当调用GetDlgItem https://msdn.microsoft.com/en-us/library/windows/desktop/ms645481.aspx or GetDlgItemInt https://msdn.microsoft.com/en-us/library/windows/desktop/ms645485.aspx).
Some controls rarely need to be identified in code. A prominent example is the Static Control https://msdn.microsoft.com/en-us/library/windows/desktop/bb760769.aspx1, that, if defined in a resource script, usually does not need to be referenced in code. You (or the dialog manager) still need to pass a control ID when creating the control, even though you don't use it later on. For those controls you can pass the IDC_STATIC
control ID, that is defined in a wizard-generated Resource.h
file2.
1 Other examples include the Icon Control https://msdn.microsoft.com/en-us/library/windows/desktop/aa369208.aspx (a static control with the SS_ICON
style), the Line Control https://msdn.microsoft.com/en-us/library/windows/desktop/aa369760.aspx (a static control with the SS_ETCHEDHORZ
and SS_SUNKEN
styles), or the GroupBox Control https://msdn.microsoft.com/en-us/library/windows/desktop/aa368766.aspx.
2 This is not a convention of the Windows API3. It is strictly a decision made by user code. You could use another ID value, or not define IDC_STATIC
at all if you want, and use an integer literal in the LTEXT control statement https://msdn.microsoft.com/en-us/library/windows/desktop/aa381021.aspx instead: LTEXT "Filename", -1, 10, 10, 100, 100
3 Granted, the SDK header winres.h
does define the preprocessor symbol IDC_STATIC
as (-1)
, so if you do define it in your code, make sure to assign the same value to avoid any confusion.