windows窗体实例(C++)

来源:互联网 发布:2016最新的幸运28源码 编辑:程序博客网 时间:2024/05/20 18:40
/*头文件*/
#include <windows.h>
#include <stdio.h>

/***********************************
*功能:windows窗体实例
*winMain
************************************/


/*全局变量*/
HINSTANCE hinst;
/*函数声明*/
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
/**windows应用实例*/

int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPtevInstance,
LPSTR lpComLine,
int nComShow
      )

{



/******************************Windosw窗口****************************************************/
WNDCLASSEX wcx;//窗口类
HWND hwnd; //窗口句柄
MSG msg; //消息
BOOL fGotMessage;//是否成功获取消息
hinst = hInstance;//应用程序实例句柄,保存为全局变量


//填充窗口类的数控结构
wcx.cbSize = sizeof(wcx);//结构体的大小
wcx.style = CS_HREDRAW | CS_VREDRAW;//样式:大小改变时重绘界面
wcx.lpfnWndProc = MainWndProc;//窗口消息处理函数
wcx.cbClsExtra = 0;//不使用类内存
wcx.cbWndExtra = 0;//不使用窗口内存
wcx.hInstance = hInstance;//所属的应用程序实例句柄
wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION);//图标: 默认
wcx.hCursor = LoadIcon(NULL,IDC_ARROW);//光标: 默认
//wcx.hbrBackground =  (HBRUSH)GetStockObject(WHITE_BRUSH);//背景: 白色 
wcx.hbrBackground = CreateSolidBrush(RGB(255,255,0));     //自定义背景颜色
wcx.lpszMenuName = NULL;//菜单:不使用
wcx.lpszClassName = "MainWClass";   //窗口类名
wcx.hIconSm = (HICON)LoadImage(hInstance,//小图标
MAKEINTRESOURCE(5),
IMAGE_ICON,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CXSMICON),
LR_DEFAULTCOLOR);


//注册窗口类
if(!RegisterClassEx(&wcx))
{
return 1;
}


//创建窗口
hwnd = CreateWindow(
"MainWClass", //窗口名
"Window编程", //窗口标题
WS_OVERLAPPEDWINDOW,//窗口样式
CW_USEDEFAULT,//水平位置X:默认
CW_USEDEFAULT,//垂直位置Y:默认
CW_USEDEFAULT,//宽度:默认
CW_USEDEFAULT,  //高度:默认
(HWND) NULL, //父窗口:无
(HMENU) NULL, //菜单:使用窗口类的菜单
hInstance, //应用程序实例句柄
(LPVOID) NULL );//窗口创建时数据;无
 


if(!hwnd)
{
return 1;
}
//显示窗口
ShowWindow(hwnd,nComShow);
UpdateWindow(hwnd);
while((fGotMessage = GetMessage(&msg,(HWND)NULL,0,0))!=0 && fGotMessage!=-1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
/******************************Windosw窗口*******************************************************/


//return 0;
}


/****************************************************************************************************
*MainWndPorc 
*功能 窗口消息处理函数,对所有的消息都是用默认处理函数
*****************************************************************************************************/
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg,WPARAM wParam,LPARAM lParam )
{
switch(uMsg)
{
case WM_DESTROY:
ExitThread(0);
   return 0;
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);


}
}

0 0