文章目录
免费图标网站
https://iconarchive.com/search?q=Calculator
新建项目-》windows桌面-》windows桌面向导-》其他选项里面勾选空项目,应用程序类型选择桌面应用程序
mfc_02.cpp文件
#include "mfc_02.h"
MyApp app; //全局应用程序对象,有且仅有一个
BOOL MyApp::InitInstance()
{
//创建窗口
MyFrame* frame = new MyFrame;
//显示和更新
frame->ShowWindow(SW_SHOWNORMAL); //激活并显示窗口
frame->UpdateWindow();
m_pMainWnd = frame; //指针指向创建出来的窗口
return TRUE; //显示正常初始化
}
//分界宏
//开始消息映射的定义(必须用在类实现中)
//参数(theClass 指定消息映射所属的类的名字,baseClass 指定theClass的基类的名字)
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
//映射入口
ON_WM_LBUTTONDOWN() //鼠标左键按下
ON_WM_CHAR() //键盘
ON_WM_PAINT() //绘图
//结束消息映射的定义(必须用在类实现中)
END_MESSAGE_MAP()
MyFrame::MyFrame()
{
Create(NULL,TEXT("mfc_02"));
}
//获取当鼠标左键按下时,输出按下的位置的坐标
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(402, 410, 100, 100);
//TEXT和 TCHAR 自适应的编码转换 多字节转为宽字节
//1个字符对应一个字节 多字节
//1个字符对应多个字节 宽字节
}
mfc_02.h文件
#pragma once
#include <afxwin.h>
class MyApp :public CWinApp //CWinAPP 应用程序类
{
public:
//程序入口
virtual BOOL InitInstance(); //初始化函数
};
class MyFrame :public CFrameWnd //窗口框架类
{
public:
MyFrame();
//声明宏 提供消息映射机制,声明将在一个类中
//使用消息映射,把消息映射到函数,必须用在类声明中
DECLARE_MESSAGE_MAP();
//声明(函数原型)
afx_msg void OnLButtonDown(UINT, CPoint point);
afx_msg void OnChar(UINT, UINT, UINT);
afx_msg void OnPaint();
};
MFC应用程序类型:单文档、多文档与基于对话框
详情请点我
--------------------- 作者:Lemon萫 来源:CSDN 原文:https://blog.csdn.net/shushuidewoniu/article/details/8631721
版权声明:本文为博主原创文章,转载请附上博文链接!
MFC中afx_msg是什么,afx_msg void function()是什么意思
Windows API 函数: SetClassLong
单文档下怎么覆盖视图
这个是事件下面的消息处理函数
// TODO: 在此添加命令处理程序代码
CView *pOldActiveView = GetActiveView();//旧视图
AddrList *pNewActiveView = new AddrList();//要切换的视图 //添加类的时候要把AddList的构造函数声明为public
CCreateContext context;
context.m_pCurrentDoc = pOldActiveView->GetDocument();//关联到文档.为什么,文档视图,MFC中每个视图要对应一个文档
//创建视图并且关联到上下文中,IDD_MYVIEW为视图CMyView的ID号
pNewActiveView->Create(NULL, NULL, WS_CHILD | WS_BORDER, CFrameWnd::rectDefault, this, IDD_ADDRLIST, &context);
pNewActiveView->UpdateData();
//视图显示/隐藏
SetActiveView(pNewActiveView);
pNewActiveView->ShowWindow(SW_SHOW);
pOldActiveView->ShowWindow(SW_HIDE);
//重新布局主框架
pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST);
RecalcLayout();
delete pOldActiveView;
重写create函数在对话框类里面
BOOL AddrList::Create(LPCTSTR a1, LPCTSTR a2, DWORD a3, const RECT &a4, CWnd *a5, UINT a6, CCreateContext *a7)
{
CFormView::Create(a1,a2,a3,a4,a5,a6,a7);
return TRUE;
}