您的位置:首页 > 其它

MFC 基础(实例)

2011-11-18 15:28 246 查看
#include <windows.h>

#include <stdio.h>

LRESULT CALLBACK WinSunProc(//名字可以更改。参数类型不能变

HWND hwnd, // handle to window 窗口句柄

UINT uMsg, // message identifier

WPARAM wParam, // first message parameter 消息参数

LPARAM lParam // second message parameter

);

/*

int WINAPI WinMain(

HINSTANCE hInstance, // handle to current instance

HINSTANCE hPrevInstance, // handle to previous instance

LPSTR lpCmdLine, // pointer to command line

int nCmdShow // show state of window

);

*/

int WINAPI WinMain(//Windows程序的入口函数

HINSTANCE hInstance, // handle to current instance 当前实例句柄

HINSTANCE hPrevInstance, // handle to previous instance 前次实例句柄

LPSTR lpCmdLine, // command line 命令行参数

int nCmdShow // show state 显示方式

)

{

/*

创建一个完整的窗口需要经过下面四个操作步骤:

1、设计一个窗口类;

2、注册窗口类;

3、创建窗口;

4、显示及更新窗口。

typedef struct _WNDCLASS {

UINT style; //类的类型

WNDPROC lpfnWndProc; //longpointfunction函数指针

int cbClsExtra; //类的附加内存

int cbWndExtra; //窗口附加内存

HANDLE hInstance; //实例号(句柄)

HICON hIcon; //图标句柄

HCURSOR hCursor; //光标句柄

HBRUSH hbrBackground; //画刷句柄

LPCTSTR lpszMenuName; //菜单的名字

LPCTSTR lpszClassName; //类的名字

} WNDCLASS;

*/

//1、设计一个窗口类;

WNDCLASS wndcls;//窗口类

wndcls.style=CS_HREDRAW | CS_VREDRAW;//重画垂直方向、水平方向

wndcls.lpfnWndProc=WinSunProc;//函数地址

wndcls.cbClsExtra=0;//类的附加内存

wndcls.cbWndExtra=0;//窗口的附加内存

wndcls.hInstance=hInstance;//实例号

wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);//图标 设置

wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);//光标 设置

wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);//画刷的句柄

wndcls.lpszMenuName=NULL;//(菜单名字)

wndcls.lpszClassName="VCXTU";//注册类名(longpoint命令行参数)



//2、注册窗口类;

RegisterClass(&wndcls);//注册类

/*

HWND CreateWindow(

LPCTSTR lpClassName, // pointer to registered class name

LPCTSTR lpWindowName, // pointer to window name

DWORD dwStyle, // window style

int x, // horizontal position of window

int y, // vertical position of window

int nWidth, // window width

int nHeight, // window height

HWND hWndParent, // handle to parent or owner window

HMENU hMenu, // handle to menu or child-window identifier

HANDLE hInstance, // handle to application instance

LPVOID lpParam // pointer to window-creation data

);

*/

//3、创建窗口;

HWND hwnd;//窗口句柄

hwnd=CreateWindow(

"VCXTU",//注册类名(lpszClassName)

"湖南省湘潭市湘潭大学",//窗口名字

/*

WS_OVERLAPPEDWINDOW:

窗口风格

WS_OVERLAPPED 层叠的 具有标题栏和边框

WS_CAPTION 有标题栏

WS_SYSMENU 带有系统菜单

WS_THICKFRAME 具有可调边框

WS_MINIMIZEBOX 具有最小化按钮

WS_MAXIMIZEBOX) 具有最大化按钮

*/

WS_OVERLAPPEDWINDOW,//窗口风格

CW_USEDEFAULT,//窗口水平坐标(缺省的)

CW_USEDEFAULT,//窗口垂直坐标(缺省的)

600,//窗口宽度

400,//窗口高度

NULL,// 父窗口句柄(hWndParent/handle to parent or owner window)

NULL,// 菜单句柄(handle to menu or child-window identifier)

hInstance,//句柄(handle to application instance)

NULL//窗口参数(pointer to window-creation data)

);

