11 MFC 制作记事本

这篇具有很好参考价值的文章主要介绍了11 MFC 制作记事本。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

界面制作

11 MFC 制作记事本

制作菜单

选择Menu 点击新建

11 MFC 制作记事本

将内容写入"_"的用& 符号

11 MFC 制作记事本

将菜单加入到窗口中

11 MFC 制作记事本

设置编译框随着窗口的变化而变化OnSize

//设置编译框随着窗口的变化而变化
void CnotepadPlusDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	//获取控件
	CWnd* pEdit=GetDlgItem(IDC_EDIT1);
	if(pEdit!=NULL)
		pEdit->MoveWindow(0, 0, cx, cy);
}

打开文件

右键选择添加事件处理程序

11 MFC 制作记事本

点击确定

11 MFC 制作记事本

Edit设置多行显示

11 MFC 制作记事本

Edit设置按回车能够换行

11 MFC 制作记事本

Edit设置竖直方向滚动

11 MFC 制作记事本

打开文件代码

//打开文件
void CnotepadPlusDlg::OnOpen()
{

	CFileDialog dlg(TRUE);
	if (IDCANCEL == dlg.DoModal())
		return;
	
	CString strFilePath = dlg.GetPathName();//获取全路径
	CString strFileName = dlg.GetFileName();

	//文件操作
	//CFile 文件类
	CFile file;
	if (FALSE == file.Open(strFilePath, CFile::modeRead))//file.Open(文件路径,文件读取模式)
	{
		MessageBox(L"打开文件失败", L"温馨提示",MB_OK);
		return;
	}
	//读取内容
	CStringA strContent ;//清空数组
	char szContent[10] = { 0 };
	while (file.Read(szContent, 2))
	{
		strContent = strContent+szContent;//内容连接
	}
	
	//编码转换
	CCharset charset;
	wchar_t* wContent=charset.AnsiToUnicode(strContent);

	//设置窗口
	SetDlgItemText(IDC_EDIT1, wContent);

	//设置标题
	CString strTitle;
	strTitle.Format(L"超级记事本-%s[%d字节]", strFileName, file.GetLength());
	SetWindowText(strTitle);

	file.Close();
}

文件另存为

void CnotepadPlusDlg::OnSaveAs()
{
	CFileDialog dlg(FALSE,NULL,NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
	if (IDCANCEL == dlg.DoModal())
		return;
	CString strFilePath = dlg.GetPathName();
	CFile file;
	if (FALSE == file.Open(strFilePath, CFile::modeCreate |CFile::modeWrite))//file.Open(文件路径,文件读取模式)
	{
		MessageBox(L"打开文件失败", L"温馨提示", MB_OK);
		return;
	}

	//读取出控件中所有的文本
	CString strContent;
	GetDlgItemText(IDC_EDIT1, strContent);

	//另存为Unicode
	CCharset charset;
	char* content=charset.UnicodeToAnsi(strContent);


	//file.Write(strContent, strContent.GetLength() * 2);
	file.Write(content, strlen(content));
	file.Close();

	//::MultiByteToWideChar
	//::WideCharToMultiByte
}

设置字体颜色

//设置颜色
void CnotepadPlusDlg::OnBtmColor()
{
	CColorDialog dlg;
	if (IDCANCEL == dlg.DoModal())
		return;
    m_color = dlg.GetColor();//获取到颜色
	
}

//编辑控件颜色
HBRUSH CnotepadPlusDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	switch (pWnd->GetDlgCtrlID())
	{
		case IDC_EDIT1:
			pDC->SetTextColor(m_color);
			break;
	}
	
	return hbr;
}

11 MFC 制作记事本

修改字体

//修改字体
void CnotepadPlusDlg::OnBtnFont()
{
	//字体选择对话框
	CFontDialog dlg;
	if (IDCANCEL == dlg.DoModal())
		return;
	//设置字体
	CFont font;
	font.CreatePointFont(dlg.GetSize(), dlg.GetFaceName());
	GetDlgItem(IDC_EDIT1)->SetFont(&font);
}

