简单窗口

#include<windows.h>
#include<stdio.h>

//消息处理函数
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrecInstance,PSTR szCmdLine,int iCmdShow)
{
 //int cxScreen,cyScreen;
 //cxScreen = GetSystemMetrics(SM_CXSCREEN);
 //cyScreen = GetSystemMetrics(SM_CYSCREEN);
 //TCHAR buff[1024];
 //wsprintf(buff,"The screen is %i,%i",&cxScreen,&cyScreen);
 //   句柄 输出内容 窗口标题 按键...
 //MessageBox(0,buff,TEXT("ScreenSize"), MB_YESNO);
 //MB_DEFBUTTON1 | MB_ICONHAND | MB_ICONINFORMATION);

 static TCHAR szAppName[] = TEXT("Hello Win");
 HWND hwnd;
 MSG  msg;
 WNDCLASS wndclass;

 wndclass.style = CS_HREDRAW | CS_VREDRAW;
 wndclass.lpfnWndProc = WndProc;
 wndclass.cbClsExtra = 0;
 wndclass.cbWndExtra = 0;
 wndclass.hInstance = hInstance;
 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
 wndclass.lpszMenuName = 0;
 wndclass.lpszClassName = szAppName;
 

 //注册窗口
 if(!RegisterClass(&wndclass))
 {
  MessageBox(NULL,TEXT("ERROR AT RegisterClass"),TEXT("ERROR"),0);
  return 0;
 }
 
 //创建窗口+
 hwnd = CreateWindow(
 szAppName,  //WINDOW CLASS NAME 
 TEXT("The Hello Program"), //WINDOW CAPTION
 0,//WS_OVERLAPPEDWINDOW,  //window style
 CW_USEDEFAULT, //initial X position
 CW_USEDEFAULT, //initial y position
 CW_USEDEFAULT, //initial X size
 CW_USEDEFAULT, //initial y size
 0,    //parent window handle
 0,    //window menu handle
 hInstance,  //Program instance handle
 0    //Creation parameters
 );
 

 ShowWindow(hwnd, iCmdShow);
 UpdateWindow(hwnd);

 while(GetMessage(&msg, 0, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return msg.wParam;
 return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 HDC hdc;
 PAINTSTRUCT ps;
 RECT rect;

 switch(message)
 {
 case WM_CREATE:
  return 0;
 case WM_PAINT:
  hdc = BeginPaint(hwnd, &ps);

  GetClientRect(hwnd, &rect);

  DrawText(hdc, TEXT("Hello, Windows SDK!"), -1, &rect,
  DT_SINGLELINE | DT_CENTER | DT_VCENTER 
  );

  EndPaint(hwnd, &ps);
  return 0;
 case WM_DESTROY:
  PostQuitMessage(0);
  return 0;
 default:
  break;
 }
 return DefWindowProc(hwnd, message, wParam, lParam);
}










/****************************************************************************************************************************************/

#include <Windows.h>
#include <cstdio>
#include <cstdlib>


LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
bool InitApplication(HINSTANCE hinstance);
bool CreateWindowlication(HINSTANCE hinstance , int nCmdShow);
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//MessageBox(0, TEXT("11"), TEXT("22"), 0);
InitApplication(hinstance);
CreateWindowlication(hinstance, nCmdShow);
MSG  msg;
//从消息队列中获取一个消息
while(GetMessage(&msg, 0, 0, 0))
{
//系统转换消息
TranslateMessage(&msg);
//转发到消息处理函数
DispatchMessage(&msg);
}
return 0;
}


bool InitApplication(HINSTANCE hinstance)
{
WNDCLASSEX wcx;
//结构大小
wcx.cbSize = sizeof(WNDCLASSEX);
//风格
wcx.style = CS_HREDRAW | CS_VREDRAW;
//窗口消息处理函数
wcx.lpfnWndProc = MainWndProc;
//无附加窗口类内存
wcx.cbClsExtra = 0;
//无附加窗口内存
wcx.cbWndExtra = 0;
//应用程序实例
wcx.hInstance = hinstance;
//图标
wcx.hIcon = LoadIcon(0, IDI_APPLICATION);
//鼠标指针
wcx.hCursor = LoadCursor(0, IDC_ARROW);
//背景画刷
wcx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
//菜单资源
wcx.lpszMenuName = 0;
//窗口类名
wcx.lpszClassName = "MainWClass";
//小图标
wcx.hIconSm = (HICON)LoadImage(hinstance, 0, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CXSMICON), LR_DEFAULTCOLOR);
//注册窗口
return RegisterClassEx(&wcx);
}
bool CreateWindowlication(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
RECT rect;
hwnd = CreateWindow(
//窗口类名
"MainWClass",
//窗口名
"WName",
//窗口样式
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_POPUPWINDOW,
//水平位置
CW_USEDEFAULT,
//垂直位置
CW_USEDEFAULT,
//宽
800,
//高
600,
//父句柄
0,
//菜单句柄
LoadMenu(hinstance, 0),
//应用程序实例
hinstance,
//无窗口创建数据
0
);
if(!hwnd)
{
return false;
}
//显示主窗口
ShowWindow(hwnd, nCmdShow);
//更新窗口
UpdateWindow(hwnd);
return true;
}
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
//窗口创建消息
case WM_CREATE:
//窗口绘制消息
case WM_PAINT:
//窗口大小改变消息
case WM_SIZE:
//子窗口被操作
case WM_NOTIFY:
//窗口销毁
case WM_DESTROY:
//命令行输入,菜单项被选中,或按钮被单击
case WM_COMMAND:
//窗口关闭消息
case WM_CLOSE:
//窗口被移动
case WM_MOVING:
//用户在窗口用单击左键
case WM_NCLBUTTONDOWN:
//鼠标在窗口客户区徘徊
case WM_MOUSEHOVER:
break;
default:
break;
}
//Windows默认消息处理
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值