MFC控件按钮的使用
MFC窗口的创建/消息映射机制
mfc.h
#include<afxwin.h>//mfc头文件//应用程序类
class MyApp:public CWinApp //继承于应用程序类
{
public://程序入口virtual BOOL InitInstance();
};//框架类
class MyFrame:public CFrameWnd
{
public:MyFrame();//声明宏 提供消息映射机制DECLARE_MESSAGE_MAP()afx_msg void OnLButtonDown( UINT, CPoint );afx_msg void OnChar( UINT, UINT,UINT ) ;afx_msg void OnPaint( );
};
mfc.cpp
#include "mfc.h"MyApp app;//全局应用程序对象,有且仅有一个BOOL MyApp::InitInstance()
{//创建窗口MyFrame * frame = new MyFrame;//显示和更新frame -> ShowWindow(SW_SHOWNORMAL);frame -> UpdateWindow();m_pMainWnd = frame;//保存指向应用程序的主窗口的指针return TRUE;//返回正常初始化
}//分界宏
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)ON_WM_LBUTTONDOWN()//鼠标左键按下ON_WM_CHAR()//键盘按下ON_WM_PAINT( )//绘图END_MESSAGE_MAP()MyFrame::MyFrame()
{Create(NULL,TEXT("mfc"));//窗口标题
}//鼠标
void MyFrame::OnLButtonDown( UINT, CPoint point)
{/*TCHAR buf[1024];wsprintf(buf,TEXT("x=%d,y=%d"),point.x,point.y);MessageBox(buf);*///mfc中的字符串 CStringCString str;str.Format(TEXT("x=%d,.,y=%d"),point.x,point.y);MessageBox(str);}//键盘
void MyFrame::OnChar(UINT key, UINT,UINT)
{CString str;str.Format(TEXT("按下了%c键"),key);MessageBox(str);
}//绘图
void MyFrame::OnPaint()
{CPaintDC dc(this);//CDC里找dc.TextOutW(100,100,TEXT("upup"));//画文字dc.Ellipse(10,10,100,100);//画椭圆//统计字符串长度int num=0;char *p ="aaaa";num=strlen(p);//统计宽字节的字符串长度wchar_t *p2=L"bbb";num = wcslen(p2);//char * 与 CString之间的转换//char * ->CStringchar *p3 = "ccc";CString str = CString(p3);//CString->char *CStringA temp;temp = str;char *pp = temp.GetBuffer();
}
复制按钮
//复制按钮
void CMy03EditCtrlDlg::OnBnClickedButton1()
{// TODO: 在此添加控件通知处理程序代码//获取到edit1的内容,然后给edit2赋值CString str;m_edit1.GetWindowTextW(str);m_edit2.SetWindowTextW(str);
}
退出按钮
//退出按钮
void CMy03EditCtrlDlg::OnBnClickedButton2()
{// TODO: 在此添加控件通知处理程序代码//exit(0);//退出程序//退出当前对话框//CDialog::OnOK();CDialog::OnCancel();
}
设置内容/获取内容(值)
将控件内容同步到变量中updatedata(true)
void CMy03EditCtrl_2Dlg::OnBnClickedButton1()
{// TODO: 在此添加控件通知处理程序代码//利用关value的方式 设置和改变edit的内容//设置内容m_text = TEXT("哈哈");//将变量内容同步到控件中UpdateData(FALSE);
}void CMy03EditCtrl_2Dlg::OnBnClickedButton2()
{// TODO: 在此添加控件通知处理程序代码//将控件的内容同步到变量中UpdateData(TRUE);MessageBox(m_text);
}
下拉框
//下拉框的添加操作m_cbx.AddString(TEXT("唐僧"));m_cbx.AddString(TEXT("孙悟空"));m_cbx.AddString(TEXT("猪八戒"));//设置默认选择第一项m_cbx.SetCurSel(0);//插入m_cbx.InsertString(3,TEXT("白龙马"));//删除m_cbx.DeleteString(2);//从0开始的//获取1号的索引的具体内容CString str;m_cbx.GetLBText(1,str);MessageBox(str);void CcomboBoxCtrlDlg::OnCbnSelchangeCombo1()
{// TODO: 在此添加控件通知处理程序代码//拿到索引位置,一有切换就会触发int index = m_cbx.GetCurSel();CString str;m_cbx.GetLBText(index,str);MessageBox(str);
}