11 MFC 制作记事本

文件的查找与替换

11 MFC 制作记事本

查找与替换对话框显示(非模态对话框)

//显示
//查找非模态对话框,需要注册消息
void CnotepadPlusDlg::OnFind()
{
	CFindReplaceDialog* pFindDlg=new CFindReplaceDialog;
	//第一个:TRUE 代表查找框  FALSE 代表替换框
	//第二个参数:要查找的东西
	//第三个:要替换的东西
	pFindDlg->Create(TRUE,NULL,NULL);
	pFindDlg->ShowWindow(SW_SHOW);
    m_bFind=TRUE;//查找


	
}

//替换
void CnotepadPlusDlg::OnReplace()
{
	CFindReplaceDialog* pFindDlg = new CFindReplaceDialog;
	//第一个:TRUE 代表查找框  FALSE 代表替换框
	//第二个参数:要查找的东西
	//第三个:要替换的东西
	pFindDlg->Create(FALSE, NULL, NULL);
	pFindDlg->ShowWindow(SW_SHOW);
	m_bFind = FALSE;//替换
}

对话框消息与对话框处理函数

11 MFC 制作记事本
11 MFC 制作记事本

对话框处理函数

//CFindReplaceDialog处理函数
LONG CnotepadPlusDlg::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
	CFindReplaceDialog* pFindReplaceDlg = CFindReplaceDialog::GetNotifier(lParam);//接收查找到的参数,获取窗口指针
	if (pFindReplaceDlg == NULL)
		return 0;

	//pFindReplaceDlg->IsTerminating();//是否点击关闭
	if (pFindReplaceDlg->IsTerminating())
	{
		return 0;
	}

	CString strFindString=pFindReplaceDlg->GetFindString();//要查找的字符串
	CString strRepalceString = pFindReplaceDlg->GetReplaceString();//要替换的字符串
	BOOL bSearchDown=pFindReplaceDlg->SearchDown();//判断是否向下查找
	BOOL bMatchCase=pFindReplaceDlg->MatchCase();//匹配大小写

	int nStartIndex = 0, nEndIndex = 0;//光标的开始和结束位置

	//获取编辑框里面的所有文本
	CString strContent;
	GetDlgItemText(IDC_EDIT1, strContent);
	//操纵控件
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);

	//查找
	if (m_bFind)
	{
		
		//向下查找
		if (bSearchDown)
		{
			//获取光标位置
			pEdit->GetSel(nStartIndex, nEndIndex);
			nStartIndex=strContent.Find(strFindString, nEndIndex);//要查找的内容一直到结束位置

			//找到了
			if (nStartIndex != -1)
			{
				pEdit->SetSel(nStartIndex, nStartIndex + strFindString.GetLength());
				pEdit->SetFocus();//蓝色
			}
			else
			{
				//找不到重头开始找
				nStartIndex = strContent.Find(strFindString, 0);
				//找不到
				if (nStartIndex == -1)
				{
					MessageBox(L"没有找到");
					return 0;
				}
				pEdit->SetSel(nStartIndex, nStartIndex + strFindString.GetLength());
				pEdit->SetFocus();//蓝色
				
			}

		}
		else//向上查找
		{
			pEdit->GetSel(nStartIndex, nEndIndex);
			strContent = strContent.MakeReverse();//反转字符串倒着查找
			CString strReverseFindString=strFindString.MakeReverse();//反转后查找字符串
			nStartIndex = strContent.Find(strReverseFindString,strContent.GetLength()-nStartIndex);//要查找的内容一直到结束位置,比如有10个是第二个数为2,反转后在第八个数的位置上

			if (nStartIndex != -1)
			{
				//设置光标
				nEndIndex = strContent.GetLength()-nStartIndex-1;
				pEdit->SetSel(nEndIndex+1-strFindString.GetAllocLength(),nEndIndex+1);
				pEdit->SetFocus();
			}
			else
			{

			}
		}

	}
	else//替换
	{
		//查找下一个
		if (pFindReplaceDlg->FindNext())
		{

		}
		//替换当前
		if (pFindReplaceDlg->ReplaceCurrent())
		{
			nStartIndex=strContent.Find(strFindString);
			if (nStartIndex == -1)
			{
				MessageBox(L"没有查找到");
				return 0;
			}
			else
			{
				nEndIndex = nStartIndex + strFindString.GetLength();
				//做截取
				CString strLeft=strContent.Left(nStartIndex);//获取左右
				CString strRight = strContent.Right(strContent.GetLength()- nEndIndex);
				//替换的链接
				strContent = strLeft + strRepalceString + strRight;
				pEdit->SetWindowText(strContent);//设置文本内容
			}
		}

		//替换全部
		if (pFindReplaceDlg->ReplaceAll())
		{
			int nCount= strContent.Replace(strFindString,strRepalceString);
			pEdit->SetWindowText(strContent);
			CString str;
			str.Format(L"总共替换了%d处", nCount);
			MessageBox(str);
		}
	}
	return 0;
}

