Win32 control creation and message response, win32 Control Message response
1. Control Creation
Controls are created in the same way as windows, for example:
// -------- Create a window or control ----
Hwnd = CreateWindow ("button", "myButton01", WS_VISIBLE | WS_CHILD, 80, 60, 35, 25, hWnd, (HMENU)IDB_BUTTON01, HInst,NULL);
Is the creation of a button, where hWnd is the window handle, and hInst is the application handle.
For other controls, you can change the first parameter "button" to "edit", "listbox", "static", "combobox", and so on;
If you want to create many widgets, you can use a loop and a macro combination instead:
# Define CreateCtrl (s) s. hCtrl = CreateWindow (s. sClass, s. sCaption, s. wStyle, s. x, s. y, s. w, s. h, hWnd, s. id, hInst,NULL)
# Define ctrlNum 2
// The above hInst should be set as a global variable and initialized during Window Creation
StructCtrlParam{
HWNDHCtrl;
CharSClass [8];
CharSCaption [16];
DWORDDwStyle;
IntX;
IntY;
IntW;
IntH;
IntId;
};
CtrlParamCtrlParams [ctrlNum] = {"button", "myButton02 ",WS_VISIBLE|WS_CHILD, 60, 35, 25 ,(HMENU)IDB_BUTTON02},
// {...}, {"Edit", "myEdit01 ",WS_VISIBLE|WS_CHILD
| ES_WANTRETURN | ES_AUTOVSCROLL | ES_MULTILINE, 80,110,100, 80 ,(HMENU)IDE_EDIT01}};
// --- WM_CREATE:
For(Int I = 0; I <CtrlNum; I ++)
{
CreateCtrl(CtrlParams [I]);
}
2. About dwStyle Parameters
The dwStyle parameter of the control has two layers. The window type starts with WS _, and other BS _, ES _, and LBS _ parameters indicate the unique type of the control.
WS_VISIBLE indicates visible. If no, the control is hidden. It can be dynamically changed in the program:
// ------ Set the window or control to be visible ---- ShowWindow (ctrlParams [0]. hCtrl,SW_SHOWNOMAL);
WS_CHILD is used to create the control and works with hWnd to determine its parent window. The position parameters x, y, width, and height are relative to the parent window;
The window or control created by WS_DISABLED is unavailable or can be dynamically changed:
// ----- SendMessage (..., WM_ENABLE,...); ------- cannot be used here ,..);-------
// ----- The result is that the control is removed in gray, but the control is still unavailable -----------
EnableWindow (ctrlparams [0]. hCtl,TRUE);
WS_GROUP is mainly used for the edit tab key function. Scope: A WS_GROUP must be in the middle of the next WS_GROUP.
WS_BORDER adds a border. If you need other borders, you can use createdomainwex. The first parameter is added, and WS_EX_CLIENTEDGE at the end of EDGE starting with WS_EX _ is added.
Other MSDN queries will not be repeated.
(1). edit Control:
ES_AUTOHSCROLL and ES_AUTOVSCROLL when the horizontal and vertical input exceeds the width or height, the slider is automatically displayed; WS_VSCORLL and WS_HSCORLL are not required;
ES_MULTILINE supports multiple rows;
ES_WANTRETURN: Press ENTER or the program outputs '\ n' to wrap the line. It must be combined with ES_MULTILINE. If it is not selected, the line feed is not accepted until the end of the line;
ES_READONLY some visual editors, also known as Lock, cannot be input on the keyboard after selection, but can be output using the WM_SETTEXT program;
// ------ TRUE indicates LOCK ---------
// ------- FALSE indicates UNLOCK ----------
SendMessage (ctrlParams [1]. hCtrl,EM_SETREADONLY,TRUE, 0 );
// -------- Obtain the edit content ----------------
SendMessage (hCtrl,WM_GETTEXT, (WPARAM) lenth, (LPARAM) buf );
// ------- Set the edit content, with a character length limit, generally 1kB ------------------
SendMessage (hCtrl,WM_SETTEXT, 0, (LPARAM) buf );
// ------ The selected characters nStart and nEnd can be equal --------
SendMessage (hCtrl,EM_GETSEL, (WPARAM) nStart, (LPARAM) nEnd );
// ------ NStart =-1 Add the character ---------- at the end of the Selection ----------
SendMessage (hCtrl,EM_REPLACESEL, 0, buf );
(2). listbox control:
LBS_NOTIFY can receive the message of clicking and double-clicking a list item in the main window;
LBS_MULTIPLESEL can be selected multiple times;
CaseWM_COOMAND:
Switch (HIWORD(WParam ))
{CaseLBN_DBLCLK: Break; caseLBN_SELCHANGE: Break; default: break ;}
// ----- Add an item --------
Index = SendMessage (hCtrl,LB_ADDSTRING, 0, lpName );
// ----- Delete an item --------
SendMessage (hCtrl,LB_DELETESTRING, SelIndex, 0 );
// ----- Select an item --------
SendMessage (hCtrl,LB_SETCURSEL, Index, 0 );
// ----- Obtain the selected item index --------
Index = SendMessage (hCtrl,LB_GETCURSEL, 0, 0 );
// --- Note: Use LB_SETSEL and LB_GETSEL in LBS_MULTIPLESEL
3.Window position
// ----- Obtain the position and size of the window or control ----
GetWindowRect (hwnd, & rect );
// ---- Set ....---
SetWindowPos (hwnd,HAND_TOPMOST, X, y, w, h,SWP_NOZORDER);