天行健,君子以自强不息
分类: C/C++
2010-12-19 12:10:37
1、文本框和按钮
void CCex05dlgDlg::OnTest()
{
// TODO: Add your control notification handler code here
//将第一个按钮设置成只读
CWnd * pwnd=GetDlgItem(IDC_EDIT1);
ASSERT(NULL!=pwnd);
CEdit * pedit=(CEdit * )pwnd;
pedit->SetReadOnly(true);
//在第二个按钮标题是上加A
CString title;
GetDlgItemText(IDC_EDIT2,title);
title+='A';
SetDlgItemText(IDC_EDIT2,title);
//将第三个编辑框加88
int idata;
idata=GetDlgItemInt(IDC_EDIT3);
idata+=88;
SetDlgItemInt(IDC_EDIT3,idata);
}
2、单选框复选框
void CDialog_6Dlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
CString str;
if(m_sex==0)
str="你是帅哥。\n";
else if(m_sex==1)
str="你是美女。\n";
else
str="性别未知。\n";
str+="-----------------\n你的爱好:\n";
CButton *pb=NULL;
pb=(CButton *)GetDlgItem(IDC_CHECK1);
if(pb&&pb->GetCheck()!=NULL)
str+="中国象棋\n";
pb=(CButton *)GetDlgItem(IDC_CHECK2);
if(pb&&pb->GetCheck()!=NULL)
str+="上网聊天\n";
pb=(CButton *)GetDlgItem(IDC_CHECK3);
if(pb&&pb->GetCheck()!=NULL)
str+="电脑游戏\n";
AfxMessageBox(str);
}
3、列表框
BOOL CLiebiaoDlg::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
int nitem=m_namelistbox.AddString("张三");
m_namelistbox.SetItemData(nitem,3);
nitem=m_namelistbox.AddString("李四");
m_namelistbox.SetItemData(nitem,2);
nitem=m_namelistbox.AddString("王五");
m_namelistbox.SetItemData(nitem,5);
return TRUE; // return TRUE unless you set the focus to a control
}
为listbox增加LBN_SELCHANGE消息响应函数如下:
void CLiebiaoDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
int nsel=m_namelistbox.GetCurSel();
if(nsel!=-1)
{
CString strname;
m_namelistbox.GetText(nsel,strname);
UINT uid=m_namelistbox.GetItemData(nsel);
CString str;
str.Format("你选中了%s,他的ID是%u",strname,uid);
AfxMessageBox(str);
}
}
4、进度条
BOOL CJindutiaoDlg::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
m_progress.SetRange(0,100);//设置进度条的上限和下限
m_progress.SetStep(5);//设置stepit每次移动多少进度
m_progress.SetPos(0);//设置进度条的初始进度
return TRUE; // return TRUE unless you set the focus to a control
}
//下一步按钮消息处理函数
void CJindutiaoDlg::OnButton1()
{
// TODO: Add your control notification handler code here
m_progress.StepIt();
}
5、滑标控件:
BOOL CHuabiaoDlg::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
m_slider.SetRange(0,100);//设置范围是0到100
m_slider.SetLineSize(4);//键盘方向键一次移动4单位
m_slider.SetPageSize(10);//用户单击一下鼠标移动10单位
return TRUE; // return TRUE unless you set the focus to a control
}
//确定按钮消息处理函数
void CHuabiaoDlg::OnOK()
{
// TODO: Add extra validation here
int nscale=m_slider.GetPos();
CString str;
str.Format("当前的刻度是%d",nscale);
AfxMessageBox(str);
CDialog::OnOK();
}
6、串口
为接收显示区和发送输入区添加m_strEditRXData和m_strEditTXData,类型为value Cstring,为comm控件添加m_ctrlComm变量,类型为control。
为
BOOL CScommtestDlg::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
m_ctrlcomm.SetCommPort(1);//选择com1
m_ctrlcomm.SetInputMode(1);//输入方式为二进制方式
m_ctrlcomm.SetInBufferSize(1024);//设置输入缓冲区的大小
m_ctrlcomm.SetOutBufferSize(512);//设置输出缓冲区的大小
m_ctrlcomm.SetSettings("9600,n,8,1");//设置波特率、校验位,数据位,停止位
if(!m_ctrlcomm.GetPortOpen())//打开串口
m_ctrlcomm.SetPortOpen(true);
m_ctrlcomm.SetRThreshold(1);//接收到一个字符就引发一次oncomm事件
m_ctrlcomm.SetInputLen(0);//设置当前接受区数据长度为0
m_ctrlcomm.GetInput();//先预读取缓冲区以清理残留数据
return TRUE; // return TRUE unless you set the focus to a control
}
//oncomm事件响应函数
void CScommtestDlg::OnOnCommMscomm1()
{
// TODO: Add your control notification handler code here
VARIANT variant_input;
COleSafeArray safearray_input;
LONG len,k;
BYTE rxdata[2048];
CString strtemp;
if(m_ctrlcomm.GetCommEvent()==2)
{
variant_input=m_ctrlcomm.GetInput();
safearray_input=variant_input;
len=safearray_input.GetOneDimSize();
m_strEditRXData+=strtemp;
for(k=0;k
safearray_input.GetElement(&k,rxdata+k);
for(k=0;k
{
BYTE bt=*(char *)(rxdata+k);
strtemp.Format("%c",bt);
m_strEditRXData+=strtemp;
}
}
UpdateData(FALSE);//更新输出
}
//手动发送按钮消息响应函数
void CScommtestDlg::OnButtonManulsend()
{ // TODO: Add your control notification handler code here
UpdateData(TRUE);//更新输入
m_ctrlcomm.SetOutput(COleVariant(m_strEditTXData));//发送输入框中的内容
}