C++如何创造按钮

打开dev-cpp,选择新建——项目,看到下面的对话框

 选择Basic——Window Application,你会看到已经写好了一些代码:

#include <windows.h>
 
/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		
		/* Upon destruction, tell the main thread to stop */
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}
 
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG msg; /* A temporary location for all messages */
 
	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
 
	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
 
	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInstance,NULL);
 
	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
 
	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produce unreasonably high CPU usage
	*/
	while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&msg); /* Translate key codes to chars if present */
		DispatchMessage(&msg); /* Send it to WndProc */
	}
	return msg.wParam;
}

其中,WINAPI WINMAIN是主程序,里面已经写好了创建子窗口,所以你不用管它

LRESULT CALLBACK WndProc是创建东西,获取信息的函数

在创建指令(WM_CREATE)下可以用CreateWindow创建

顺便讲一下CreateWindow的用法

CreateWindow函数原型:

CreateWindow(lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam)

lpClassName:一个已经被注册的窗口类,常用的窗口类有:button(按钮),static(文本框)

IpWindowName:创建时它要显示的文字,以后可以用SetWindowText改

dwStyle:窗口样式,常用的有:WS_CHILD(子窗口)和WS_VISIBLE(可见)

x:创建的东西的左上角的x坐标

y:创建的东西的左上角的y坐标

nWidth:创建的东西的宽度

nHeight:创建的东西的高度

hWndParent:主窗口的句柄,填WndProc函数的第一个参数就可以了

hMenu:将要创建的东西的编号(是唯一的,不同的东西不能相同)

hInstance:主程序的句柄,填主程序(WINAPI WinMain)的第一个参数(hInstance)就可以了

lpParam:附加的参数,一般写成NULL

在信息指令(WM_COMMAND)中,可以根据按钮的编号来指定要做的动作

然后把WM_QUIT删掉,不然可能会闪退
 

