⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cstringdemodlg.cpp

📁 VC++6开发指南的源代码第7章-第11章
💻 CPP
字号:
// CStringDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CStringDemo.h"
#include "CStringDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCStringDemoDlg dialog

CCStringDemoDlg::CCStringDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCStringDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCStringDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCStringDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCStringDemoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCStringDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CCStringDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCStringDemoDlg message handlers

BOOL CCStringDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCStringDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCStringDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		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;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCStringDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCStringDemoDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	/*CString str1=_T("连接");
	CString str2=_T("字符串");
	CString str3=str1+str2;
	AfxMessageBox(str3);
	CString str1="1267";
	CString str2="345";
	str1.Insert(2,str2);
	AfxMessageBox(str1);
	
	CString str1="字符串A";
	CString str2="字符串B";
	if(str1==str2)
		AfxMessageBox("二者相同");
	else if(str1>str2)
		AfxMessageBox("'字符串A'大于'字符串B'");
	else
		AfxMessageBox("'字符串A'小于'字符串B'");
	CString str1="abstract";
	CString str2="balance";
	int result=str1.Compare(str2);
	if(result=0)
		AfxMessageBox("二者相同");
	else if(result>0)
		AfxMessageBox("'abstract'大于'balance'");
	else
		AfxMessageBox("'abstract'小于'balance'");


	CString str1(_T("abcdefghijk"));
	CString str2=str1.Left(2)+str1.Mid(3,2)+str1.Right(1);
	AfxMessageBox(str2);

	CString str1("cabbkage");
	CString res1 = str1.SpanIncluding(_T("abckl"));
	AfxMessageBox("包含提取:"+res1);
	CString res2 = str1.SpanExcluding(_T("kl"));
	AfxMessageBox("不包含提取:"+res2);

	CString str1("cabbkage");
	CString str2="bk";
	CString message;
	int index=str1.Find('b');//正向查找字符
	if(index>=0)
	{
		message.Format("字符b的位置:%d",index);
		AfxMessageBox(message);
	}
	else
		AfxMessageBox("未找到字符b");
	index=str1.Find(str2);//查找字符串
	if(index>=0)
	{
		message.Format("字符串'bk'的位置:%d",index);
		AfxMessageBox(message);
	}
	else
		AfxMessageBox("未找到字符串'bk'");
	index=str1.ReverseFind('b');//反向查找字符
	if(index>=0)
	{
		message.Format("反向字符'b'位置:%d",index);
		AfxMessageBox(message);
	}
	else
		AfxMessageBox("未找到字符'b'");
	index=str1.FindOneOf("ka");//查找某一字符串中的任意字符
	if(index>=0)
	{
		message.Format("'ka'任意字符位置:%d",index);
		AfxMessageBox(message);
	}
	else
		AfxMessageBox("未找到'ka'中的任意字符");
	CString str1("cabbkage");
	str1.Replace("k","mn");//替换字符串
	AfxMessageBox("替换后:"+str1);
	str1.Remove('a');//移除字符
	AfxMessageBox("移除字符'a':"+str1);
	str1.Delete(1,3);//删除三个字符
	AfxMessageBox("删除字符:"+str1);
	CString str1("cabbkage");
	TCHAR thechar;
	thechar=str1.GetAt(0);
	//thechar=str1[0];
	int  nLength=str1.GetLength();
	str1.SetAt(nLength-1,thechar);
	CString message("将字符串的最后一个字符替换为第一个字符:\n\r");
	AfxMessageBox(message+str1);
	
	CString str;
	float f=12345.12345;
	str.Format(_T("浮点数显示两位小数:%.2f\n"), f);
	AfxMessageBox(str);
	int i=35;
	str.Format(_T("整数显示4位,前面补2个0:%.4d\n"), i);
	AfxMessageBox(str);

	CString str("352");
	int iTemp=atoi(str);
	CString str(_T("352"));
	int iTemp=_ttoi(str); 

	char* p="This is a test";
	CString s = p;//CString s(p);

	char *p; 
	CString str="hello"; 
	p=str.GetBuffer(str.GetLength()); 
	str.ReleaseBuffer();

	CString mCS=_T("cxl"); 
	char mch[20]; 
	memcpy(mch,mCS,mCS.GetLength()); */

	char *ch; 
	CString str=_T("cxl"); 
	ch=(LPSTR)(LPCTSTR)str; 


	













	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -