///////////////////////////////////////////////////////////////////////////
////// 本程序实现对windows程序的封装,使其类似MFC框架 //////////
///////////////////////////////////////////////////////////////////////////
//说明:添加消息响应函数步骤:1,在CView类中声明;2,在CView类外定义;3,在CView类的msgMap映射表内添加映射。
//其他对客户区界面的操作,均可在CView类内添加相应的函数来实现
#include "windows.h"
#include "resource.h"
//定义全局变量和函数
HINSTANCE hInst;
HINSTANCE hInstance;
MSG msg;
char lpszClassName[]="hehe";
char *ShowText;
//定义消息映射结构体
struct tagMsgMap{
UINT uMsgID;
LRESULT (CALLBACK* lpfnProc)(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
};
//定义消息映射数组
tagMsgMap myMsgMap[20];
//声明消息响应函数
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
LRESULT CALLBACK OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
//声明窗口类
class CFrameWnd
{
public:
HWND hWnd;
public:
int RegisterWindow();
void Create(LPCTSTR lpClassName,LPCTSTR lpWindowName);
void ShowWindow(int nCmdShow);
void UpdateWindow();
};
//类中的函数
int CFrameWnd::RegisterWindow()
{
WNDCLASS wndclass;
wndclass.style=0;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName="hMenu";
wndclass.lpszClassName=lpszClassName;
return RegisterClass(&wndclass);
}
void CFrameWnd::Create(LPCTSTR lpClassName,LPCTSTR lpWindowName)
{
RegisterWindow();
hInst=hInstance;
HMENU hMenu;
hMenu=LoadMenu(hInstance,(LPCSTR)IDR_MENU1);
hWnd=CreateWindow(lpszClassName,
lpWindowName,
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
0,CW_USEDEFAULT,0,NULL,hMenu,hInstance,NULL);
}
void CFrameWnd::ShowWindow(int nCmdShow)
{
::ShowWindow(hWnd,nCmdShow);
}
void CFrameWnd::UpdateWindow()
{
::UpdateWindow(hWnd);
}
//声明应用程序类
class CWinApp
{
public:
CFrameWnd *m_pMainWnd;
CWinApp *m_pCurrentWnd;
public:
CWinApp();
~CWinApp();
virtual BOOL InitInstance(int nCmdShow);
int Run();
};
CWinApp::CWinApp()
{
m_pCurrentWnd=this;
}
CWinApp::InitInstance(int nCmdShow)
{
m_pMainWnd=new CFrameWnd;
m_pMainWnd->Create(NULL,"Windows程序");
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
int CWinApp::Run()
{
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
CWinApp::~CWinApp()
{
delete m_pMainWnd;
}
//声明窗口类CView作为CFrameWnd的派生类,该部分主要针对客户区窗口的管理
class CView:public CFrameWnd
{
public:
void msgMap();
};
void CView::msgMap()
{
myMsgMap[0].uMsgID=WM_DESTROY;
myMsgMap[0].lpfnProc=OnDestroy;
myMsgMap[1].uMsgID=WM_PAINT;
myMsgMap[1].lpfnProc=OnPaint;
myMsgMap[2].uMsgID=WM_LBUTTONDOWN;
myMsgMap[2].lpfnProc=OnLButtonDown;
}
//声明应用程序类CWinApp派生类
class CMyApp:public CWinApp
{
public:
BOOL InitInstance(int nCmdShow);
};
CMyApp::InitInstance(int nCmdShow)
{
CView *pMainWnd;
pMainWnd=new CView;
pMainWnd->Create(NULL,"派生类Windows程序");
pMainWnd->ShowWindow(nCmdShow);
pMainWnd->UpdateWindow();
pMainWnd->msgMap();
return TRUE;
}
//构造CMyApp类对象
CMyApp myApp;
//重写AfxGetApp()函数
CWinApp *AfxGetApp()
{
return myApp.m_pCurrentWnd;
}
//WinMain
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
int rt;
CMyApp *pApp=(CMyApp *)AfxGetApp();
pApp->InitInstance(nCmdShow);
rt=pApp->Run();
return rt;
}
//窗口函数
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
for(int i=0;i<sizeof(myMsgMap)/sizeof(myMsgMap[0]);i++)
if(message==myMsgMap[i].uMsgID)
return (*myMsgMap[i].lpfnProc)(hWnd,message,wParam,lParam);//调用相应的处理函数
return DefWindowProc(hWnd,message,wParam,lParam);
/*switch(message)
{
case WM_LBUTTONDOWN:OnLButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:OnPaint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:OnDestroy(hWnd,message,wParam,lParam);
break;
default:return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;*/
}
//消息响应函数
LRESULT CALLBACK OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
ShowText="面向对象的封装性";
InvalidateRect(hWnd,NULL,1);
return 0l;
}
//刷新消息
LRESULT CALLBACK OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hWnd,&ps);
TextOut(hdc,150,150,ShowText,16);
EndPaint(hWnd,&ps);
return 0l;
}
//关闭消息
LRESULT CALLBACK OnDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
return 0l;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
MyApp封装.zip (18个子文件)
MyApp封装
MyApp.ncb 33KB
resource.h 503B
MyApp.opt 53KB
MyApp.dsp 4KB
MyApp.plg 1KB
Script1.aps 17KB
Debug
MyApp.ilk 197KB
vc60.pdb 76KB
MyApplication.sbr 924KB
MyApp.pdb 361KB
vc60.idb 137KB
MyApplication.obj 26KB
MyApp.pch 4.26MB
MyApp.exe 176KB
Script1.res 108B
MyApplication.cpp 5KB
MyApp.dsw 516B
Script1.rc 2KB
共 18 条
- 1
资源评论
- 游戏前端小明2013-05-18谢谢了 确实是封装而且实现了。但是俺是个菜鸟,能帮忙解释下创建窗口时hInstance是怎么赋值的吗 ,好像没有给他赋值就自动获取了当前实例?
- 孙卡2013-10-31代码写的不错,谢谢作者的分享,学习了!
- Yolly2013-11-20刚刚学会了应用程序打包,代码很好。
hyperminer
- 粉丝: 68
- 资源: 36
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功