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);
}