Windows Programming Series Second article: Windows SDK Create basic controls

Source: Internet
Author: User

In the WIN32 SDK environment, how do you create common basic controls? We know that if we use MFC, simple drag and drop to complete the creation of most of the control, but since we are programming with the Windows SDK API, of course, from the root to solve the problem, in fact, MFC's lower layer is also done through these APIs.

In fact, the control is also a window, but a pre-created window class by Microsoft, such as a button is a class named "Button" class. Since the control is actually a window, of course, is also used CreateWindow or CreateWindowEx this function to complete, the second function in addition to a more extended style, the other is exactly the same, the following mainly in CreateWindow as an example, The function is prototyped as follows (actually mentioned in the first article):

HWND CreateWindow (LPCTSTR lpclassname,

LPCTSTR Lpwindowname,

DWORD Dwstyle,

int x, int y, int nwidth, int nheight,

HWND hWndParent,

HMENU HMENU,

HANDLE HInstance,

PVOID Lpparam);

Lpclassname: The class name of the control

Lpwindowname: The name or text of a control

Dwstyle: Control style

X,y,nwidth,nheight: control's coordinates and width height

hWndParent: The parent window of the control

HMenu: The ID of the control

HINSTANCE: Control is set to empty

Lpparam: Control is set to NULL

So how do you implement all the basic controls? Let's take an example first.

    • Create labels/static text

CreateWindow (Text ("STATIC"), Text ("Test String"), ws_child| Ws_visible, ten, ten, N, A, hWnd, (HMENU) idc_static1, NULL, NULL);

    • Create button

CreateWindow (Text ("button"), Text ("Click Me"), ws_child| Ws_visible, Max, Max, N, A, hWnd, (HMENU) idc_button1, NULL, NULL);

    • Create an edit box

CreateWindowEx (Ws_ex_clientedge, TEXT ("EDIT"), NULL, ws_child| ws_visible| Ws_border, N, A, (HMENU) idc_edit1, NULL, and NULL);

    • Create GroupBox

CreateWindow (Text ("button"), Text ("Frame"), ws_child| Ws_visible|bs_groupbox, +, (HMENU) idc_frame1, NULL, and NULL);

    • Create a radio box

CreateWindow (Text ("button"), Text ("radio button"), ws_child| Ws_visible|bs_autoradiobutton, 134, Max, Max, N, HWnd, (HMENU) idc_male, NULL, NULL);

    • Create check box

CreateWindow (Text ("button"), Text ("Red"), ws_child| Ws_visible|bs_checkbox|bs_autocheckbox, si, si, Wu, Wu, HWnd, (HMENU) idc_ckbred, NULL, NULL);

    • Create a list box

CreateWindowEx (Ws_ex_clientedge, TEXT ("LISTBOX"), NULL, ws_child| ws_visible| Lbs_standard, (HMENU) idc_listbox1, NULL, and NULL);

    • Create combo box

CreateWindow (TEXT ("COMBOBOX"), NULL, ws_child| ws_visible| ws_vscroll| cbs_autohscroll| Cbs_dropdownlist, +, +, +, N, HWnd, (HMENU) IDC_CB1, NULL, NULL);

    • Create scroll bar

CreateWindow (TEXT ("SCROLLBAR"), NULL, ws_child| ws_visible| Sbs_horz, ten, Max, N, A, hWnd, (HMENU) IDC_SCB1, NULL, NULL);

The above to create the various controls are very poor fool, you can copy down to change coordinates, text, style, ID, etc. can be created according to your needs.

Here we come to the actual combat, the use of these common controls in the program implementation, the following we implement a complete Windows program, the program contains these common controls and more usage.

/* Common Control instances */

#include <windows.h>

#define IDC_STATIC1 1001 #define IDC_BUTTON1 1002 #define IDC_EDIT1 1003 #define IDC_FRAME1 1004 #define Idc_male 20   #define IDC_FEMALE 2002 #define IDC_LISTBOX1 1005 #define IDC_CB1 1006 #define IDC_CKBRED 1007 #define IDC_CKBGRN 1008 #define IDC_CKBBLU 1009 #define IDC_SCB1 1010 #define IDC_SCB2 1011

Static TCHAR szappname[] = TEXT ("HelloWin32"); Static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, PSTR szcmdline, int icmdshow) {HWND hwnd; MSG msg; Wndclass Wndclass;

Wndclass.style = Cs_hredraw | Cs_vredraw; Wndclass.lpfnwndproc = WndProc; Wndclass.cbclsextra = 0; Wndclass.cbwndextra = 0; Wndclass.hinstance = hinstance; Wndclass.hicon = LoadIcon (NULL, idi_application); Wndclass.hcursor = LoadCursor (NULL, Idc_arrow); Wndclass.hbrbackground = (hbrush) getstockobject (White_brush); Wndclass.lpszmenuname = NULL; Wndclass.lpszclassname = Szappname;

if (! RegisterClass (&wndclass)) {MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, mb_iconerror); re Turn 0; }

HWnd = CreateWindow (szappname,                  //window class name szappname,                 //Window caption ws_overlappedwindow,        //Window style cw_usedefault,              //initial x position cw_usedefault,              //initial y position 400,              //initial x size 300,             // Initial y size null,                       //parent window handle null,                      //Window menu handle hinstance,                  //program instance handle NULL);                     //Creation parameters

ShowWindow (HWnd, icmdshow); UpdateWindow (HWND);

while (GetMessage (&msg, NULL, 0, 0)) {translatemessage (&msg); DispatchMessage (&MSG); }

return msg.wparam; }