/*

BOOL ShowWindow(

HWND hWnd, // handle to window

int nCmdShow // show state of window

);

*/

//4、显示及更新窗口。

ShowWindow(

hwnd, //窗口句柄

SW_SHOWNORMAL //显示状态 show state of window

);

UpdateWindow(hwnd);//可有可无

/*

操作系统将每个事件都包装成一个称为消息的结构体MSG来传递给应用程序

MSG结构定义如下:

typedef struct tagMSG {

HWND hwnd;

UINT message;

WPARAM wParam;

LPARAM lParam;

DWORD time;

POINT pt;

} MSG;

*/

//消息循环

MSG msg;

/*

BOOL GetMessage(

LPMSG lpMsg, // address of structure with message

HWND hWnd, // handle of window

UINT wMsgFilterMin, // first message

UINT wMsgFilterMax // last message

);

*/

while(

GetMessage(

&msg,

NULL,

0,//设成0。表示对所有消息都有兴趣

0

)

)

{

/*

转换消息。可以将WM_KEYUP/WM_KEYDOWN转换为WM_CHAR消息、产生新消息并放到队列中

BOOL TranslateMessage(

CONST MSG *lpMsg // address of structure with message

);

*/

TranslateMessage(&msg);

/*

将收到的消息传到窗口的回调函数:wndcls.lpfnWndProc=WinSunProc;回调函数:WinSunProc

LONG DispatchMessage(

CONST MSG *lpmsg // pointer to structure with message

);

*/

DispatchMessage(&msg);

}

return 0;

}

LRESULT CALLBACK WinSunProc(

HWND hwnd, // handle to window

UINT uMsg, // message identifier

WPARAM wParam, // first message parameter

LPARAM lParam // second message parameter

)

{

switch(uMsg)

{

case WM_CHAR: //表示按下了某个字符按键

char szChar[20];

sprintf(szChar,"char is %d",wParam);//格式化消息。WM_CHAR中的wParam参数

/*

int MessageBox(

HWND hWnd, //拥有消息的父窗口 handle of owner window

LPCTSTR lpText, //文本 address of text in message box

LPCTSTR lpCaption, //标题 address of title of message box

UINT uType //消息框的风格 style of message box

);

*/

MessageBox(

hwnd,

szChar,

"湘潭大学", //标题

MB_OKCANCEL//MB_YESNO//消息框的风格

);

break;

case WM_LBUTTONDOWN:

MessageBox(hwnd,"鼠标左键点击了","湘潭大学",MB_OKCANCEL);

HDC hdc;//句柄 handle to device context

/*

HDC GetDC(

HWND hWnd // handle to a window

);

*/

hdc=GetDC(hwnd);

/*

BOOL TextOut(

HDC hdc, // handle to device context

int nXStart, // x-coordinate of starting position

int nYStart, // y-coordinate of starting position

LPCTSTR lpString, // pointer to string

int cbString // number of characters in string

);

*/

TextOut(hdc,0,50,"湖南省湘潭市湘潭大学1",strlen("湖南省湘潭市湘潭大学1"));

ReleaseDC(hwnd,hdc);//释放DC

break;

case WM_PAINT://窗口重绘

HDC hDC;

PAINTSTRUCT ps;

/*

HDC BeginPaint(

HWND hwnd, // handle to window

LPPAINTSTRUCT lpPaint

// pointer to structure for paint information

);

*/

hDC=BeginPaint(hwnd,&ps);//只能在WM_PAINT里面使用

TextOut(hDC,0,0,"湖南省湘潭市湘潭大学hDC",strlen("湖南省湘潭市湘潭大学hDC"));

EndPaint(hwnd,&ps);//只能在WM_PAINT里面使用

break;

case WM_CLOSE://关闭窗口时响应

if(IDYES==MessageBox(hwnd,"是否真的结束?","湘潭大学",MB_YESNO))

{

DestroyWindow(hwnd);//销掉hwnd窗口 还会发送 WM_DESTROY、WM_NCDESTROY 消息

}

break;

case WM_DESTROY:

PostQuitMessage(0);//退出程序

break;

default:

return DefWindowProc(hwnd,uMsg,wParam,lParam);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: