MFC学习笔记

一,控件操作类
1.0将光标移到最后
volumeCEdit.SetSel(2, 2, FALSE);从第几个位置,选中到第几个位置
volumeCEdit.SetFocus();

//1.获得文本框字符tools:: doprice(comcity, comcounty, volumeCEdit,heightCEdit, issuetime)

CString ss;
((CEdit*)GetDlgItem(IDC_EDIT1))->GetWindowText(ss);

1.0给文本框赋值
xiugaipro.SetWindowText(strpro);
GetDlgItem(IDC_EDIT1)->SetWindowText(tools::aaaaaa);
1.11获得comboxcontrol字符
CString ss;
selectpro.GetWindowText(ss);//无效
GetDlgItemText(IDC_COMBO1,strpro);//无效
int cindex= m_combobox.GetCurSel(); // 获取选定项的序号
m_combobox.GetLBText(cindex,text); // 获取选定项的文本内容
1.12//获得静态文本框的字符
CString str;
GetDlgItem(IDC_STATICA)->GetWindowText(str);//获得静态文本框的字符

1.2获得combo字符长度
comprovince.GetWindowTextLength()
1.3combo清空文字
comcounty.SetCurSel(-1);
1.4combo清空下拉列表
comcounty.ResetContent()
//2文本格式
strd.Format(_T(“地址长度:%d”), addresslens);
3.listcontrl插入
CString aaa;
daibanlist.InsertItem(0, aaa);//插入行;0是即将要插入的第一行,aaa是CString文本字符,
daibanlist.SetItemText(0, 1, aaa);//插入行的内容,0表示第一行,和insertitem的第一个参数一致。1表示第2列,aaa是内容;
4.Float 和CString互相转换
strtime.Format(L"%d", (t2 - t1));
m_bar.SetPaneText(1, strtime, 1);

char tmp_str[100] = { 0 };
strcpy_s(tmp_str, CT2A(strtime.GetBuffer()));
float f = atof(tmp_str)/1000;
strtime.Format(L"%f", f);
AfxMessageBox(strtime)

5,//测试程序用时
CString strtime, str1;
long t1 = GetTickCount();//开始计时,查询用时

            测试代码...................................

            long t2 = GetTickCount();
strtime.Format(L"%d", (t2 - t1));
char tmp_str[100] = { 0 };
strcpy_s(tmp_str, CT2A(strtime.GetBuffer()));
float f = atof(tmp_str)/1000;
strtime.Format(L"%f", f);
          //m_bar.SetPaneText(1, strtime, 1);

AfxMessageBox(strtime);

6.0//判断是不是0.5的整倍数
if(!fabs(actualweight * 2.0 - floor(actualweight * 2.0)) < 0.0001)
7.0画直线

void CMFCdrawmylineDlg::OnPaint()
{
CClientDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CDialog::OnPaint()

CDialog::OnPaint();
CPoint start, start1, start2, start3, start4, start5, start6, start7, start8, start9;
CPoint end, end1, end2, end3, end4, end5, end6, end7, end8, end9;

CPen cPen, cPen1, cPen2, cPen3, cPen4, cPen5, cPen6, cPen7, cPen8, cPen9;//创建画笔

cPen.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen1.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen2.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen3.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen4.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen5.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen6.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen7.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen8.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
cPen9.CreatePen(PS_SOLID, 2, RGB(135, 206, 250));
dc.SelectObject(&cPen);
dc.SelectObject(&cPen1);
dc.SelectObject(&cPen2);
dc.SelectObject(&cPen3);
dc.SelectObject(&cPen4);
dc.SelectObject(&cPen5);
dc.SelectObject(&cPen6);
dc.SelectObject(&cPen7);
dc.SelectObject(&cPen8);
dc.SelectObject(&cPen9);

/*start.x = 10;
start.y = 80;
end.x = 300;
end.y = 500;
dc.MoveTo(start);
dc.LineTo(end);*/

start1.x = 20;	
start1.y = 60;
end1.x = 20;
end1.y = 200;
dc.MoveTo(start1);
dc.LineTo(end1);

start2.x = 400;
start2.y = 80;
end2.x = 400;
end2.y = 150;
dc.MoveTo(start2);
dc.LineTo(end2);

start3.x = 400;
start3.y = 80;
end3.x = 400;
end3.y = 150;
dc.MoveTo(start3);
dc.LineTo(end3);

start4.x = 400;
start4.y = 80;
end4.x = 400;
end4.y = 150;
dc.MoveTo(start4);
dc.LineTo(end4);

start5.x = 400;
start5.y = 80;
end5.x = 400;
end5.y = 150;
dc.MoveTo(start5);
dc.LineTo(end5);

start6.x = 400;
start6.y = 80;
end6.x = 400;
end6.y = 150;
dc.MoveTo(start6);
dc.LineTo(end6);

start7.x = 400;
start7.y = 80;
end7.x = 400;
end7.y = 150;
dc.MoveTo(start7);
dc.LineTo(end7);

start8.x = 400;
start8.y = 80;
end8.x = 400;
end8.y = 150;
dc.MoveTo(start8);
dc.LineTo(end8);

}
8,只能输入纯小数
void tools::shuruchunxiaoshu(CEdit& edit)
{
//输入小数点和句号“.。”转换成“0.”
CString point;
//((CEdit*)GetDlgItem(id))->GetWindowText(point);
edit.GetWindowText(point);

if (point == '.' || point == "。" || point == "," || point == ',')
{
	edit.SetWindowText(_T("0."));
	
	edit.SetSel(2, 2, FALSE);
	edit.SetFocus();
}
//设置只能输入数字和一个小数点
//设置只能输入一个小数点
int len = edit.GetWindowTextLength();
CString str;

edit.GetWindowText(str);//str edit 
int a =0;//小数点个数
//设置职能输入数字
for (int nIndexm = 0; nIndexm < len; nIndexm++)
{
	if (str[nIndexm] == '.')
	{
		a++;
	}
	//int b =str.Replace('.', '.');
	if ((str[nIndexm] > '9' || str[nIndexm] < '0')&&str[nIndexm] != '.'||a>1)
	{
		str = str.Left(nIndexm) + str.Right(str.GetLength() - nIndexm - 1);
		edit.SetWindowText(str);		
		edit.SetSel(len, len, FALSE);
		edit.SetFocus();
	}
}

}
9列表排序