全部代码

//notepadPlusDlg.h

// notepadPlusDlg.h: 头文件
//

#pragma once


// CnotepadPlusDlg 对话框
class CnotepadPlusDlg : public CDialogEx
{
// 构造
public:
	CnotepadPlusDlg(CWnd* pParent = nullptr);	// 标准构造函数

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_NOTEPADPLUS_DIALOG };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支持


// 实现
protected:
	HICON m_hIcon;

	// 生成的消息映射函数
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	COLORREF m_color;
	BOOL m_bFind;//判断是点击了查找还是替换
	afx_msg void OnBnClickedOk();
	afx_msg void OnEnChangeEdit1();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	
	afx_msg void OnOpen();
	afx_msg void OnSaveAs();
	afx_msg void OnBtmColor();
	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
	afx_msg void OnBtnFont();
	afx_msg void OnFind();
	afx_msg void OnReplace();
	afx_msg LONG OnFindReplace(WPARAM wParam, LPARAM lParam);//定义关联函数

};

//notepadPlusDlg.cpp

// notepadPlusDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "notepadPlus.h"
#include "notepadPlusDlg.h"
#include "afxdialogex.h"
#include "CCharset.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


//注册消息
static UINT WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING);

// CnotepadPlusDlg 对话框

CnotepadPlusDlg::CnotepadPlusDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_NOTEPADPLUS_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_color = RGB(0, 0, 0);
    m_bFind=FALSE;

}

void CnotepadPlusDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CnotepadPlusDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CnotepadPlusDlg::OnBnClickedOk)
	ON_EN_CHANGE(IDC_EDIT1, &CnotepadPlusDlg::OnEnChangeEdit1)
	ON_WM_SIZE()
	ON_COMMAND(IDM_OPEN, &CnotepadPlusDlg::OnOpen)
	ON_COMMAND(IDM_SAVE_AS, &CnotepadPlusDlg::OnSaveAs)
	ON_COMMAND(IDM_BTM_COLOR, &CnotepadPlusDlg::OnBtmColor)
	ON_WM_CTLCOLOR()
	ON_COMMAND(IDM_BTN_FONT, &CnotepadPlusDlg::OnBtnFont)
	ON_COMMAND(IDM_FIND, &CnotepadPlusDlg::OnFind)
	ON_COMMAND(ID_REPLACE, &CnotepadPlusDlg::OnReplace)
	ON_REGISTERED_MESSAGE(WM_FINDREPLACE, &CnotepadPlusDlg::OnFindReplace) 
END_MESSAGE_MAP()


// CnotepadPlusDlg 消息处理程序

BOOL CnotepadPlusDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CnotepadPlusDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CnotepadPlusDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CnotepadPlusDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	//CDialogEx::OnOK();
}


