win32 API编程之常用控件

2021-05-16 05:27

阅读:450

标签:nap   eof   handle   rev   word   over   指定   struct   while   

一、按钮控件

二、美化按钮

三、windows资源文件.rc

 

 

 

一、按钮控件

1)注意事项

  • 创建按钮一般在WM_CREATE消息中;
  • 系统注册的按钮类名是“button”,不区分大小写。
  • WS_CHILD:控件是放在我们的窗口上的,自然要作为窗口的子窗口
  • WS_CHILDWINDOW也一样,为了节约几个字母,用WS_CHILD吧。
  • WS_VISIBLE:设置可见。
  • 创建窗口函数讲解

HWND WINAPI CreateWindow( 类名, 显示内容,  风格,  nt x,   int y,   
                int nWidth,  int nHeight,  父窗口,  控件ID,  
                HINSTANCE hInstance,  LPVOID lpParam  );

2)按钮样式

技术图片

3)获取单选按钮与复选按钮的状态

技术图片

4)代码实例

#include 
#include//Button_GetCheck的头文件
#include//自定义的窗口过程
LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
    WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);//可以使GetMessage返回0    
        return 0;

    case  WM_CREATE:
    {
        LPCREATESTRUCT   pcs = (LPCREATESTRUCT)lParam;

        //创建控件或者子窗口

        //普通按钮BS_PUSHBUTTON
        HWND  hBtn=CreateWindow(L"button", L"普通按钮", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON ,30, 30, 80, 30, hWnd,
            (HMENU)1000,//控件ID
            pcs->hInstance, NULL); 

        //单选按钮
        HWND  hBtn1 = CreateWindow(L"button", L"单选按钮", WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 30, 70, 80, 30, hWnd,
            (HMENU)1001,//控件ID
            pcs->hInstance, NULL);

        //复选按钮
        HWND  hBtn2 = CreateWindow(L"button", L"复选按钮", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 30, 110, 80, 30, hWnd,
            (HMENU)1002,//控件ID
            pcs->hInstance, NULL);


        return 0;
      }


    case   WM_COMMAND:
    {
         WORD  id = LOWORD(wParam);
         WORD  code= HIWORD(wParam);
         HWND   hCtrl = (HWND)lParam;
         if (id == 1000 &&  code ==  BN_CLICKED)
         {
             MessageBox(hWnd, L"普通按钮被单击!", L"提示",MB_OK); 
         }
         else if (id == 1001)
         {
             MessageBox(hWnd, L"单选按钮被单击!", L"提示", MB_OK);
 
             if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
             {
                 MessageBox(hWnd, L"被选中!", L"提示", MB_OK); 
             }
             else  if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
             {
                 MessageBox(hWnd, L"没选中!", L"提示", MB_OK);
             }
         }
         else if (id == 1002)
         {
             MessageBox(hWnd, L"复选按钮被单击!", L"提示", MB_OK);
/*
             if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_CHECKED)
             {
                 MessageBox(hWnd, L"被选中!", L"提示", MB_OK);
             }
             else  if (SendMessage(hCtrl, BM_GETCHECK, 0, 0) == BST_UNCHECKED)
             {
                 MessageBox(hWnd, L"没选中!", L"提示", MB_OK);
             }
             */
              

             //简写形式((int)(DWORD)SNDMSG((hwndCtl), BM_GETCHECK, 0L, 0L))
             if (Button_GetCheck(hCtrl) == BST_CHECKED)
             {
                 MessageBox(hWnd, L"被选中!", L"提示", MB_OK);
             }
             else  if (Button_GetCheck(hCtrl) == BST_UNCHECKED)
             {
                 MessageBox(hWnd, L"没选中!", L"提示", MB_OK);
             }
         }
    }break;



     
     default:
         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, 300, 300, 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);//分发消息到窗口过程
    }
}

二、美化按钮

技术图片

代码实例

#include  
#include//自定义的窗口过程
LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
    WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);//可以使GetMessage返回0    
        return 0;

    case  WM_CREATE:
    {
        LPCREATESTRUCT   pcs = (LPCREATESTRUCT)lParam; 

        //创建位图按钮
        HWND   hBtn = CreateWindow(L"BuTtOn" /*不区分大小写*/, 
            L"XXX", WS_CHILD | WS_VISIBLE | BS_BITMAP,//指定为图片
            30, 30, 217, 46, hWnd, (HMENU)1000, pcs->hInstance, NULL);

        //加载位图
        HBITMAP  hBmp=(HBITMAP)LoadImage(NULL, L"C:\\Users\\35497\\Desktop\\code\\016\\Debug\\btn1.bmp",
            IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

        //向控件发送BM_SETIMAGE消息设置按钮图片
        LRESULT  ret=SendMessage(hBtn, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);


        return 0;
    }


    case   WM_COMMAND:
    {
        WORD  id = LOWORD(wParam);
        WORD  code = HIWORD(wParam);
        HWND   hCtrl = (HWND)lParam;
        if (id == 1000 && code == BN_CLICKED)
        {
            MessageBox(hWnd, L"普通按钮被单击!", L"提示", MB_OK);
        } 
    }break; 

    default:
        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, 300, 300, 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);//分发消息到窗口过程
    }
}