void CMFCdrawmylineDlg::OnLvnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
*pResult = 0;
}

void CMFCdrawmylineDlg::OnLvnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLISTVIEW pNMLV = reinterpret_cast(pNMHDR);
CString list0[8];
CString list1[8];
CString list2[8];
CString list3[8];
CString list4[8];
for (int k = 0; k < 8; k++)
{
list0[k] = pricelist.GetItemText(k, 0);
list1[k] = pricelist.GetItemText(k, 1);
list2[k] = pricelist.GetItemText(k, 2);
list3[k] = pricelist.GetItemText(k, 3);
list4[k] = pricelist.GetItemText(k, 4);
}
CString tem0[7],tem1[7],tem2[7],tem3[7], tem4[7];
if (a)
{
for (int i = 0; i < 8; i++)
{
for (int j = i + 1; j < 7; j++)
{
if (_ttoi(list2[i]) < _ttoi(list2[j]))
{
tem0[i] = list0[i];
tem1[i] = list1[i];
tem2[i] = list2[i];
tem3[i] = list3[i];
tem4[i] = list4[i];

				list0[i] = list0[j];
				list1[i] = list1[j];
				list2[i] = list2[j];
				list3[i] = list3[j];
				list4[i] = list4[j];

				list0[j] = tem0[i];
				list1[j] = tem1[i];
				list2[j] = tem2[i];
				list3[j] = tem3[i];
				list4[j] = tem4[i];
			}
		}
	}

	for (int i = 0; i <= 8; i++)
	{

		pricelist.SetItemText(i, 0, list0[i]);
		pricelist.SetItemText(i, 1, list1[i]);
		pricelist.SetItemText(i, 2, list2[i]);
		pricelist.SetItemText(i, 3, list3[i]);
		pricelist.SetItemText(i, 4, list4[i]);
	}
	a = 0;
}
else
{
	for (int i = 0; i < 8; i++)
	{
		for (int j = i + 1; j < 7; j++)
		{
			if (_ttoi(list2[i]) > _ttoi(list2[j]))
			{
				tem0[i] = list0[i];
				tem1[i] = list1[i];
				tem2[i] = list2[i];
				tem3[i] = list3[i];
				tem4[i] = list4[i];

				list0[i] = list0[j];
				list1[i] = list1[j];
				list2[i] = list2[j];
				list3[i] = list3[j];
				list4[i] = list4[j];

				list0[j] = tem0[i];
				list1[j] = tem1[i];
				list2[j] = tem2[i];
				list3[j] = tem3[i];
				list4[j] = tem4[i];
			}
		}
	}

	for (int i = 0; i <= 8; i++)
	{

		pricelist.SetItemText(i, 0, list0[i]);
		pricelist.SetItemText(i, 1, list1[i]);
		pricelist.SetItemText(i, 2, list2[i]);
		pricelist.SetItemText(i, 3, list3[i]);
		pricelist.SetItemText(i, 4, list4[i]);
	}
	a = 1;
}

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

*pResult = 0;

}
二、操作步骤类
------删除函数
1.删除定义和声明
2.也要删除cpp的映射

