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
示例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); } }
【示例代碼稍後補充】