首页 > 其他分享 >win32api添加按钮与响应

win32api添加按钮与响应

时间:2023-04-21 16:37:22浏览次数:43  
标签:hwnd TEXT messages window 添加 按钮 NULL wincl win32api

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>


#define IDB_ONE     3301  
#define IDB_TWO     3302  
#define IDB_THREE   3303


/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("firstapp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("firstapp"),       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;

		case WM_CREATE:
			//创建窗口时调用,我们也可以在这里面创建按钮
			{  
				//创建三个按钮  
				CreateWindow(TEXT("Button"), TEXT("按钮一"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,  
							35, 10, 120, 60, hwnd, (HMENU)IDB_ONE, ((LPCREATESTRUCT)lParam)->hInstance, NULL);  
				CreateWindow(TEXT("Button"), TEXT("按钮二"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,  
							35, 80, 120, 60, hwnd, (HMENU)IDB_TWO, ((LPCREATESTRUCT)lParam)->hInstance, NULL);  
				CreateWindow(TEXT("Button"), TEXT("按钮三"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,  
							35, 150, 120, 60, hwnd, (HMENU)IDB_THREE, ((LPCREATESTRUCT)lParam)->hInstance, NULL);  
			}
			break;


		case WM_COMMAND:  
		{  
			switch(LOWORD(wParam))  
			{  
				case IDB_ONE:  
					MessageBox(hwnd, TEXT("您点击了第一个按钮。"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);  
					SendMessage((HWND)lParam, WM_SETTEXT, (WPARAM)NULL, (LPARAM)TEXT("第一个按鈕已点击"));  
					break;  
				case IDB_TWO:  
					MessageBox(hwnd, TEXT("您点击了第二个按钮。"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);  
					SendMessage((HWND)lParam, WM_SETTEXT, (WPARAM)NULL, (LPARAM)TEXT("第二个按鈕已点击"));  
					break;  
				case IDB_THREE:  
					MessageBox(hwnd, TEXT("您点击了第三个按钮。"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);  
					SendMessage((HWND)lParam, WM_SETTEXT, (WPARAM)NULL, (LPARAM)TEXT("第三个按鈕已点击"));  
					break;  
				default:  
					break;  
			}
		}
		
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

  

标签:hwnd,TEXT,messages,window,添加,按钮,NULL,wincl,win32api
From: https://www.cnblogs.com/roverq/p/17340840.html

相关文章

  • 06添加身份验证
    06添加身份验证Domain.Shared添加Nugget包:Volo.Abp.PermissionManagement.Domain.SharedVolo.Abp.Identity.Domain.SharedVolo.Abp.OpenIddict.Domain.SharedDomainSharedModule.cs添加以下依赖:typeof(AbpIdentityDomainSharedModule)typeof(AbpPermissionManagementD......
  • vsCode添加插件方式
    vscode的几种安装插件方式1、联网正常的时候可以直接通过vsCode自带的工具直接搜索进行插件安装下载即可2、在有网络限制的时候,可以通过先下载的离线包进行安装插件vsCode下载离线包的地址:https://marketplace.visualstudio.com/vscode(到vscode官网,搜索想要的插件进行下......
  • 按钮
        v                   ......
  • JMeter入门教程(6) --脚本添加
    文章目录1.添加线程组2.添加HTTPCookie管理器3.添加HTTP请求默认值4.添加HTTP请求5.添加查看结果树1.添加线程组1.创建JMeter测试计划的第一步就是添加线程组测试元件。线程组会告诉JMeter需要模拟的并发用户数,以及并发用户发送请求的频率和数目。要添加线程组,首先选中测试计划,......
  • ASP.NET点击按钮回车提交web页面回车提交点击回车按钮提交
    ASP.NET回车提交事件其实说到底并不是ASP.NET的编程问题,却是关于htmlform中的submit按钮就是如何规划的具体讨论。也可归于ASP.NET编程的一部分,那么ASP.NET回车提交事件的具体实现是怎么样的呢?下面我们具体的看下:ASP.NET回车提交事件实现1、当你的光标焦点进入某个表单元素......
  • MFC-添加资源
     添加图片资源           ......
  • Mysql添加用户和设置权限的操作方法
    Mysql添加用户和设置权限的操作方法 更新时间:2022年07月28日09:42:08 作者:怪 咖@  https://www.jb51.net/article/257120.htm这篇文章主要介绍了Mysql添加用户和设置权限的操作方法,主要包括管理用户,权限控制的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具......
  • Android 导入添加图片
    1.找到要添加的图片   按住Ctrl+c先复制2.打开res的drawable文件夹 然后一定要使用右键paste进行粘贴 之后选好位置点ok即可。 3.使用:@drawabel,只要左边出现图标,说明添加没问题 设置背景图片效果:    ......
  • CentOS7使用systemctl添加自定义服务
    一、简介Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos56已经是实质差别。systemd是靠管理unit的方式来控制开机服务,开机级别等功能。在/usr/lib/systemd/system目录下包含了各种unit文件,有service后缀的服务unit,有target后缀的开机级别unit等,这里介绍关......
  • 使用scapy给pcap包添加vlan
    1、使用wireshark查看pcap文件,一个没有vlan,一个有vlan  2、使用scapy查看有vlan的报文 可以看到Ether层type=VLAN,vlan层为<Dot1Q prio=0id=0vlan=10type=IPv4 3、使用scapy编辑没有vlan的文件的第4个报文fromscapy.allimport*packets=rdpcap("gen_full_......