---------、新建弹出对话框
1、新建添加dlg资源对话框
2、添加新建的dlg类
3在其父窗口cpp中包含新建dlg类的头文件
4、添加菜单按钮事件处理程序(所属类为主菜单的类)
//系统自动添加的如下:
//主菜单的头文件中//public: afx_msg void add_pcc();
//头文件私有属性//DECLARE_MESSAGE_MAP()
//cpp中的消息映射:
//BEGIN_MESSAGE_MAP(addpcc, CDialogEx)
// ON_COMMAND(ID_32789, &addpcc::add_pcc)
//END_MESSAGE_MAP()
5、主菜单cpp的事件函数中创建新建类的实例/
//addpcc myaddpcc;
//myaddpcc.DoModal();
-------添加子窗口的初始化函数
1、类视图->找到窗口所属的类->右击->添加类向导->添加虚函数->选择oninitdlg
//自动添加的内容为:
//.h头文件的声明Public:virtual BOOL OnInitDialog();
//cpp文件函数的定义
//BOOL addpcc::OnInitDialog()
//{
// CDialogEx::OnInitDialog();

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

// return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
//}

三错误总结类
1、无法引用控件,已隐式声明
传入参数必须是指针类型;&
2._ConnectionPtr mycon2;无法声明为头文件的静态全局变量;解决办法,把他放到cpp文件中,当做全局变量

四数据库操作类(容易忘记的)
1insert dbo.temp(tem) values(‘待办事项’)
2delete dbo.temp where tem =‘值1’

5.表和表之间的复制insert into dbo.pcctext select* from dbo.pcc
6、查看行数select*from dbo.pcc
7.insert dbo.pcctext(Province,Province2,City,City2,County,County2,Province3,City3,County3,ProvinceAndCity,ProvinceAndCityAndCounty)
values(’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’,’%s’)
8,清除数据库的空格Select LTrim(RTrim(ProvinceAndCityAndCounty)) From dbo.pcctext

五、连接数据库类
_ConnectionPtr mycon;//头文件中声明连接
----,
第一步初始化ADO
//第二步:主CPP里初始化ADO BOOL CMFCsqlserverApp::InitInstance()
//初始化ADO
HRESULT hr;
hr = CoInitialize(NULL);
try
{
if (!SUCCEEDED(hr))
{
AfxMessageBox(_T(“初始化ADO COM失败!”));
}
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format(_T("%s"), e.ErrorMessage());
AfxMessageBox(errormessage);
}
//第三步:dLDcpp里添加初始化窗口的代码(加载省列表等等)
//加载省列表
if (!SUCCEEDED(mycon.CreateInstance(__uuidof(Connection))))
{
mycon = NULL;
AfxMessageBox(_T(“连接数据库失败!”));
}
_RecordsetPtr myrec(__uuidof(Record));

CString strcon = _T("Provider=SQLOLEDB;Data Source=XXX;Initial Catalog=管理系统;Persist Security Info=True;User ID=HHH;Password=123456");
if (!SUCCEEDED(mycon->Open(_bstr_t(strcon), _T(""), _T(""), adConnectUnspecified)))
{
	AfxMessageBox(_T("打开数据库失败!"));
}
else
{
	myrec = mycon->Execute(_T("select distinct province from dbo.pcc"), NULL, adCmdText);
	while (!myrec->adoEOF)
	{
		((CComboBox*)GetDlgItem(IDC_COMBO1))->AddString((_bstr_t)myrec->GetCollect(_T("province")));

		myrec->MoveNext();
	}
	myrec->Close();
	mycon->Close();
	myrec.Release();
	mycon.Release();
}

//-----数据库插入
mycon.CreateInstance(__uuidof(Connection));
_RecordsetPtr myrec(__uuidof(Record));

CString strcon = _T("Provider=SQLOLEDB;Data Source=XXX;Initial Catalog=管理系统;Persist Security Info=True;User ID=HHH;Password=123456");
mycon->Open(_bstr_t(strcon), _T(""), _T(""), adConnectUnspecified);

CString sql;
sql.Format( _T("insert dbo.temp(tem) values('%s')"),ss);

try
{
	mycon->Execute(_bstr_t(sql), 0, adCmdText);
}
catch (_com_error e)
{
	MessageBox(e.Description());
	return ;
}
mycon->Close();
mycon.Release();
AfxMessageBox(_T("添加成功"));

-----数据库查询返回记录集
mycon2.CreateInstance(__uuidof(Connection));
_RecordsetPtr myrec2(__uuidof(Record));

		strmysql.Format(_T("select distinct province from dbo.pcc where province = '%s'"), stra3);
		
		mycon2->Open(_bstr_t(strcon2), _T(""), _T(""), adConnectUnspecified);
		myrec2 = mycon2->Execute(_bstr_t(strmysql), NULL, adCmdText);

			if (!myrec2->GetadoEOF())//	如果记录集不为空
			{
				int countprovince = comprovince.FindString(-1, (_bstr_t)myrec2->GetCollect(_T("province")));//省所在combox的位置
			    comprovince.SetCurSel(countprovince);
				CMFCpriceDlg::OnCbnSelchangeCombo1();//调用省窗口事件,不调用就加载不出市
			    break;
			}
			myrec2->Close();
			mycon2->Close();
			myrec2.Release();
			mycon2.Release();
			//AfxMessageBox(stra3);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值