vc的一些简单用法
一: CStatic的用法
public:
CMyStaticDlg(CWnd* pParent = NULL); // standard constructor
CStatic m_Dynamic;
//设置文本
CStatic* static1 = (CStatic*)GetDlgItem(IDC_STATIC1);
static1->SetWindowText("明11");
//动态创建
if (!IsWindow(m_Dynamic.m_hWnd))//m_Dynamic是在窗口类中定义的CStatic对象
m_Dynamic.Create("欢迎您!",WS_CHILD,CRect(110,50,180,100),this,1525);
m_Dynamic.ShowWindow(SW_SHOW);
//修改按钮风格
CStatic* static2 = (CStatic*)GetDlgItem(IDC_STATIC2);
static2->ModifyStyle(0,SS_BITMAP);//修改按钮风格
static2->SetBitmap(LoadBitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP1))); //设置位图
////加载图标
CStatic* static3 = (CStatic*)GetDlgItem(IDC_STATIC3);
static3->ModifyStyle(0,SS_ICON);//修改按钮风格
static3->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));//加载图标
二:文本框用法
//设置文本
CEdit* m_edit1 = (CEdit*)GetDlgItem(IDC_EDIT1);
m_edit1->SetWindowText("hello");
//获取文本
CString c_user;
m_edit1->GetWindowText(c_user);
//取消编辑
CEdit* m_edit2 = (CEdit*)GetDlgItem(IDC_EDIT2);
m_edit2->Undo();
//密码文本
CEdit* m_edit3 = (CEdit*)GetDlgItem(IDC_EDIT3);
m_edit3->SetPasswordChar('*');
//数字编辑框
CEdit* m_edit4 = (CEdit*)GetDlgItem(IDC_EDIT4);
m_edit4->ModifyStyle(0,ES_NUMBER);
//设置控件焦点
GotoDlgCtrl((CEdit*)GetDlgItem(IDC_EDIT1)); //设置控件焦点
三: 按钮类
//设置按钮风格、样式
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CButton* button1 =(CButton*) GetDlgItem(IDC_BUTTON1);
button1->ModifyStyle(0,BS_ICON);
button1->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));
//复选
CButton* m_button = (CButton*)GetDlgItem(IDC_CHECK1);
m_button->SetCheck(TRUE);
//单选
m_button = (CButton*)GetDlgItem(IDC_RADIO1);
m_button->SetCheck(TRUE);
//鼠标形状
SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
四 列表框
//添加数据
CListBox* pList = (CListBox*)GetDlgItem(IDC_LIST2);
pList->AddString("语文");
pList->AddString("数学");
pList = (CListBox*)GetDlgItem(IDC_LIST3);
pList->AddString("Visual C++");
pList->AddString("SQL Server");
CListBox* listbox1 = (CListBox*)GetDlgItem(IDC_LIST1);
//Add data to ListBox
listbox1->AddString("孙海霞");
listbox1->AddString("王兵");
//查找数据
CListBox* listbox2 = (CListBox*)GetDlgItem(IDC_LIST2);
listbox2->SelectString(0,"英语");
//当前数据
CButton* button = (CButton*)GetDlgItem(IDC_BUTTON2);
CListBox* listbox3 = (CListBox*)GetDlgItem(IDC_LIST3);
CString str;
int index = listbox3->GetCurSel();
if (index >=0)
{
listbox3->GetText(index,str);
button->SetWindowText(str);
}
//列举文件
CListBox* listbox4 = (CListBox*)GetDlgItem(IDC_LIST4);
listbox4->Dir(DDL_DIRECTORY|DDL_SYSTEM,"c:\\*.*");
五 组合框
//添加数据
CComboBox* combobox1 = (CComboBox*)GetDlgItem(IDC_COMBO1);
combobox1->AddString("男");
combobox1->AddString("女");
//清空数据
CComboBox* combobox2 = (CComboBox*)GetDlgItem(IDC_COMBO2);
combobox2->ResetContent();
//当前数据
CComboBox* combobox3 = (CComboBox*)GetDlgItem(IDC_COMBO3);
CString text;
combobox3->GetLBText(combobox3->GetCurSel(),text);
MessageBox(text,"提示");
//列举磁盘
CComboBox* combobox4 = (CComboBox*)GetDlgItem(IDC_COMBO4);
combobox4->Dir(DDL_DRIVES|DDL_EXCLUSIVE,"c:\*.*");
*/
// 显示窗体
CProviderInfo provider;
CCustomer customer;
provider.Create(CProviderInfo::IDD);
provider.ShowWindow(SW_SHOW);
六 连接数据库
// LIST列表框------------------------------------------1
m_grid.SetExtendedStyle(LVS_EX_FLATSB
|LVS_EX_FULLROWSELECT
|LVS_EX_HEADERDRAGDROP
|LVS_EX_ONECLICKACTIVATE
|LVS_EX_GRIDLINES);
m_grid.InsertColumn(0,"编号",LVCFMT_LEFT,100,0);
m_grid.InsertColumn(1,"姓名",LVCFMT_LEFT,100,1);
m_grid.InsertColumn(2,"年龄",LVCFMT_LEFT,100,2);
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
CString sql;
sql.Format("select* from employees order by id desc");
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet((_bstr_t)sql);
while(m_AdoConn.m_pRecordset->adoEOF==0)
{
m_grid.InsertItem(0,"");
m_grid.SetItemText(0,0,(char*)(_bstr_t)m_pRecordset->GetCollect("id"));
m_grid.SetItemText(0,1,(char*)(_bstr_t)m_pRecordset->GetCollect("name"));
m_grid.SetItemText(0,2,(char*)(_bstr_t)m_pRecordset->GetCollect("age"));
m_pRecordset->MoveNext();
}
m_AdoConn.ExitConnect();
//m_list.DeleteAllItems();//删除
//数据判断----------------------------------2
if(m_id.IsEmpty())
{
MessageBox("编号不能为空!");
return;
}
if(m_name.IsEmpty())
{
MessageBox("姓名不能为空!");
return;
}
//下拉框--------------------------------------3
m_combo.GetLBText(m_combo.GetCurSel(),sex);//-----
try
{
m_pRecordset->Move((long)pos,vtMissing);//--------
m_pRecordset->PutCollect("编号",(_bstr_t)m_id);
}
catch(...)
{
MessageBox("操作失败");
return;
}
MessageBox("保存成功.");
m_grid.DeleteAllItems();//------
//列表框事件-------------------------------------------4
void C--::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
pos=m_grid.GetSelectionMark();
m_oid=m_grid.GetItemText(pos,0);
*pResult = 0;
}