• 16 Windows编程——系统内置窗口子类型之edit、ComboBox、ownerbutton、listbox


    edit类型的子窗口

    ES_MULTILINE:多行输入文本框

    窗口的消息:

    WL_COMMAND:

    EN_CHANGE:当edit窗口内的文本内容改变的时候,edit子窗口给父窗口发送一个WL_COMMAND消息,其通知码是EN_CHANGE。

    WM_GETTEXT:

    用SendMessage函数给edit窗口发送这个消息,同时,给wparam参数传递我们想要获得的字符个数,lParam传递一个内存块地址,sendMessage函数返回后,
    lParam指向的内存中,将保存获得的字符。

    WL_GETTEXTLENGTH:

    用 SendMessage函数给edit窗口发送这个消息,将wParam和1Param都赋值为NULL,那SendMessage函数,将返回edit中的字符个数,但这个字符个数不包括NULL结尾字符。

    edit空间演示源码

     1 #include<Windows.h>
     2 
     3 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
     4 
     5 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
     6 {
     7     WNDCLASS WndClass;
     8     TCHAR* ClassName = TEXT("MyClass");
     9     HWND hwnd;
    10     MSG msg;
    11     HBRUSH hBrush;
    12     hBrush = CreateSolidBrush(RGB(249,98,241));
    13 
    14     WndClass.cbClsExtra = 0;
    15     WndClass.cbWndExtra = 0;
    16     WndClass.hbrBackground = hBrush;
    17     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    18     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    19     WndClass.hInstance = hInst;
    20     WndClass.lpfnWndProc = WindProc;
    21     WndClass.lpszClassName = ClassName;
    22     WndClass.lpszMenuName = NULL;
    23     WndClass.style = CS_VREDRAW | CS_HREDRAW;
    24 
    25     if (!RegisterClass(&WndClass))
    26     {
    27         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
    28         return 0;
    29     }
    30 
    31     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
    32     if (hwnd == NULL)
    33     {
    34         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
    35         return 0;
    36     }
    37     ShowWindow(hwnd, nShow);
    38     UpdateWindow(hwnd);
    39 
    40     while (GetMessage(&msg, NULL, 0, 0))
    41     {
    42         TranslateMessage(&msg);
    43         DispatchMessage(&msg);
    44     }
    45 
    46     return 0;
    47 }
    48 
    49 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    50 {
    51     HDC hdc;
    52     static int cx, cy;
    53     static editHwnd, staticHwnd;
    54     TCHAR str[1024];
    55     int length;
    56     switch (message)
    57     {
    58     case WM_CREATE:
    59         editHwnd = CreateWindow(TEXT("edit"),NULL, WS_CHILD | WS_VISIBLE|WS_BORDER|ES_MULTILINE|ES_AUTOVSCROLL , 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
    60         staticHwnd = CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE|WS_BORDER, 0, 0, 0, 0, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL);
    61         return 0;
    62     case WM_SIZE:
    63         cx = LOWORD(lParam);
    64         cy = HIWORD(lParam);
    65         MoveWindow(editHwnd, 0, 0, cx/2, cy/2, TRUE);
    66         MoveWindow(staticHwnd, cx / 2, cy / 2, cx, cy, TRUE);
    67         return 0;
    68     case WM_COMMAND:
    69         if (HIWORD(wParam)==EN_CHANGE)
    70         {
    71             length = SendMessage(editHwnd, WM_GETTEXTLENGTH, NULL, NULL) + 1;
    72             length = length > 1023 ? 1023 : length;
    73             SendMessage(editHwnd, WM_GETTEXT, length, (LPARAM)str) ;
    74             str[length] = '/0';
    75             SetWindowText(staticHwnd,str);
    76         }
    77         return 0;
    78     case WM_DESTROY:
    79         PostQuitMessage(0);
    80         return 0;
    81     default:
    82         break;
    83     }
    84 
    85     return DefWindowProc(hwnd, message, wParam, lParam);
    86 }
    View Code

    运行结果

    ComboBox类型的子窗口 

    ComboBox子窗口的三种主要子类型:

    CB_ADDSTRING:在combobox控件的list框中,增加一个字符串,wParam=NULL,lParam符串的指针。

    WL_COMMAND:

    CBN_SELCHANGE:当combobox中的选项发生变化时,发送这个通知码,其中LOWORD(lParam)就是这个combobox的窗口标识ID。

    CB_GETCURSEL:用SendMessage函数,发送这个消息给combobox子窗口,那么这个函数将返回被选中的列表项的索引(索引是从0开始的)。wParam=lParam=NULL.

    演示代码

     1 #include<Windows.h>
     2 #include<WinUser.h>
     3 #include<tchar.h>
     4 #include<stdio.h>
     5 
     6 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
     7 
     8 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
     9 {
    10     WNDCLASS WndClass;
    11     TCHAR* ClassName = TEXT("MyClass");
    12     HWND hwnd;
    13     MSG msg;
    14 
    15     WndClass.cbClsExtra = 0;
    16     WndClass.cbWndExtra = 0;
    17     WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    20     WndClass.hInstance = hInst;
    21     WndClass.lpfnWndProc = WindProc;
    22     WndClass.lpszClassName = ClassName;
    23     WndClass.lpszMenuName = NULL;
    24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
    25 
    26     if (!RegisterClass(&WndClass))
    27     {
    28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
    29         return 0;
    30     }
    31 
    32     //CreateWindow返回之前,会发送WM_CREATE消息
    33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
    34     if (hwnd == NULL)
    35     {
    36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
    37         return 0;
    38     }
    39     ShowWindow(hwnd, nShow);
    40     UpdateWindow(hwnd);
    41 
    42     while (GetMessage(&msg, NULL, 0, 0))
    43     {
    44         TranslateMessage(&msg);
    45         DispatchMessage(&msg);
    46     }
    47 
    48     return 0;
    49 }
    50 
    51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    52 {
    53     PAINTSTRUCT pt;
    54     HDC hdc;
    55     static HWND ComboHwnd1, ComboHwnd2, ComboHwnd3,button,staticHwnd;
    56     TCHAR str[1024];
    57     switch (message)
    58     {
    59     case WM_CREATE:
    60         ComboHwnd1 = CreateWindow(TEXT("ComboBox"), NULL, WS_CHILD | WS_VISIBLE | CBS_SIMPLE|CBS_AUTOHSCROLL|WS_VSCROLL, 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
    61         ComboHwnd2 = CreateWindow(TEXT("ComboBox"), NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWN, 0, 0, 0, 0, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL);
    62         ComboHwnd3 = CreateWindow(TEXT("ComboBox"), NULL, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST, 0, 0, 0, 0, hwnd, (HMENU)3, GetModuleHandle(NULL), NULL);
    63         button = CreateWindow(TEXT("button"), TEXT("OK"), WS_CHILD | WS_VISIBLE , 0, 0, 0, 0, hwnd, (HMENU)4, GetModuleHandle(NULL), NULL);
    64         staticHwnd= CreateWindow(TEXT("static"), NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)5, GetModuleHandle(NULL), NULL);
    65         return 0;
    66     case WM_SIZE:
    67         MoveWindow(ComboHwnd1, 0, 0, 100, 100, TRUE);
    68         MoveWindow(ComboHwnd2, 150, 0, 100, 150, TRUE);
    69         MoveWindow(ComboHwnd3, 300, 0, 100, 150, TRUE);
    70         MoveWindow(button, 20, 120, 50, 25, TRUE);
    71         MoveWindow(staticHwnd, 0, 150, 100, 100, TRUE);
    72         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-1"));
    73         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-2"));
    74         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-3"));
    75         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-4"));
    76         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-5"));
    77         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-6"));
    78         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-7"));
    79         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-8"));
    80         SendMessage(ComboHwnd1, CB_ADDSTRING, NULL, (LPARAM)TEXT("char-9"));
    81         return 0;
    82     case WM_COMMAND:
    83         if (HIWORD(wParam) == CBN_SELCHANGE)
    84         {
    85             GetWindowText(ComboHwnd1,str,50);
    86             SetWindowText(staticHwnd,str);
    87         }
    88         return 0;
    89     case WM_DESTROY:
    90         PostQuitMessage(0);
    91         return 0;
    92     default:
    93         break;
    94     }
    95 
    96     return DefWindowProc(hwnd, message, wParam, lParam);
    97 }
    View Code

    运行结果:

     ownerbutton、listbox

    演示代码

      1 #include<Windows.h>
      2 #include<WinUser.h>
      3 #include<tchar.h>
      4 #include<stdio.h>
      5 
      6 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      7 
      8 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      9 {
     10     WNDCLASS WndClass;
     11     TCHAR* ClassName = TEXT("MyClass");
     12     HWND hwnd;
     13     MSG msg;
     14     HBRUSH hBrush;
     15 
     16     hBrush = CreateSolidBrush(RGB(249, 98, 241));
     17 
     18     WndClass.cbClsExtra = 0;
     19     WndClass.cbWndExtra = 0;
     20     WndClass.hbrBackground = hBrush;
     21     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     22     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     23     WndClass.hInstance = hInst;
     24     WndClass.lpfnWndProc = WindProc;
     25     WndClass.lpszClassName = ClassName;
     26     WndClass.lpszMenuName = NULL;
     27     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     28 
     29     if (!RegisterClass(&WndClass))
     30     {
     31         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     32         return 0;
     33     }
     34 
     35     //CreateWindow返回之前,会发送WM_CREATE消息
     36     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     37     if (hwnd == NULL)
     38     {
     39         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     40         return 0;
     41     }
     42     ShowWindow(hwnd, nShow);
     43     UpdateWindow(hwnd);
     44 
     45     while (GetMessage(&msg, NULL, 0, 0))
     46     {
     47         TranslateMessage(&msg);
     48         DispatchMessage(&msg);
     49     }
     50 
     51     return 0;
     52 }
     53 
     54 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     55 {
     56     static int cx, cy;
     57     static HWND ownerbutton,scroll,listbox;
     58     int index;
     59     TCHAR str[1024];
     60     RECT rect;
     61     switch (message)
     62     {
     63     case WM_CREATE:
     64         //创建普通button
     65         //ownerbutton = CreateWindow(TEXT("button"), TEXT("OK"), WS_CHILD | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
     66         //创建ownerbutton
     67         ownerbutton = CreateWindow(TEXT("button"), TEXT("OK"), WS_CHILD | WS_VISIBLE|BS_OWNERDRAW, 0, 0, 0, 0, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL);
     68         scroll = CreateWindow(TEXT("ScrollBar"), NULL, WS_CHILD | WS_VISIBLE|SBS_VERT , 0, 0, 0, 0, hwnd, (HMENU)2, GetModuleHandle(NULL), NULL);
     69         listbox = CreateWindow(TEXT("listbox"), NULL, WS_CHILD | WS_VISIBLE |LBS_STANDARD, 0, 0, 0, 0, hwnd, (HMENU)3, GetModuleHandle(NULL), NULL);
     70         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-1"));
     71         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-2"));
     72         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-3"));
     73         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-4"));
     74         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-5"));
     75         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-6"));
     76         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-7"));
     77         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-8"));
     78         SendMessage(listbox, LB_ADDSTRING, NULL, (LPARAM)TEXT("char-9"));
     79         return 0;
     80     case WM_SIZE:
     81         cx = LOWORD(lParam);
     82         cy = HIWORD(lParam);
     83         MoveWindow(ownerbutton, 10, 10, 100, 20, TRUE);
     84         MoveWindow(scroll, cx-20, 0, 20, cy, TRUE);
     85         MoveWindow(listbox, 200, 100, 100, 50, TRUE);
     86         return 0;
     87     case WM_PAINT:
     88         return 0;
     89     case WM_DRAWITEM:
     90         SetTextColor(((DRAWITEMSTRUCT*)lParam)->hDC, RGB(255, 0, 0));
     91         TextOut(((DRAWITEMSTRUCT*)lParam)->hDC,30,2, TEXT("确定"),2);
     92         return 0;
     93     case WM_CTLCOLORBTN:
     94         GetWindowRect(ownerbutton, &rect);
     95         SetBkMode((HDC)wParam,TRANSPARENT);
     96         SelectObject((HDC)wParam, CreateSolidBrush(RGB(0, 255, 0)));
     97         Rectangle((HDC)wParam,0,0,rect.right-rect.left,rect.bottom-rect.top);
     98         return (LRESULT)CreateSolidBrush(RGB(0, 255, 0));
     99     case WM_VSCROLL:
    100         //处理方法参考之前滚动条处理方法
    101         return 0;
    102     case WM_COMMAND:
    103         index = SendMessage(listbox, LB_GETCURSEL, NULL,NULL);
    104         SendMessage(listbox, LB_GETTEXT, index, (LPARAM)str);
    105         return 0;
    106     case WM_DESTROY:
    107         PostQuitMessage(0);
    108         return 0;
    109     default:
    110         break;
    111     }
    112 
    113     return DefWindowProc(hwnd, message, wParam, lParam);
    114 }
    View Code
  • 相关阅读:
    Python数据结构之列表
    前端html表单与css样式
    Http协议基本知识
    PHP-CGI远程任意代码执行漏洞(CVE-2012-1823)修复方案
    云计算定义
    编译最新linux内核(version 4.4.2)
    nginx负载均衡
    Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
    WebService--使用 CXF 开发 REST 服务
    WebService- 使用 CXF 开发 SOAP 服务
  • 原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9343731.html
Copyright © 2020-2023  润新知