void CnotepadPlusDlg::OnEnChangeEdit1()
{
	// TODO:  如果该控件是 RICHEDIT 控件,它将不
	// 发送此通知,除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask(),
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


//设置编译框随着窗口的变化而变化
void CnotepadPlusDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	//获取控件
	CWnd* pEdit=GetDlgItem(IDC_EDIT1);
	if(pEdit!=NULL)
		pEdit->MoveWindow(0, 0, cx, cy);
}




//打开文件
void CnotepadPlusDlg::OnOpen()
{

	CFileDialog dlg(TRUE);
	if (IDCANCEL == dlg.DoModal())
		return;
	
	CString strFilePath = dlg.GetPathName();//获取全路径
	CString strFileName = dlg.GetFileName();

	//文件操作
	//CFile 文件类
	CFile file;
	if (FALSE == file.Open(strFilePath, CFile::modeRead))//file.Open(文件路径,文件读取模式)
	{
		MessageBox(L"打开文件失败", L"温馨提示",MB_OK);
		return;
	}
	//读取内容
	CStringA strContent ;//清空数组
	char szContent[10] = { 0 };
	while (file.Read(szContent, 2))
	{
		strContent = strContent+szContent;//内容连接
	}
	
	//编码转换
	CCharset charset;
	wchar_t* wContent=charset.AnsiToUnicode(strContent);

	//设置窗口
	SetDlgItemText(IDC_EDIT1, wContent);

	//设置标题
	CString strTitle;
	strTitle.Format(L"超级记事本-%s[%d字节]", strFileName, file.GetLength());
	SetWindowText(strTitle);

	file.Close();
}


//另存为
void CnotepadPlusDlg::OnSaveAs()
{
	CFileDialog dlg(FALSE,NULL,NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*||");
	if (IDCANCEL == dlg.DoModal())
		return;
	CString strFilePath = dlg.GetPathName();
	CFile file;
	if (FALSE == file.Open(strFilePath, CFile::modeCreate |CFile::modeWrite))//file.Open(文件路径,文件读取模式)
	{
		MessageBox(L"打开文件失败", L"温馨提示", MB_OK);
		return;
	}

	//读取出控件中所有的文本
	CString strContent;
	GetDlgItemText(IDC_EDIT1, strContent);

	//另存为Unicode
	CCharset charset;
	char* content=charset.UnicodeToAnsi(strContent);


	//file.Write(strContent, strContent.GetLength() * 2);
	file.Write(content, strlen(content));
	file.Close();

	
}


//设置颜色
void CnotepadPlusDlg::OnBtmColor()
{
	CColorDialog dlg;
	if (IDCANCEL == dlg.DoModal())
		return;
    m_color = dlg.GetColor();//获取到颜色
	
}

//编辑控件颜色
HBRUSH CnotepadPlusDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	switch (pWnd->GetDlgCtrlID())
	{
		case IDC_EDIT1:
			pDC->SetTextColor(m_color);
			break;
	}
	
	return hbr;
}

//修改字体
void CnotepadPlusDlg::OnBtnFont()
{
	//字体选择对话框
	CFontDialog dlg;
	if (IDCANCEL == dlg.DoModal())
		return;
	//设置字体
	CFont font;
	font.CreatePointFont(dlg.GetSize(), dlg.GetFaceName());
	GetDlgItem(IDC_EDIT1)->SetFont(&font);
}







//显示
//查找非模态对话框,需要注册消息
void CnotepadPlusDlg::OnFind()
{
	CFindReplaceDialog* pFindDlg=new CFindReplaceDialog;
	//第一个:TRUE 代表查找框  FALSE 代表替换框
	//第二个参数:要查找的东西
	//第三个:要替换的东西
	pFindDlg->Create(TRUE,NULL,NULL);
	pFindDlg->ShowWindow(SW_SHOW);
    m_bFind=TRUE;//查找


	
}