int num=0;
HWND h1,h2,h3,h4,h5,h6,h7,h8,h9,h0,hok,hx,hb,h;
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam){
	switch(Message){
		case WM_CREATE:{
			h1=CreateWindow("button","1",WS_CHILD|WS_VISIBLE,
							0,0,50,50,hwnd,(HMENU)1,hInst,NULL);
			h2=CreateWindow("button","2",WS_CHILD|WS_VISIBLE,
							50,0,50,50,hwnd,(HMENU)2,hInst,NULL);
			h3=CreateWindow("button","3",WS_CHILD|WS_VISIBLE,
							100,0,50,50,hwnd,(HMENU)3,hInst,NULL);
			h4=CreateWindow("button","4",WS_CHILD|WS_VISIBLE,
							0,50,50,50,hwnd,(HMENU)4,hInst,NULL);
			h5=CreateWindow("button","5",WS_CHILD|WS_VISIBLE,
							50,50,50,50,hwnd,(HMENU)5,hInst,NULL);
			h6=CreateWindow("button","6",WS_CHILD|WS_VISIBLE,
							100,50,50,50,hwnd,(HMENU)6,hInst,NULL);
			h7=CreateWindow("button","7",WS_CHILD|WS_VISIBLE,
							0,100,50,50,hwnd,(HMENU)7,hInst,NULL);
		    h8=CreateWindow("button","8",WS_CHILD|WS_VISIBLE,
							50,100,50,50,hwnd,(HMENU)8,hInst,NULL);
			h9=CreateWindow("button","9",WS_CHILD|WS_VISIBLE,
							100,100,50,50,hwnd,(HMENU)9,hInst,NULL);
			h0=CreateWindow("button","0",WS_CHILD|WS_VISIBLE,
							50,150,50,50,hwnd,(HMENU)0,hInst,NULL);
			hok=CreateWindow("button","完成",WS_CHILD|WS_VISIBLE,
							30,225,75,20,hwnd,(HMENU)10,hInst,NULL);
			hx=CreateWindow("static","0",WS_CHILD|WS_VISIBLE,
							0,300,300,30,hwnd,(HMENU)11,hInst,NULL);
			hb=CreateWindow("button",">",WS_CHILD|WS_VISIBLE,
							100,150,50,50,hwnd,(HMENU)12,hInst,NULL);
			h=CreateWindow("static","0.0s",WS_CHILD|WS_VISIBLE,
							0,350,200,50,hwnd,(HMENU)13,hInst,NULL);
			break;
		}
		case WM_COMMAND:{
			switch(wParam){
				case 1:{
					num*=10;
					num+=1;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 2:{
					num*=10;
					num+=2;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 3:{
					num*=10;
					num+=3;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 4:{
					num*=10;
					num+=4;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 5:{
					num*=10;
					num+=5;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 6:{
					num*=10;
					num+=6;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 7:{
					num*=10;
					num+=7;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 8:{
					num*=10;
					num+=8;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 9:{
					num*=10;
					num+=9;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 0:{
					num*=10;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 12:{
					num/=10;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 10:{
					ShowWindow(h0,SW_HIDE);
					ShowWindow(h1,SW_HIDE);
					ShowWindow(h2,SW_HIDE);
					ShowWindow(h3,SW_HIDE);
					ShowWindow(h4,SW_HIDE);
					ShowWindow(h5,SW_HIDE);
					ShowWindow(h6,SW_HIDE);
					ShowWindow(h7,SW_HIDE);
					ShowWindow(h8,SW_HIDE);
					ShowWindow(h9,SW_HIDE);
					ShowWindow(hok,SW_HIDE);
					ShowWindow(hb,SW_HIDE);
					MoveWindow(hx,0,0,90,30,TRUE);
					MessageBox(GetConsoleWindow(),"按确定继续","输入数字",MB_OK);
					break;
				}
				default:{
					num=0;
					DefWindowProc(hwnd,Message,wParam,lParam);
					break;
				}
			}
			break;
		}
		default:{
			DefWindowProc(hwnd,Message,wParam,lParam);
			break;
		}
	}
}

可以直接复制,最后别忘了在主程序中加上

hInst=hInstance;

 头文件和名字空间也要加上

#include<bits/stdc++.h>
using namespace std;

最后加上可复制的样例代码

#include <windows.h>
#include <bits/stdc++.h>
using namespace std;
/* This is where all the input to the window goes to */
int num=0;
HWND h1,h2,h3,h4,h5,h6,h7,h8,h9,h0,hok,hx,hb,h;
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND hwnd,UINT Message,WPARAM wParam,LPARAM lParam){
	switch(Message){
		case WM_CREATE:{
			h1=CreateWindow("button","1",WS_CHILD|WS_VISIBLE,
							0,0,50,50,hwnd,(HMENU)1,hInst,NULL);
			h2=CreateWindow("button","2",WS_CHILD|WS_VISIBLE,
							50,0,50,50,hwnd,(HMENU)2,hInst,NULL);
			h3=CreateWindow("button","3",WS_CHILD|WS_VISIBLE,
							100,0,50,50,hwnd,(HMENU)3,hInst,NULL);
			h4=CreateWindow("button","4",WS_CHILD|WS_VISIBLE,
							0,50,50,50,hwnd,(HMENU)4,hInst,NULL);
			h5=CreateWindow("button","5",WS_CHILD|WS_VISIBLE,
							50,50,50,50,hwnd,(HMENU)5,hInst,NULL);
			h6=CreateWindow("button","6",WS_CHILD|WS_VISIBLE,
							100,50,50,50,hwnd,(HMENU)6,hInst,NULL);
			h7=CreateWindow("button","7",WS_CHILD|WS_VISIBLE,
							0,100,50,50,hwnd,(HMENU)7,hInst,NULL);
		    h8=CreateWindow("button","8",WS_CHILD|WS_VISIBLE,
							50,100,50,50,hwnd,(HMENU)8,hInst,NULL);
			h9=CreateWindow("button","9",WS_CHILD|WS_VISIBLE,
							100,100,50,50,hwnd,(HMENU)9,hInst,NULL);
			h0=CreateWindow("button","0",WS_CHILD|WS_VISIBLE,
							50,150,50,50,hwnd,(HMENU)0,hInst,NULL);
			hok=CreateWindow("button","完成",WS_CHILD|WS_VISIBLE,
							30,225,75,20,hwnd,(HMENU)10,hInst,NULL);
			hx=CreateWindow("static","0",WS_CHILD|WS_VISIBLE,
							0,300,300,30,hwnd,(HMENU)11,hInst,NULL);
			hb=CreateWindow("button",">",WS_CHILD|WS_VISIBLE,
							100,150,50,50,hwnd,(HMENU)12,hInst,NULL);
			h=CreateWindow("static","0.0s",WS_CHILD|WS_VISIBLE,
							0,350,200,50,hwnd,(HMENU)13,hInst,NULL);
			break;
		}
		case WM_COMMAND:{
			switch(wParam){
				case 1:{
					num*=10;
					num+=1;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 2:{
					num*=10;
					num+=2;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 3:{
					num*=10;
					num+=3;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 4:{
					num*=10;
					num+=4;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 5:{
					num*=10;
					num+=5;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 6:{
					num*=10;
					num+=6;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 7:{
					num*=10;
					num+=7;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 8:{
					num*=10;
					num+=8;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 9:{
					num*=10;
					num+=9;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 0:{
					num*=10;
					if(num>=1000000) num=999999;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 12:{
					num/=10;
					stringstream ss;
					ss<<num;
					SetWindowText(hx,ss.str().c_str());
					break;
				}
				case 10:{
					ShowWindow(h0,SW_HIDE);
					ShowWindow(h1,SW_HIDE);
					ShowWindow(h2,SW_HIDE);
					ShowWindow(h3,SW_HIDE);
					ShowWindow(h4,SW_HIDE);
					ShowWindow(h5,SW_HIDE);
					ShowWindow(h6,SW_HIDE);
					ShowWindow(h7,SW_HIDE);
					ShowWindow(h8,SW_HIDE);
					ShowWindow(h9,SW_HIDE);
					ShowWindow(hok,SW_HIDE);
					ShowWindow(hb,SW_HIDE);
					MoveWindow(hx,0,0,90,30,TRUE);
					MessageBox(GetConsoleWindow(),"按确定继续","输入数字",MB_OK);
					break;
				}
				default:{
					num=0;
					DefWindowProc(hwnd,Message,wParam,lParam);
					break;
				}
			}
			break;
		}
		default:{
			DefWindowProc(hwnd,Message,wParam,lParam);
			break;
		}
	}
}
 
/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	hInst=hInstance; 
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG msg; /* A temporary location for all messages */
 
	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
 
	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
 
	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInstance,NULL);
 
	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}
 
	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produce unreasonably high CPU usage
	*/
	while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&msg); /* Translate key codes to chars if present */
		DispatchMessage(&msg); /* Send it to WndProc */
	}
	return msg.wParam;
}

本文章借鉴了一点Windows的操作系统和我孙子的课程,转载请点赞

若点赞我祝您大吉大利今晚吃鸡

本文为转载

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值