标签:des style blog http color 使用 os io
一 MFC控件m_wndList.SetTextBkColor(CLR_NONE);
示例1:
1、新建MFC对话框应用程序DlgCtrl。
2、可视化编辑界面控件
3、ctrl+w 绑定控件对应的成员变量
4、ctrl+w 绑定列表框项的双击事件和按钮的单击事件
5、***Dlg.cpp中的主要实现代码(部分是自动生成的)
#include "stdafx.h" #include "DlgCtrl.h" #include "DlgCtrlDlg.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() ///////////////////////////////////////////////////////////////////////////// // CDlgCtrlDlg dialog CDlgCtrlDlg::CDlgCtrlDlg(CWnd* pParent /*=NULL*/) : CDialog(CDlgCtrlDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDlgCtrlDlg) m_strFilePath = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDlgCtrlDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgCtrlDlg) DDX_Control(pDX, IDC_PLAYTIMES, m_cboPlayCount); DDX_Control(pDX, IDC_REPLAY, m_btnReplay); DDX_Control(pDX, IDC_VIDEO, m_animateVideo); DDX_Control(pDX, IDC_LIST_FILENAME, m_listFileName); DDX_Text(pDX, IDC_EDIT_FILEPATH, m_strFilePath); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgCtrlDlg, CDialog) //{{AFX_MSG_MAP(CDlgCtrlDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_OPEN_FILE, OnOpenFile) ON_LBN_DBLCLK(IDC_LIST_FILENAME, OnDblclkListFilename) ON_WM_CLOSE() ON_BN_CLICKED(IDC_REPLAY, OnReplay) ON_BN_CLICKED(IDC_PLAY, OnPlay) ON_BN_CLICKED(IDC_STOP, OnStop) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgCtrlDlg message handlers BOOL CDlgCtrlDlg::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_cboPlayCount.EnableWindow(FALSE);//次数不可用 return TRUE; // return TRUE unless you set the focus to a control } void CDlgCtrlDlg::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 CDlgCtrlDlg::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 CDlgCtrlDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDlgCtrlDlg::OnOpenFile() { // TODO: Add your control notification handler code here char lpszFilter[]="视频(*.avi)|*.avi|视频(*.rmvb)|*.rmvb|所有文件(*.*)|*.*||"; CFileDialog dlg(TRUE,NULL,NULL, OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, lpszFilter); if(IDOK!=dlg.DoModal()) return; //编辑框中更新文件路径 m_strFilePath=dlg.GetPathName(); UpdateData(FALSE); //列表加入文件名 //m_listFileName.AddString(dlg.GetFileName()); CString strName=dlg.GetFileName(); if (LB_ERR==m_listFileName.FindString(-1,strName)) { //添加文件名称 int nIndex=m_listFileName.AddString(strName); CString *pPath=new CString; *pPath=m_strFilePath; //通过附加数据,保存文件路径 m_listFileName.SetItemData(nIndex,(DWORD)pPath); } //动画控件播放动画 play(); } void CDlgCtrlDlg::play() { int nCount=1;//保存播放的次数 if (m_btnReplay.GetCheck()) { int nSel=m_cboPlayCount.GetCurSel(); switch(nSel) { case 0://重复播放 nCount=-1; break; case 1://重复一次 nCount=2; break; case 2://重复两次 nCount=3; break; } } m_animateVideo.Open(m_strFilePath); m_animateVideo.Play(0,-1,nCount); } void CDlgCtrlDlg::OnDblclkListFilename() { //获取双击时,当前的选择项 int nSel=m_listFileName.GetCurSel(); if (LB_ERR==nSel)return; //播放当前选中的文件 CString *pPath=(CString*) m_listFileName.GetItemData(nSel); m_strFilePath=*pPath; //播放 play(); UpdateData(FALSE); AfxMessageBox(*pPath); } void CDlgCtrlDlg::OnClose() { //字符串路径在附加数据里,关闭时要删除 for (int i=0;i<m_listFileName.GetCount();i++) { CString *pPath= (CString *)m_listFileName.GetItemData(i); delete pPath; } CDialog::OnClose(); } void CDlgCtrlDlg::OnReplay() { // TODO: Add your control notification handler code here if (m_btnReplay.GetCheck()) { m_cboPlayCount.EnableWindow(TRUE); } else { m_cboPlayCount.EnableWindow(FALSE); } } void CDlgCtrlDlg::OnPlay() { // TODO: Add your control notification handler code here play(); } void CDlgCtrlDlg::OnStop() { // TODO: Add your control notification handler code here m_animateVideo.Stop(); }
示例2:
1、新建MFC对话框应用程序DlgCtrl2。
2、可视化编辑界面控件
3、ctrl+w 绑定控件对应的成员变量
4、ctrl+w 绑定列表框项的双击事件和按钮的单击事件
5、***Dlg.cpp中的主要实现代码(部分是自动生成的)
#include "stdafx.h" #include "DlgCtrl2.h" #include "DlgCtrl2Dlg.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() ///////////////////////////////////////////////////////////////////////////// // CDlgCtrl2Dlg dialog CDlgCtrl2Dlg::CDlgCtrl2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CDlgCtrl2Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CDlgCtrl2Dlg) m_edit_a = 0; m_edit_b = 0; m_edit_sum = 0; //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CDlgCtrl2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgCtrl2Dlg) DDX_Control(pDX, IDC_LIST_RIGHT, m_list_right); DDX_Control(pDX, IDC_LIST_LEFT, m_list_left); DDX_Text(pDX, IDC_EDIT_A, m_edit_a); DDX_Text(pDX, IDC_EDIT_B, m_edit_b); DDX_Text(pDX, IDC_EDIT_SUM, m_edit_sum); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgCtrl2Dlg, CDialog) //{{AFX_MSG_MAP(CDlgCtrl2Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_EQUAL, OnButtonEqual) ON_BN_CLICKED(IDC_BUTTON_ToLeft, OnBUTTONToLeft) ON_BN_CLICKED(IDC_BUTTON_ToLeftALL, OnBUTTONToLeftALL) ON_BN_CLICKED(IDC_BUTTON_ToRight, OnBUTTONToRight) ON_BN_CLICKED(IDC_BUTTON_ToRightALL, OnBUTTONToRightALL) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgCtrl2Dlg message handlers BOOL CDlgCtrl2Dlg::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_list_left.AddString("北京"); m_list_left.AddString("上海"); m_list_left.AddString("广州"); m_list_left.AddString("深圳"); return TRUE; // return TRUE unless you set the focus to a control } void CDlgCtrl2Dlg::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 CDlgCtrl2Dlg::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 CDlgCtrl2Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CDlgCtrl2Dlg::OnButtonEqual() { // TODO: Add your control notification handler code here UpdateData(TRUE); m_edit_sum=m_edit_a+m_edit_b; UpdateData(FALSE); } void CDlgCtrl2Dlg::OnBUTTONToLeft() { // TODO: Add your control notification handler code here int sel=m_list_right.GetCurSel(); if(sel==LB_ERR) return; CString str; m_list_right.GetText(sel,str);//获取右边值 m_list_left.AddString(str);//添加到左边 m_list_right.DeleteString(sel);//删除右边 } void CDlgCtrl2Dlg::OnBUTTONToLeftALL() { // TODO: Add your control notification handler code here CString str; while(m_list_right.GetCount()>0) { m_list_right.GetText(0,str); m_list_left.AddString(str); m_list_right.DeleteString(0); } } void CDlgCtrl2Dlg::OnBUTTONToRight() { // TODO: Add your control notification handler code here //获取左边列表选择项 int sel=m_list_left.GetCurSel(); if(sel==LB_ERR) return; CString str; m_list_left.GetText(sel,str);//获取左边值 m_list_right.AddString(str);//添加到右边 m_list_left.DeleteString(sel);//删除左边 } void CDlgCtrl2Dlg::OnBUTTONToRightALL() { // TODO: Add your control notification handler code here CString str; while(m_list_left.GetCount()>0) { m_list_left.GetText(0,str); m_list_right.AddString(str); m_list_left.DeleteString(0); } }
【示例代码稍后补充】
C++MFC编程笔记day09 MF界面控件的使用1,布布扣,bubuko.com
标签:des style blog http color 使用 os io
原文地址:http://blog.csdn.net/pukuimin1226/article/details/38553079