//替换
void CnotepadPlusDlg::OnReplace()
{
	CFindReplaceDialog* pFindDlg = new CFindReplaceDialog;
	//第一个:TRUE 代表查找框  FALSE 代表替换框
	//第二个参数:要查找的东西
	//第三个:要替换的东西
	pFindDlg->Create(FALSE, NULL, NULL);
	pFindDlg->ShowWindow(SW_SHOW);
	m_bFind = FALSE;//替换
}


//CFindReplaceDialog处理函数
LONG CnotepadPlusDlg::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
	CFindReplaceDialog* pFindReplaceDlg = CFindReplaceDialog::GetNotifier(lParam);//接收查找到的参数,获取窗口指针
	if (pFindReplaceDlg == NULL)
		return 0;

	//pFindReplaceDlg->IsTerminating();//是否点击关闭
	if (pFindReplaceDlg->IsTerminating())
	{
		return 0;
	}

	CString strFindString=pFindReplaceDlg->GetFindString();//要查找的字符串
	CString strRepalceString = pFindReplaceDlg->GetReplaceString();//要替换的字符串
	BOOL bSearchDown=pFindReplaceDlg->SearchDown();//判断是否向下查找
	BOOL bMatchCase=pFindReplaceDlg->MatchCase();//匹配大小写

	int nStartIndex = 0, nEndIndex = 0;//光标的开始和结束位置

	//获取编辑框里面的所有文本
	CString strContent;
	GetDlgItemText(IDC_EDIT1, strContent);
	//操纵控件
	CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);

	//查找
	if (m_bFind)
	{
		
		//向下查找
		if (bSearchDown)
		{
			//获取光标位置
			pEdit->GetSel(nStartIndex, nEndIndex);
			nStartIndex=strContent.Find(strFindString, nEndIndex);//要查找的内容一直到结束位置

			//找到了
			if (nStartIndex != -1)
			{
				pEdit->SetSel(nStartIndex, nStartIndex + strFindString.GetLength());
				pEdit->SetFocus();//蓝色
			}
			else
			{
				//找不到重头开始找
				nStartIndex = strContent.Find(strFindString, 0);
				//找不到
				if (nStartIndex == -1)
				{
					MessageBox(L"没有找到");
					return 0;
				}
				pEdit->SetSel(nStartIndex, nStartIndex + strFindString.GetLength());
				pEdit->SetFocus();//蓝色
				
			}

		}
		else//向上查找
		{
			pEdit->GetSel(nStartIndex, nEndIndex);
			strContent = strContent.MakeReverse();//反转字符串倒着查找
			CString strReverseFindString=strFindString.MakeReverse();//反转后查找字符串
			nStartIndex = strContent.Find(strReverseFindString,strContent.GetLength()-nStartIndex);//要查找的内容一直到结束位置,比如有10个是第二个数为2,反转后在第八个数的位置上

			if (nStartIndex != -1)
			{
				//设置光标
				nEndIndex = strContent.GetLength()-nStartIndex-1;
				pEdit->SetSel(nEndIndex+1-strFindString.GetAllocLength(),nEndIndex+1);
				pEdit->SetFocus();
			}
			else
			{

			}
		}

	}
	else//替换
	{
		//查找下一个
		if (pFindReplaceDlg->FindNext())
		{

		}
		//替换当前
		if (pFindReplaceDlg->ReplaceCurrent())
		{
			nStartIndex=strContent.Find(strFindString);
			if (nStartIndex == -1)
			{
				MessageBox(L"没有查找到");
				return 0;
			}
			else
			{
				nEndIndex = nStartIndex + strFindString.GetLength();
				//做截取
				CString strLeft=strContent.Left(nStartIndex);//获取左右
				CString strRight = strContent.Right(strContent.GetLength()- nEndIndex);
				//替换的链接
				strContent = strLeft + strRepalceString + strRight;
				pEdit->SetWindowText(strContent);//设置文本内容
			}
		}

		//替换全部
		if (pFindReplaceDlg->ReplaceAll())
		{
			int nCount= strContent.Replace(strFindString,strRepalceString);
			pEdit->SetWindowText(strContent);
			CString str;
			str.Format(L"总共替换了%d处", nCount);
			MessageBox(str);
		}
	}
	return 0;
}