三、windows资源文件.rc

1)创建步骤

技术图片

四、资源相关函数

1)相关函数

技术图片

2)LoadBitmap函数

技术图片

3)LoadCursor函数

技术图片

4)代码实例

#include  
#include
#include"resource.h"

//自定义的窗口过程
LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
    WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);//可以使GetMessage返回0    
        return 0;

    case  WM_CREATE:
    {
        LPCREATESTRUCT   pcs = (LPCREATESTRUCT)lParam; 

        //创建位图按钮
        HWND   hBtn = CreateWindow(L"BuTtOn" /*不区分大小写*/, 
            L"XXX", WS_CHILD | WS_VISIBLE | BS_BITMAP,//指定为图片
            30, 30, 217, 46, hWnd, (HMENU)1000, pcs->hInstance, NULL);

        //LoadImage加载位图
        //HBITMAP  hBmp=(HBITMAP)LoadImage(NULL, L"C:\\Users\\35497\\Desktop\\code\\016\\Debug\\btn1.bmp",
        //    IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);


        //采用LoadBitmap加载位图资源(必须在rc文件中定义)
        HBITMAP  hBmp= LoadBitmap(pcs->hInstance, MAKEINTRESOURCE(IDB_BITMAP1) );

        //向控件发送BM_SETIMAGE消息设置按钮图片
        LRESULT  ret=SendMessage(hBtn, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);


        return 0;
    }


    case   WM_COMMAND:
    {
        WORD  id = LOWORD(wParam);
        WORD  code = HIWORD(wParam);
        HWND   hCtrl = (HWND)lParam;
        if (id == 1000 && code == BN_CLICKED)
        {
            MessageBox(hWnd, L"普通按钮被单击!", L"提示", MB_OK);
        } 
    }break; 

    default:
        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));//背景色
      //加载系统光标,第一个参数为NULL
    //wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
    //加载资源中的光标
    wnd.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR2));


     //加载系统图标,第一个参数为NULL
    //wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    //加载资源中的图标
    wnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON2));


    //wnd.lpfnWndProc = DefWindowProc;//默认窗口过程函数,用于处理消息
    wnd.lpfnWndProc = MyWindowProc;//自定义的窗口过程函数
    wnd.lpszClassName = L"MrHuang";//窗口类名
    wnd.lpszMenuName = NULL;//菜单资源名称
    wnd.style = CS_HREDRAW | CS_DBLCLKS;//窗口类、样式
    wnd.hInstance = hInstance;//实例句柄
    RegisterClass(&wnd);


    //加载字符串资源
    TCHAR  buf[64] = {0};
    LoadString(hInstance, IDS_STRING1, buf, sizeof(buf));
    LoadString(GetModuleHandle(NULL), IDS_STRING2, buf, sizeof(buf));

    //创建窗口(返回之前发送WM_CREATE)
    HWND    hWnd = CreateWindow(L"MrHuang", buf,
        WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, 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)设置文本内容SetWindowText

技术图片

4)可以向静态文本框发送消息来设置图标、位图等

技术图片

5)代码实例

#include 
#include"resource.h"  
#include//自定义的窗口过程
LRESULT  CALLBACK    MyWindowProc(HWND hWnd, UINT Msg,
    WPARAM wParam, LPARAM lParam)
{
    switch (Msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);//可以使GetMessage返回0    
        return 0;

    case  WM_CREATE:
    {
        LPCREATESTRUCT   pcs = (LPCREATESTRUCT)lParam; 

        //静态文本控件
     HWND  hStatic=    CreateWindow(L"Static", L"这是一个静态文本", WS_CHILD | WS_VISIBLE |WS_BORDER|SS_BITMAP|SS_NOTIFY,
            30, 30,200, 30, hWnd, (HMENU)111, pcs->hInstance, NULL);
     //更改窗口标题
     SetWindowText(hWnd, L"新窗口标题");
     //设置控件(子窗口)内容
     SetWindowText(hStatic, L"更改后的内容!");


     //加载位图
    HBITMAP  hBmp= LoadBitmap(pcs->hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
    
    //向控件发送STM_SETIMAGE消息设置静态控件的位图
    SendMessage(hStatic, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);




        return 0;
    }


    case   WM_COMMAND:
    {

        WORD  id = LOWORD(wParam);//控件ID
        WORD  code = HIWORD(wParam);//消息类型
        HWND   hCtrl = (HWND)lParam;//窗口句柄

        //父窗口接收到子控件发的消息
        if (id == 111  &&  code ==  STN_CLICKED)
        {
            MessageBox(NULL, L"静态文本被点击!", L"提示", MB_OK);
        }
      
    }break;




    default:
        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, 300, 300, 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);//分发消息到窗口过程
    }
}

 

win32 API编程之常用控件

标签:nap   eof   handle   rev   word   over   指定   struct   while   

原文地址:https://www.cnblogs.com/mango1997/p/14610061.html


评论


亲,登录后才可以留言!