Creation of MFC window, message mapping mechanism, windows character set

Windows character set
English, 1 character corresponds to 1 byte, multi-byte
Chinese, 1 character corresponds to multiple bytes, wide byte, Unicode, utf-8 3, GBK 2
scheme right click-Properties-General- Character set, check the character set used by the current project
MessageBox(L"aaa"); //L stands for multi-byte widened byte
TEXT, adaptive encoding conversion, converting the encoding to the encoding used by the current project,
TCHAR, adaptive encoding Conversion

If you have OnDraw, don’t have OnPaint anymore. If it exists at the same time, OnPaint will overwrite OnDraw
. The functions with the suffix named Ex in
MFC are all extended functions in MFC. The functions prefixed with Afx are all global functions and can be used in any part of the program. Place call

mfc.h

#include <afxwin.h>//mfc使用的头文件

//CWinApp应用程序类
class MyApp:public CWinApp 
{
    
    
public:
	virtual BOOL InitInstance();//程序入口(没有main和winmain)
};
//窗口框架类
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;//保存指向应用程序的主窗口的指针,CWinThread
	return TRUE; //返回正常的初始化
}

//分界宏
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
	ON_WM_LBUTTONDOWN()//鼠标左键按下
	ON_WM_CHAR()//键盘
	ON_WM_PAINT()//绘图
END_MESSAGE_MAP()

MyFrame::MyFrame()
{
    
    
	Create(NULL, TEXT("mfc"));
}

//鼠标按下
void MyFrame::OnLButtonDown(UINT, CPoint point)
{
    
    
	//TCHAR buf[1024];//TCHAR:MFC中的字符数组
	//wsprintf(buf, TEXT("x=%d,y=%d"), point.x, point.y);
	//MessageBox(buf);

	//mfc中的字符串,CString
	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);//类似QT中的画家,this代表绘图设备;在父类CDC中找能画的图形

	dc.TextOutW(100, 100, TEXT("为了部落"));

	//画椭圆
	dc.Ellipse(10, 10, 100, 100);//两个坐标组成矩形,画矩形内切圆

	//统计字符串长度
	int num = 0;
	char * p = "aaaaaa";
	num = strlen(p);

	//统计宽字节的字符串长度
	wchar_t * p2 = L"bbbbb";//声明宽字节字符串
	num = wcslen(p2);

	//char *与CString之间的转换
	//char*转CStirng
	char *p3 = "ccc";
	CString str = CString(p3);

	//CString转char*
	CStringA tmp;
	tmp = str;
	char * pp = tmp.GetBuffer();
}

Guess you like

Origin blog.csdn.net/weixin_40355471/article/details/108602906