//CCharset.h

#pragma once
class CCharset
{

private:
	wchar_t* m_wstr;
	char* m_str;
	char* m_utf8;
public:
	CCharset();
	~CCharset();

	//ANSIתUnicode
	wchar_t* AnsiToUnicode(const char* str);
	//UnicodeתANSI
	char* UnicodeToAnsi(const wchar_t* wstr);
	//UTF8 תANSI
	char* Utf8ToAnsi(const char* str);
	//ANSIתUTF - 8
	char* AnsitoUtf8(const char* str);
	//Unicode ת UTF-8
	char* UnicodeToUtf8(const wchar_t* wstr);
	//UTF-8תUnicode
	wchar_t* Utf8ToUnicode(const char* str);

};

//CCharset.cpp文章来源地址https://www.toymoban.com/news/detail-514228.html

#include "pch.h"
#include "CCharset.h"

CCharset::CCharset()
{
	m_wstr = NULL;
	m_str = NULL;
	m_utf8 = NULL;
}


CCharset::~CCharset()
{
	if (m_wstr)
	{
		delete m_wstr;
		m_wstr = NULL;
	}
		
	if (m_str)
	{
		delete m_str;
		m_str = NULL;
	}

	if (m_utf8)
	{
		delete[] m_utf8;
		m_utf8 = NULL;
	}
}

//ANSI转Unicode
wchar_t* CCharset::AnsiToUnicode(const char* str)
{
	if (m_wstr)//安全
	{
		delete[] m_wstr;
		m_wstr = NULL;
	}
		
	DWORD dwSize=::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);//求宽字符的大小
	m_wstr = new wchar_t[dwSize];
	::MultiByteToWideChar(CP_ACP, 0, str, -1, m_wstr, dwSize);
	return m_wstr;
}

//Unicode转ANSI
char * CCharset::UnicodeToAnsi(const wchar_t * wstr)
{
	if (m_str)
	{
		delete[] m_str;
		m_str = NULL;
	}
	DWORD dwSize=WideCharToMultiByte(CP_ACP, 0, wstr, -1,NULL,0,NULL,NULL);//求字符的大小
	m_str = new char[dwSize];
	::WideCharToMultiByte(CP_ACP, 0, wstr, -1, m_str, dwSize, NULL, NULL);
	return m_str;
}


char * CCharset::Utf8ToAnsi(const char * str)
{
	if (m_wstr)
	{
		delete[] m_wstr;
		m_wstr = NULL;
	}

	if (m_str)
	{
		delete[] m_str;
		m_str = NULL;
	}
	//UTF-8 转Unicode
	m_wstr= Utf8ToUnicode(str);
	//Unicode 转ANSI
	m_str= UnicodeToAnsi(m_wstr);
	return m_str;
}

char* CCharset::AnsitoUtf8(const char* str)
{
	if (m_wstr)
	{
		delete[] m_wstr;
		m_wstr = NULL;
	}

	if (m_utf8)
	{
		delete[] m_utf8;
		m_utf8 = NULL;
	}

	//Ansi 转Unicode
	m_wstr= AnsiToUnicode(str);
	//Unicode 转UTF-8
	m_utf8=UnicodeToUtf8(m_wstr);

	return m_utf8;
}

//Unicode 转 UTF-8
char * CCharset::UnicodeToUtf8(const wchar_t * wstr)
{
	if (m_utf8)
	{
		delete[] m_utf8;
		m_utf8 = NULL;
	}
	DWORD dwSize=WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	m_utf8 = new char[dwSize];
	memset(m_utf8,0,dwSize);//清空内存
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, m_utf8, dwSize, NULL, NULL);
	return m_utf8;
}

