C++ 窗体

Wesley13
• 阅读 669

1、引用

#include <stdlib.h>
//#include <stdio.h>
#include<windows.h>
#include<stdio.h>
#include<shellapi.h>
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32.lib")

2.创建类,句柄

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wp,LPARAM lp);

HWND bhwnd;
//程序入口函数
int WINAPI WinMain(
HINSTANCE hInstance, //handle to current instance
HINSTANCE hPrevInstance,//handle to previous instance
LPSTR lpCmdLine, //command line
int nCmdShow //show state
)
{
//设计一个窗口类
WNDCLASS wndcls;
wndcls.cbClsExtra =0;
wndcls.cbWndExtra =0;
//wndcls.hbrBackground =(HBRUSH)GetStockObject(BLACK_BRUSH);
wndcls.hCursor =LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance =hInstance; //应用程序实例句柄由WinMain函数传进来
wndcls.lpfnWndProc =WndProc;//WinSunProc; // 传入回调函数地址
wndcls.lpszClassName="mywindow";
wndcls.lpszMenuName =NULL;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
RegisterClass(&wndcls); //注册窗口类
//创建窗口,定义一个变量用来保存成功创建窗口后返回的句柄
HWND hwnd;
hwnd=CreateWindow("mywindow","my first application",
WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);
//显示及刷新窗口
//bhwnd=CreateWindow("Button","MyButton",WS_CHILDWINDOW,100,100,100,100,hwnd,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
//Add Btn
bhwnd=CreateWindow("Button","MyButton",WS_CHILDWINDOW,100,100,100,100,hwnd,(HMENU)OK,hInstance,NULL);
ShowWindow(bhwnd,SW_SHOWNORMAL);
//ShellExecute((hwnd)::AfxGetMainWnd(),"open", "http://www.baidu.com/",NULL,NULL, SW_NORMAL);
//定义消息结构体,开始消息循环
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg); //将虚拟键消息转换为字符消息
DispatchMessage(&msg); //把消息传递给操作系统
}
return msg.wParam;
}

#define BUTTON_ID 10011 //按钮ID
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wp,LPARAM lp)
{
HDC hdc;
PAINTSTRUCT ps;
static HWND btnWnd;
static bool flag=false;
switch(message)
{
case WM_CREATE:btnWnd =CreateWindow("Button","按钮",WS_VISIBLE|WS_CHILD,
10,10,150,120,hwnd,(HMENU)BUTTON_ID,0,0);break; //倒数第3个为按钮子窗口ID
case WM_PAINT:break;
case WM_COMMAND: //按钮消息WM_COMMAND来处理
switch(LOWORD(wp)) //wParam的低字节为点击的按钮的ID
{ //下面随便写点什么啦!
case BUTTON_ID:
//SetWindowText(btnWnd,"再点我试试?");
if(flag==true)
{
SendMessage(btnWnd,WM_SETTEXT,0, LPARAM("干嘛点我?"));
}
else
{
SendMessage(btnWnd,WM_SETTEXT,0, LPARAM("再点试试?"));
MessageBox(hwnd,"HHHHHHH","message",0);
//SetWindowText(btnWnd,(LPCSTR)"点点");
}
flag = !flag;
//MessageBox(hwnd,"你点我干什么?", "点我啦!",MB_OK);
break;
default:break;
}
break;
case WM_DESTROY:PostQuitMessage(0);return 0;
default:break;
}
return DefWindowProc(hwnd,message,wp,lp);
}

点赞
收藏
评论区
推荐文章
Stella981 Stella981
3年前
Pthreads 信号量,路障,条件变量
▶使用信号量来进行线程间信息传递●代码1include<stdio.h2include<pthread.h3include<semaphore.h4pragmacomment(lib,"pthreadVC2.lib")56constint
Wesley13 Wesley13
3年前
C++获取本机IP地址信息
include<winsock2.hinclude<iostreaminclude<stringusingnamespacestd;pragmacomment(lib,"WS2_32.lib")stringgetIP(){WSADA
Wesley13 Wesley13
3年前
TCP的粘包问题以及数据的无边界性
服务端:include <stdio.hinclude <winsock2.hpragma comment (lib, "ws2_32.lib")  //加载 ws2_32.dll define BUF_SIZE 100 int main(int argc, char ar
Wesley13 Wesley13
3年前
C语言学生宿舍管理系统代码(可运行)
include<stdio.hinclude<stdlib.hinclude<conio.hinclude<string.hinclude<windows.hinclude<malloc.hinclude<dos.hdefinelensizeof(structdorminfo)
Wesley13 Wesley13
3年前
VC++实现进程冻结与解冻
冻结进程include<stdio.hinclude<windows.hinclude<tlhelp32.hintmain(intargc,char\argv\\){ PROCESSENTRY32pe32; pe32.dwSizesizeof(pe32); unsigned
Wesley13 Wesley13
3年前
C语言使用信号量机制实例:
C语言使用信号量机制实例:include<signal.hinclude<unistd.hinclude<stdio.hinclude<sys/types.hvoidsigroutine(intsigno){switch(signo){
Stella981 Stella981
3年前
C语言 单链表 Singly Linked List
include<stdio.hinclude<stdlib.hdefineboolintdefinetrue1definefalse0structList{  structNode\ptrNode;  intnLen;};structNode{
Stella981 Stella981
3年前
C语言 二叉树 BinaryTree
include<stdio.hinclude<stdlib.hdefinebool   intdefinefalse   0definetrue      1structNode{   structNodeleftNode;  
Stella981 Stella981
3年前
C++播放wav音乐和音效
1\. include<mmsystem.hpragmacomment(lib,"winmm.lib")PlaySound(TEXT("c:\\\\temp\\\\up.wav"),NULL,SND\_FILENAME|SND\_ASYNC|SND\_LOOP);wav文件在播放前将被装入内存,所
Wesley13 Wesley13
3年前
vc++实现注册表自启动,木马用的比较多
include<stdio.hinclude<windows.hintmain(void){charregname\\"Software//Microsoft//Windows//CurrentVersion//Run";HKEYhkResult;intretRegOpenKey(HKEY\_L