• win32 API编程之控件三


    一、树视图控件

    二、上下控件

    三、IP地址控件

    四、分页控件

    五、工具条控件

    六、提示控件

    七、状态栏控件

    八、控件条控件

    一、树视图控件

    1)什么是树视图控件

    2)样式

    3)向控件发送消息设置控件

    4)控件向父窗口发送消息

    5)代码实例

    #include<windows.h>
    #include<CommCtrl.h> 
    #include"resource.h" 
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    //#pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    #pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
            case WM_DESTROY:
                PostQuitMessage(0);//可以使GetMessage返回0    
                return 0;
            case  WM_COMMAND:
            {
                WORD  CtrlID = LOWORD(wParam);
                WORD   code = HIWORD(wParam);
            }break;
    
            case  WM_NOTIFY:
            {
                NMHDR  * pHDR = (NMHDR  *)lParam;
                if (pHDR->code == TVN_SELCHANGED)
                {
                    LPNMTREEVIEW  pnmtv = (LPNMTREEVIEW)lParam;
                    TVITEM  &itemNew = pnmtv->itemNew;
                    if (itemNew.state  &  TVIS_SELECTED)
                    {
                        //根据树节点句柄获取文本
                        TVITEM tv;
                        tv.mask = TVIF_TEXT;
                        TCHAR  buf[64] = {0};
                        tv.pszText = buf;
                        tv.cchTextMax = 64;
                        tv.hItem = itemNew.hItem; //查找这个节点的文本
                        SendMessage(pHDR->hwndFrom, TVM_GETITEM, 0, (LPARAM)&tv);
                        MessageBox(hWnd, tv.pszText, L"提示", MB_OK);
                    } 
                }
            }break;
    
            case WM_CREATE:
            {
                //创建树控件
                HWND  hTreeView = CreateWindow(WC_TREEVIEW, NULL,
                    WS_CHILD | WS_VISIBLE | TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT, 30, 30, 300, 300,
                    hWnd, (HMENU)111, GetModuleHandle(NULL), NULL);
                //创建图标列表
                HIMAGELIST  hImageList = ImageList_Create(16, 16, ILC_MASK, 3, 1);
                //添加图标到ImageLists
                for (int i = 0; i < 3; ++i)
                {
                    HICON  hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1 + i));
                    ImageList_AddIcon(hImageList, hIcon);
                    DestroyIcon(hIcon);
                }
                //设置树控件的图像列表
                SendMessage(hTreeView, TVM_SETIMAGELIST, TVSIL_NORMAL, (LPARAM)hImageList);
    
                //添加树根节点到控件
                TVINSERTSTRUCT  tvs;
                tvs.hInsertAfter = TVI_ROOT;
                tvs.hParent = NULL;
                
                TVITEM  tv;
                tv.mask = TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
                tv.pszText = L"根节点"; 
                tv.iImage = 2;
                tv.iSelectedImage =2;
                tvs.item = tv; 
    
                HTREEITEM  hRoot=(HTREEITEM)SendMessage(hTreeView, TVM_INSERTITEM,  0, (LPARAM)&tvs);
    
                //插入子节点
                TVINSERTSTRUCT  tvs2;
                tvs2.hInsertAfter = TVI_LAST;
                tvs2.hParent = hRoot;
    
                TVITEM  tv2;
                tv2.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
                tv2.pszText = L"子节点1";
                tv2.iImage = 1;
                tv2.iSelectedImage =1;
                tvs2.item = tv2;
    
                HTREEITEM  hChild1 = (HTREEITEM)SendMessage(hTreeView, TVM_INSERTITEM, 0, (LPARAM)&tvs2);
    
    
                //插入子节点
                TVINSERTSTRUCT  tvs3;
                tvs3.hInsertAfter = TVI_LAST;
                tvs3.hParent = hRoot;
    
                TVITEM  tv3;
                tv3.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
                tv3.pszText = L"子节点2";
                tv3.iImage = 0;
                tv3.iSelectedImage = 0;
                tvs3.item = tv3;
    
                HTREEITEM  hChild2 = (HTREEITEM)SendMessage(hTreeView, TVM_INSERTITEM, 0, (LPARAM)&tvs3);
    
    
    
                //展开树节点
                SendMessage(hTreeView, TVM_EXPAND, TVE_EXPAND, (LPARAM)hRoot);
    
                //选择节点
             SendMessage(hTreeView, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hChild1);
    
             //让树控件有焦点
             SetFocus(hTreeView);
    
            }break;
        
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    二、上下控件

    1)什么是上下控件

    2)样式

    3)向控件发送消息设置控件

    4)向父窗口发送消息

    5)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    //#pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == UDN_DELTAPOS)
            {
                 LPNMUPDOWN   pNM = (LPNMUPDOWN)lParam;
                 TCHAR  buf[64] = {0};
                 wsprintf(buf, L"iDelta: %d   iPos:%d", pNM->iDelta, pNM->iPos);
                 MessageBox(hWnd, buf, L"提示", MB_OK);
    
                 pNM->iDelta = -pNM->iDelta;//向上增加,向下减少
    
                 //return 1;//返回非0防止控件改变
            }
        }break;
    
        case WM_CREATE:
        {
         //创建伙伴控件
            HWND  hEdit = CreateWindow(WC_EDIT, L"0", WS_CHILD | WS_VISIBLE | ES_NUMBER,
                30, 30, 100, 50, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL);
    
            //创建上下控件
            HWND  hUpDown= CreateWindow(UPDOWN_CLASS , NULL, WS_CHILD | WS_VISIBLE | UDS_AUTOBUDDY| UDS_WRAP /*循环*/|UDS_ALIGNRIGHT|UDS_SETBUDDYINT,
                0, 0, 0, 0, hWnd, (HMENU)222, GetModuleHandle(NULL), NULL);
    
            //设置控件范围
            SendMessage(hUpDown, UDM_SETRANGE, 0, MAKELPARAM(1, 10));
    
            //设置当前的值
            SendMessage(hUpDown, UDM_SETPOS, 0, 6);
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    三、IP地址控件

    1)什么是IP地址控件

    2)样式

    3)向控件发送消息设置控件

    4)控件向父窗口发送消息

    5)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    //#pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == IPN_FIELDCHANGED)
            {
                 LPNMIPADDRESS   pNM = (LPNMIPADDRESS)lParam;
                 TCHAR  buf[64] = {0};
                 wsprintf(buf, L"iField  %d  ,  iValue  %d
    ", pNM->iField, pNM->iValue);
                 OutputDebugString(buf);
            }
        }break;
    
        case WM_CREATE:
        {
            //创建IP控件
            HWND   hIPAddress = CreateWindow(WC_IPADDRESS, NULL,
                WS_CHILD | WS_VISIBLE, 30, 30, 200, 30, hWnd,(HMENU)111, GetModuleHandle(NULL), NULL);
    
            //设置IP地址控件的值
            SendMessage(hIPAddress, IPM_SETADDRESS, 0, MAKEIPADDRESS(192,168,0,1));
    
            //设置每个输入的范围
            SendMessage(hIPAddress, IPM_SETRANGE, 0, MAKEIPRANGE(1,10));
            SendMessage(hIPAddress, IPM_SETRANGE, 1, MAKEIPRANGE(100, 200));
    
    
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    四、分页控件

    1)什么是分页控件

    2)样式

    3)向控件发送消息设置控件

    4)向父窗口发送消息

    5)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    //#pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    //#pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == PGN_CALCSIZE)
            {
                //设置包含窗口的大小
                LPNMPGCALCSIZE   pNM = (LPNMPGCALCSIZE)lParam;
                if (pNM->dwFlag == PGF_CALCWIDTH)//水平风格的的计算宽度,垂直的则需要计算高度
                {
                    pNM->iWidth = 600;
                    pNM->iHeight = 30;
                }
                return 0;
            }
            if (pHDR->code == PGN_SCROLL)
            {
                //通知一次滚动多少
                LPNMPGSCROLL   pNM = (LPNMPGSCROLL)lParam;
                pNM->iScroll = 500;  //修改滚动步长
            }
    
    
        }break;
    
        case WM_CREATE:
        {
            //创建分页控件
            HWND   hPage = CreateWindow(WC_PAGESCROLLER, NULL,
                WS_CHILD | WS_VISIBLE | PGS_HORZ, 30, 30, 200, 50, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL);
            
            //创建一个内容比较长的按钮 
            HWND hBtn=CreateWindow(WC_BUTTON,  L"开始,这是一个标题很长很长很长很长很长很长很长很长很长很长的按钮,现在才结束!",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0, hPage/*父窗口是分页控件*/, (HMENU)222, GetModuleHandle(NULL), NULL);
    
            //设置分页控件的子窗口
            SendMessage(hPage, PGM_SETCHILD, 0, (LPARAM)hBtn);
    
            //设置按钮箭头的宽度
            SendMessage(hPage, PGM_SETBUTTONSIZE, 0, (LPARAM) 30);
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

     五、工具条控件

    1)什么是工具条控件

    2)样式

    3)向父窗口发送消息

    4)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    #include"resource.h"
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    #pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);  
            if(code==0)
            {
                TCHAR  buf[64];
                wsprintf(buf, L"ID %d 按钮被点击!", CtrlID);
                MessageBox(hWnd, buf, L"提示", MB_OK);
            }
    
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == PGN_CALCSIZE)
            { 
                //LPNMPGCALCSIZE   pNM = (LPNMPGCALCSIZE)lParam;
             
            } 
    
    
        }break;
    
        case WM_CREATE:
        {
            //创建工具条控件
            HWND  hToolBar = CreateWindow(TOOLBARCLASSNAME, NULL,
                WS_CHILD | WS_VISIBLE, 0,0, 0, 0, hWnd, (HMENU)111,
                GetModuleHandle(NULL), NULL);
    
            //创建图像列表
            HIMAGELIST  hImageList = ImageList_Create(32, 32,  //单个位图的尺寸 
                ILC_COLOR32 | ILC_MASK /*确保透明背景*/, 3, 1);
    
            for (int i = 0; i< 3; i++)
            {
                HICON  hIcon = LoadIcon(GetModuleHandle(NULL),
                    MAKEINTRESOURCE(IDI_ICON1 + i));
                ImageList_AddIcon(hImageList, hIcon);
                DestroyIcon(hIcon);
            }
             
            //设置工具条的图片列表 
            SendMessage(hToolBar, TB_SETIMAGELIST, 0, (LPARAM)hImageList);
    
            //添加一些按钮到工具条上去
    
            TBBUTTON  btn1 , btn2; 
            btn1.fsState = TBSTATE_ENABLED;
            btn1.iString = (INT_PTR)L"按钮1";
            btn1.fsStyle = TBSTYLE_BUTTON;
            btn1.idCommand = 2000;
            btn1.iBitmap = 1;
    
            btn2.iString = (INT_PTR)L"按钮2";
            btn2.fsState = TBSTATE_ENABLED;
            btn2.fsStyle = TBSTYLE_BUTTON;
            btn2.idCommand = 3000;
            btn2.iBitmap=2;
    
            TBBUTTON arr[2] = { btn1,btn2 };
            SendMessage(hToolBar, TB_ADDBUTTONS, 2, (LPARAM)&arr);
    
            //自动调整工具条的大小并显示
           SendMessage(hToolBar, TB_AUTOSIZE, 0, 0);
           ShowWindow(hToolBar, true);
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    六、提示控件

    1)什么是提示控件

    2)样式

    3)向控件发送消息设置控件

    4)向父窗口发送消息

    5)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    //#include"resource.h"
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    //#pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);
            if (code == 0)
            {
             
            }
    
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == TTN_SHOW)
            { 
                //MessageBox(hWnd, L"提示框要出来了", L"提示" ,MB_OK);
                OutputDebugString(L"提示框要出来了
    ");
            }
             
        }break;
    
        case WM_CREATE:
        {
            //创建提示控件
            HWND  hTip = CreateWindow(TOOLTIPS_CLASS, NULL,
                WS_POPUP|TTS_BALLOON, 0, 0, 0, 0, hWnd, NULL/*传入NULL*/, GetModuleHandle(NULL), NULL);
            
            //像提示控件注册  窗口
            TOOLINFO  ti;
            ti.cbSize = sizeof(TOOLINFO);
            ti.hinst = GetModuleHandle(NULL);
            ti.hwnd = hWnd;  //指定在哪个窗口上显示tip
            ti.lpszText = L"这是一个提示控件,用于鼠标移上去之后告诉用户一些信息!";
            GetClientRect(hWnd, &ti.rect/*鼠标移到哪个区域 */); 
            ti.uFlags = TTF_SUBCLASS;
            ti.uId = (UINT_PTR)hTip; //tip的窗口句柄
    
            //注册需要tip的窗口
            SendMessage(hTip, TTM_ADDTOOL, 0, (LPARAM)&ti);
    
            //设置tip窗口的宽度
            SendMessage(hTip, TTM_SETMAXTIPWIDTH, 0, (LPARAM)100);
    
            //设置tip的显示时间(毫秒)
            SendMessage(hTip, TTM_SETDELAYTIME, TTDT_AUTOPOP,  MAKELPARAM( 10*1000, 0 ));
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    七、状态栏控件

    1)什么是状态栏控件

    2)样式

    3)向控件发送消息设置控件

    4)向父窗口发送消息

    5)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    //#include"resource.h"
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    //#pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
    
        case  WM_SIZE://窗口尺寸改变时
        {
          //获取状态栏的句柄
            HWND   hStatus=GetDlgItem(hWnd, 111);
            SendMessage(hStatus, WM_SIZE, 0, 0);//通知状态栏重绘
        }break;
    
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);
            if (code == 0)
            {
    
            }
    
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == NM_CLICK  &&  pHDR->idFrom==111)
            { 
                MessageBox(hWnd, L"状态栏被点击!", L"提示", MB_OK);
            }
    
        }break;
    
        case WM_CREATE:
        {
            //创建状态栏控件
            HWND  hStatusBar = CreateWindow(STATUSCLASSNAME, NULL,
                WS_CHILD|WS_VISIBLE, 0, 0, 0, 0, hWnd, (HMENU)111, GetModuleHandle(NULL), NULL);
             
            //设置状态栏的文本
            SendMessage(hStatusBar, SB_SETTEXT, 0, (LPARAM)L"状态栏文本");
    
            //将状态栏分区域
            int  arr[3] = { 100,200,-1}; //代表起始x坐标
            SendMessage(hStatusBar, SB_SETPARTS, 3, (LPARAM)arr );
    
            //设置其他区域状态栏的文本
            SendMessage(hStatusBar, SB_SETTEXT, 1, (LPARAM)L"第二区域");
            SendMessage(hStatusBar, SB_SETTEXT, 2, (LPARAM)L"第三区域");
    
    
            //设置状态栏的高度
            SendMessage(hStatusBar, SB_SETMINHEIGHT, 50,  0);
            SendMessage(hStatusBar, WM_SIZE, 0, 0); //让状态栏重绘
    
    
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    八、控件条控件

    1)什么是控件条控件

    2)样式

    3)代码实例

    #include<windows.h>
    #include<CommCtrl.h>  
    //#include"resource.h"
    //使用Unicode版本必须加入manifest强制让应用程序加载common Control 6.0
    #pragma comment(linker,"/manifestdependency:"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'"")  
    //#pragma comment(lib,"Comctl32.lib")
    
    
    //自定义的窗口过程
    LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
        WPARAM wParam, LPARAM lParam)
    {
        switch (Msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);//可以使GetMessage返回0    
            return 0;
    
        case  WM_COMMAND:
        {
            WORD  CtrlID = LOWORD(wParam);
            WORD   code = HIWORD(wParam);
            if (code == 0)
            {
    
            }
    
        }break;
    
        case  WM_NOTIFY:
        {
            NMHDR  * pHDR = (NMHDR  *)lParam;
            if (pHDR->code == RBN_CHILDSIZE)
            {
                LPNMREBARCHILDSIZE  pNM = (LPNMREBARCHILDSIZE)lParam;
                RECT  rcBand= pNM->rcBand;
                RECT  rcChild = pNM->rcChild; 
                TCHAR  buf[64] = { 0 };
                wsprintf(buf, L" %drcBand  %d ,%d,%d ,%d
    ", pNM->wID,rcBand.left, rcBand.top, rcBand.right, rcBand.bottom);
                OutputDebugString(buf);
    
    
            }
    
        }break;
    
        case WM_CREATE:
        {
            //创建Rebar控件
            HWND  hRebar = CreateWindow(REBARCLASSNAME, NULL,
                WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | RBS_VARHEIGHT,
                0, 0, 0, 0, hWnd, NULL, GetModuleHandle(NULL), NULL);
    
    
            //子控件
            HWND hBtn = CreateWindow(WC_BUTTON,
                L"按钮控件",
                WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                0, 0,
                0, 0,
                hRebar, //父窗口是Rebar
                (HMENU)111,
                ((LPCREATESTRUCT)lParam)->hInstance,
                NULL);
    
    
            HWND hEdit = CreateWindow(WC_EDIT,
                L"编辑控件",
                WS_CHILD | WS_VISIBLE | WS_BORDER,
                0, 0,
                0, 0,
                hRebar, //父窗口是Rebar
                (HMENU)222,
                ((LPCREATESTRUCT)lParam)->hInstance,
                NULL);
    
            //创建带区
            REBARBANDINFO  rb;
            rb.cbSize = sizeof(REBARBANDINFO);
            rb.lpText = L"控件条1";
            rb.fMask = RBBIM_ID |RBBIM_TEXT|RBBIM_STYLE |RBBIM_CHILDSIZE| RBBIM_CHILD;
            rb.fStyle = RBBS_GRIPPERALWAYS;
            rb.cxMinChild = 100;
            rb.cyMinChild = 30;
            rb.hwndChild = hBtn; 
            rb.wID = 1000;
            SendMessage(hRebar, RB_INSERTBAND, 0, (LPARAM)&rb);
    
    
            //创建带区2
            REBARBANDINFO  rb2;
            rb2.cbSize = sizeof(REBARBANDINFO);
            rb2.lpText = L"控件条2";
            rb2.fMask = RBBIM_ID | RBBIM_TEXT | RBBIM_STYLE | RBBIM_CHILDSIZE | RBBIM_CHILD;
            rb2.fStyle = RBBS_GRIPPERALWAYS;
            rb2.cxMinChild = 100;
            rb2.cyMinChild =30;
            rb2.hwndChild = hEdit;
            rb2.wID = 2000;
            SendMessage(hRebar, RB_INSERTBAND, -1, (LPARAM)&rb2);
    
    
        }break;
    
        }
    
        return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
    
    
    int  WINAPI   WinMain(HINSTANCE  hInstance, HINSTANCE  hPrevInstance,
        LPSTR lpCmdLine, int  nShowCmd)
    {
    
        //1.注册窗口类
        WNDCLASS  wnd;
        wnd.cbClsExtra = 0;
        wnd.cbWndExtra = 0;
        wnd.hbrBackground = (HBRUSH)(GetStockObject(GRAY_BRUSH));//背景色
        wnd.hCursor = LoadCursor(NULL, IDC_ARROW);//光标
        wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);//图标
                                                    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
        wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
        wnd.lpszClassName = L"MrHuang";//窗口类名
        wnd.lpszMenuName = NULL;//菜单资源名称
        wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
        wnd.hInstance = hInstance;//实例句柄
        RegisterClass(&wnd);
    
        //创建窗口(返回之前发送WM_CREATE)
        HWND    hWnd = CreateWindow(L"MrHuang", L"第一个窗口程序",
            WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
    
        //显示窗口
        ShowWindow(hWnd, nShowCmd);
        //更新窗口(发送WM_PAINT)
        UpdateWindow(hWnd);
    
    
        //消息循环(收到WM_QUIT消息退出)
        MSG  msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);//翻译消息
            DispatchMessage(&msg);//分发消息到窗口过程
        }
    }

    九、控件或子窗口操作

    ①GetDlgItem函数

    ②GetDlgItemText函数

    ③SetDlgItemText函数

    ④SendDlgItemMessage函数

  • 相关阅读:
    接口和抽象类的异同点
    实体对象间传值克隆
    什么是反射&反射的8个类的用法
    ERP中反射的两个实例
    基础02 Java 跨平台原理
    基础01 dos命令
    lists删除
    多字段 java对象排序
    Java对象排序
    MySQL表中数据的迁移
  • 原文地址:https://www.cnblogs.com/mango1997/p/14626885.html
Copyright © 2020-2023  润新知