//utf8 转Unicode
wchar_t* CCharset::Utf8ToUnicode(const char * str)
{
	if (m_wstr)
	{
		delete m_wstr;
		m_wstr = NULL;
	}
	DWORD dwSize=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	m_wstr = new wchar_t[dwSize];
	memset(m_wstr, 0, dwSize*sizeof(wchar_t));//清空内存
	MultiByteToWideChar(CP_UTF8, 0, str, -1, m_wstr, dwSize);
	return m_wstr;
}

到了这里,关于11 MFC 制作记事本的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • win11原生应用(如相机、相册、日历、记事本、邮件、画图等)英文改中文方法

            很多小伙伴发现win11升级后或者突然有一天自己电脑中的win11原生应用win11原生应用(如相机、相册、日历、记事本、邮件、画图等)突然都变成了英文界面,然后这些应用中还没有设置语言的选项,那么这个时候该怎么办呢?别着急,这是系统首选语言的锅,按照以下

    2024年02月08日
    浏览(37)
  • 电脑记事本在哪里?电脑桌面显示记事本要怎么设置?

    绝大多数上班族在使用电脑办公时,都需要随手记录一些琐碎或重要的事情,例如工作注意事项、常用的文案、某项工作的具体要求、多个平台的账号和密码等。于是就有不少小伙伴想要使用电脑记事本软件来记录,那么电脑记事本在哪里呢?想要电脑桌面显示记事本怎么设

    2024年02月15日
    浏览(30)
  • Android记事本

    1、项目需求分析 1.1、记事功能需求分析: 1.1.1、显示记事 用户打开记事本可以看到之前所写的所有记事内容,进入主页后,软件应该从数据库中搜索出该用户所写的全部记事,并将所有的数据进行显示。 1.1.2、添加记事 设置添加按钮,点击添加按钮之后可以编辑记事的标题

    2024年02月03日
    浏览(39)
  • Vue设计记事本

    项目描述 项目实现功能有:记录今天要完成的任务,勾选已经完成的任务,删除已经完成的全部任务。 界面展示: 代码展示 创建一个Myitem.vue文件夹 2.在components文件夹下创建 Myheader文件夹 3.在同一个文件夹下创建MyFooter.vue文件夹 4.在同个文件夹下创建MyList.vue文件 5.再创建

    2024年02月08日
    浏览(47)
  • Java小程序-记事本

    摘 要 为了使自己熟悉Java编译,了解更多的面向对象语言的编程策略。进而,深入了解Java语言的操作、及原理等。因此我开发了一个记事本,使自己可以巩固知识,加深记忆。设计一个简易记事本,能够记录使用者输入的信息,同时可以实现保存输入的信息,以方便后期查看

    2024年02月04日
    浏览(46)
  • java记事本源代码

    本文仿电脑自带记事本,实现的功能有新建、新窗口、打开、保存、另存为、退出、撤销、剪切、复制、粘贴、删除、查找、查找下一个、查找上一个、替换、转到、全选、时间/日期、自动换行、缩放(放大、缩小、恢复默认大小),未实现功能有页面设置、打印、字体、状

    2024年02月10日
    浏览(29)
  • vue记事本渲染以及交互

    2024年04月10日
    浏览(33)
  • Android开发_记事本(1)

    TextView中有下述几个属性: id: 为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id! layout_width: 组件的宽度,一般写: wrap_content 或者 match_parent(fill_parent) ,前

    2023年04月10日
    浏览(53)
  • 【Java】实现记事本(完整版)

    💕💕💕大家好,这是作业侠系列之Java实现记事本,还是那句话,我的粉丝们需要源码直接qq邮箱+你需要的源码私发我即可哦,大家觉得还行的话,期待你们的三连,这也是我创作的最大动力💕💕💕 往期源码回顾: 【Java】实现绘图板(完整版) 【C++】图书管理系统(完整板

    2024年02月08日
    浏览(42)
  • C# 记事本应用程序

    2024年02月10日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包