在Visual Studio中创建一个新的空Win32工程,配置属性--常规---在静态库中使用 MFC
内容如下:
1.h
#ifndef MFCTEST_HEAD
#define MFCTEST_HEAD
#pragma once
#include <afxwin.h>
#include <afxframewndex.h>
#define IDC_START 1012 //按钮定义
class CMyApp:public CWinApp
{
public:
//构造函数
CMyApp();
//析构函数
virtual ~CMyApp();
//重载函数
public:
//初始函数
virtual BOOL InitInstance();
};
class CMyWindow : public CFrameWndEx
{
public:
//构造函数
CMyWindow();
//析构函数
virtual ~CMyWindow();
//虚函数
public:
//命令响应消息
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
//消息映射
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
public:
CButton m_button; //按钮
};
#endif
1.cpp
#include "1.h"
CMyApp myapp;
//构造函数
CMyApp::CMyApp()
{
}
//析构函数
CMyApp::~CMyApp()
{
}
//初始函数
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMyWindow();
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMyWindow, CFrameWndEx)
ON_WM_CREATE()
END_MESSAGE_MAP()
//构造函数
CMyWindow::CMyWindow()
{
Create(NULL, TEXT("My Window"));
}
//析构函数
CMyWindow::~CMyWindow()
{
}
//命令响应消息
BOOL CMyWindow::OnCommand( WPARAM wParam, LPARAM lParam )
{
int wmId=LOWORD(wParam);
switch (wmId)
{
case IDC_START:
{
MessageBox(TEXT("测试一下哈"),TEXT("提示"),MB_OK);
return true;
}
break;
default:
return true;
}
return __super::OnCommand(wParam,lParam);
}
//创建事件
int CMyWindow::OnCreate( LPCREATESTRUCT lpCreateStruct )
{
__super::OnCreate(lpCreateStruct);
//居中窗口
SetActiveWindow();
SetForegroundWindow();
CenterWindow(GetParent());
//创建按钮
RECT rect={0,0,50,30};
m_button.Create(TEXT("按钮"),WS_CHILD|WS_VISIBLE,rect,this,IDC_START);
return true;
}