Static LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {hwnd Hctrl;

Switch (message) {Case Wm_create:createwindow (text ("STATIC"), Text ("Test String"), ws_child| Ws_visible, ten, ten, N, A, hWnd, (HMENU) idc_static1, NULL, NULL);

CreateWindow (Text ("button"), Text ("Click Me"), ws_child| Ws_visible, Max, Max, N, A, hWnd, (HMENU) idc_button1, NULL, NULL); CreateWindowEx (Ws_ex_clientedge, TEXT ("EDIT"), NULL, ws_child| ws_visible| Ws_border, N, A, (HMENU) idc_edit1, NULL, and NULL);

CreateWindow (Text ("button"), Text ("Gender"), ws_child| Ws_visible|bs_groupbox, +, (HMENU) idc_frame1, NULL, and NULL); CreateWindow (Text ("button"), Text ("Male"), ws_child| Ws_visible|bs_autoradiobutton, 134, Max, Max, N, HWnd, (HMENU) idc_male, NULL, NULL); CreateWindow (Text ("button"), Text ("female"), ws_child| Ws_visible|bs_autoradiobutton, 194, Max, Max, N, HWnd, (HMENU) idc_female, NULL, NULL);

CreateWindow (Text ("button"), Text ("Red"), ws_child| Ws_visible|bs_checkbox|bs_autocheckbox, si, si, Wu, Wu, HWnd, (HMENU) idc_ckbred, NULL, NULL); CreateWindow (Text ("button"), Text ("green"), ws_child| Ws_visible|bs_checkbox|bs_autocheckbox, Max, N, Wu, Wu, HWnd, (HMENU) IDC_CKBGRN, NULL, NULL); CreateWindow (Text ("button"), Text ("Blue"), ws_child| Ws_visible|bs_checkbox|bs_autocheckbox, N, A, Wu, Wu, HWnd, (HMENU) idc_ckbblu, NULL, NULL);

Hctrl = CreateWindowEx (Ws_ex_clientedge, TEXT ("LISTBOX"), NULL, ws_child| ws_visible| Lbs_standard, (HMENU) idc_listbox1, NULL, and NULL); SendMessage (Hctrl, lb_addstring, 0, (LPARAM) TEXT ("List str1")); SendMessage (Hctrl, lb_addstring, 0, (LPARAM) TEXT ("List str2")); SendMessage (Hctrl, lb_addstring, 0, (LPARAM) TEXT ("List Str3")); SendMessage (Hctrl, lb_addstring, 0, (LPARAM) TEXT ("List Str4"));

Hctrl = CreateWindow (TEXT ("COMBOBOX"), NULL, ws_child| ws_visible| ws_vscroll| cbs_autohscroll| Cbs_dropdownlist, +, +, +, N, HWnd, (HMENU) IDC_CB1, NULL, NULL); SendMessage (Hctrl, cb_addstring, 0, (LPARAM) TEXT ("Comb str1")); SendMessage (Hctrl, cb_addstring, 0, (LPARAM) TEXT ("Comb str2")); SendMessage (Hctrl, cb_addstring, 0, (LPARAM) TEXT ("Comb str3")); SendMessage (Hctrl, cb_addstring, 0, (LPARAM) TEXT ("Comb STR4")); SendMessage (Hctrl, Cb_setcursel, 1, 0);

CreateWindow (TEXT ("SCROLLBAR"), NULL, ws_child| ws_visible| Sbs_horz, ten, Max, N, A, hWnd, (HMENU) IDC_SCB1, NULL, NULL); CreateWindow (TEXT ("SCROLLBAR"), NULL, ws_child| ws_visible| Sbs_vert, +, (HMENU) IDC_SCB2, NULL, and NULL); return 0;

Case WM_COMMAND: {hwnd hwndtmp; Int. Wmid = LoWord (wParam); switch (wmid) {Case idc_button1:hwndtmp = GetDlgItem (hwnd, I DC_STATIC1); SetWindowText (hwndtmp, Text ("New text")); MessageBox (hWnd, Text ("Text changed!"), Text ("info"), MB_OK); Break }} return 0;

Case wm_ctlcolorstatic: {HDC hdc = (HDC) WParam; SetTextColor (hdc, RGB (0xFF, 0x00, 0x00)); } return (BOOL) ((hbrush) Getstockobject (Null_brush));

Case Wm_destroy:postquitmessage (0); return 0; }

Return DefWindowProc (hWnd, message, WParam, LParam); }

Compile the run and the results are as follows:

How, is not very simple, although our control to create more trouble than MFC, but we are from the "root" to solve the problem, the feeling is not the same! With these basic uses, you can build a more replicated Windows interface. Hurry up and try it out.

Reprint please specify the original address: http://www.coderonline.net/?p=680

As an IT technician to hone their own technology is essential, welcome to join QQ Group: Programmer Interaction Alliance (254241126), you can chat with Daniel online at any time to discuss their interests in the topic, Daniel every day for you to dedicate a technical original, so that they spend the least time to learn a lot of things, I am looking forward to your joining in the Programmer Interaction Alliance:)

If you want to know more about programming knowledge, please follow our public number, timely communication, there are a variety of Daniel to help you solve the problem. Public Platform: Programmer Interaction Alliance (coder_online).

Welcome to join our QQ Group: Programmer Interaction Alliance (254241126)

Windows Programming Series Second article: Windows SDK Create basic controls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.