划重点:项目->属性->常规->高级->将MFC的使用设置为在“共享dll中使用mfc”
头文件mfc.h
#include <afxwin.h>//mfc头文件
class MyApp:public CWinApp{
public:
virtual BOOL InitInstance();
};
class MyFrame :public CFrameWnd {//窗口框架类
public:
MyFrame();
//声明宏 提示消息映射机制
DECLARE_MESSAGE_MAP();
afx_msg void OnLButtonDown(UINT,CPoint);
afx_msg void OnChar(UINT,UINT,UINT);
afx_msg void OnPaint();
};
源文件mfc.cpp
#include “mfc.h”
MyApp app;
BOOL MyApp::InitInstance() {
//创建窗口
MyFrame* frame = new MyFrame;
//显示和更新
frame->ShowWindow(SW_SHOWNORMAL);
frame->UpdateWindow();
m_pMainWnd = frame;//保存指向应用程序的主窗口的指针
return TRUE;//返回正常初始化
}
//分界宏
BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
ON_WM_LBUTTONDOWN()//鼠标左键按下
ON_WM_CHAR()
ON_WM_PAINT()
END_MESSAGE_MAP()
MyFrame::MyFrame()
{
Create(NULL,TEXT(“windows”));
}
void MyFrame::OnLButtonDown(UINT, CPoint point)
{
//TCHAR buf[1024];
//wsprintf(buf, TEXT(“x = %d,y = %d”), point.x, point.y);
//MessageBox(buf);
CString str;
str.Format(TEXT(“x = %d,y = %d”),point.x,point.y);
MessageBox(str);
}
void MyFrame::OnChar(UINT key, UINT, UINT)
{
CString str;
str.Format(TEXT(“按下了%c键”),key);
MessageBox(str);
}
void MyFrame::OnPaint()
{
CPaintDC dc(this);
dc.TextOutW(100,100,TEXT(“为了部落”));
dc.Ellipse(10,10,100,100);
}
到此这篇关于如何使用visual studio2019创建简单的MFC窗口(使用C++)的文章就介绍到这了,更多相关vs2019创建MFC窗口内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!










