VC控制項使用小結

來源:互聯網
上載者:User

      這段時間通過使用MFC做車牌識別系統和媒體播放器,重新溫習了一下MFC,特別是控制項的使用,同時也學習了ADO技術、Socket網路編程、檔案傳輸、OpenCV、多線程、數位影像處理、Windows Media Player、CActiveMovie控制項、DricetShow多媒體等的知識,不過由於時間有限,有些東西也沒有深入,但是學習編程的方法都是相通的。覺得有時還是通過項目實踐學習的知識比較快,通過參考別人的代碼結合MSDN這些API參考文檔,加深對一門新技術的理解和消化對於初學者來時往往不失為一種快速的入門手段。廢話不多說了,進入主題吧,今天把我學習到的MFC控制項做一個小結吧,希望能給其他的人以協助吧。

    一、CListBox---列表框控制項

1、清除CListBox中的所有內容

兩種方法:

(1)ResetContent成員函數

CListBox m_listBox;

m_listBox.ResetContent();

 

(2)while(m_listBox.GetCount() !=0)

   m_listBox.DeleteString(0);

 

2、擷取當前選中行的文本值

CListBox m_listBox;

int index = m_listBox.GetCurSel();  //擷取選中的標號

CString strChoosed;

m_listBox.GetText(index,strChoosed); //擷取選中的標號對應的文本值

  二、CListCtrl---清單控制項

1、擷取當前選中行的文本值

CListCtrl m_listctrl;

int indexRow= m_listctrl.GetSelectionMark();   //擷取使用者當前選中的行標號
CString strChoosed =m_listctrl.GetItemText(indexRow,1);   //擷取當前選中行的第2列(列標號從0開始)對應的文本值

2、刪除所有列

    方法一:          while ( m_list.DeleteColumn (0))        因為你刪除了第一列後,後面的列會依次向上移動。

    方法二:       int nColumns = 4;       for (int i=nColumns-1; i>=0; i--)           m_list.DeleteColumn (i);

 

3、ClistCtrl如何在第二次添加資料時清除標題列?
我在使用MFC做媒體播放器時,在涉及到ADO資料庫查詢後將查詢結果放在CListCtrl控制項上。查詢一次後,當第二次查詢時,必須清除上次結果的所有內容,包括欄位標題頭和顯示行的內容。刪除顯示內容比較容易,直接使用CListCtrl類的DeleteAllItems就可以了;刪除欄位標題就有點麻煩了。這個問題困擾了我很久,終於在一篇CSDN的文章找到了相應的解決方案。樣本如下:
CListCtrl m_mediaListCtrl;  //m_mediaListCtrl是在對話方塊中綁定的CListCtrl變數
int i = 0, iCount = 0; 
m_mediaListCtrl.DeleteAllItems(); // 首先清空CListCtrl下面的所有值

//清空欄位標題頭
CHeaderCtrl* pHeadCtrl =  m_mediaListCtrl.GetHeaderCtrl(); //擷取該CListCtrl控制項變數對應的列表頭指標
iCount = pHeadCtrl->GetItemCount();  //擷取列表頭的列數

for(i = 0; i < iCount; i++) 
{ 
   m_mediaListCtrl.DeleteColumn(0);  //清空列表頭

}

 

 三、CTreeCtrl---樹控制項

1、載入表徵圖

//建立圖表列表

 CImageList m_imgPlaylist; 

CMediaPlayerApp * pApp = (CMediaPlayerApp *)AfxGetApp();
 m_imgPlaylist.Create(16,16,ILC_COLOR|ILC_MASK,2,2);  //建立表徵圖列表 表徵圖為單個表徵圖
 m_imgPlaylist.Add(pApp->LoadIcon(IDI_ICON_FILE1));
 m_imgPlaylist.Add(pApp->LoadIcon(IDI_ICON_FILE2));

//設定映像列表

 CTreeCtrl m_playlistTreeCtrl; m_playlistTreeCtrl.SetImageList(&m_imgPlaylist,TVSIL_NORMAL);

//為m_playlistTreeCtrl設定一個映像列表,使CtreeCtrl的節點顯示不同的表徵圖
 m_playlistTreeCtrl.SetBkColor(RGB(150,100,120));//設定m_playlistTreeCtrl的背景色為紅色

 //設定顯示風格
 DWORD dwStyle=GetWindowLong(m_playlistTreeCtrl.m_hWnd ,GWL_STYLE);//獲得樹控制原風格
 dwStyle|=TVS_EDITLABELS|TVS_HASBUTTONS|TVS_HASLINES|TVS_LINESATROOT;//設定風格
 ::SetWindowLong (m_mediaTreeCtrl .m_hWnd ,GWL_STYLE,dwStyle1);

//構建樹形結構

 HTREEITEM root = m_playlistTreeCtrl.InsertItem("PlayList",0,0,TVI_ROOT,TVI_LAST);
 m_playlistTreeCtrl.InsertItem("最喜歡的List",0,0,root2,TVI_LAST);
 m_playlistTreeCtrl.InsertItem("劉德華List",0,0,root2,TVI_LAST);
 m_playlistTreeCtrl.InsertItem("梁靜茹List",0,0,root2,TVI_LAST);
 m_playlistTreeCtrl.InsertItem("周杰倫List",0,0,root2,TVI_LAST);

 

2、 //清空播放清單CTreeCtrl控制項

CTreeCtrl m_mediaTreeCtrl;

 m_mediaTreeCtrl.DeleteAllItems();

 

四、CComboBox---下拉式方塊控制項

1、擷取組合框控制控制代碼

新增成員變數comboboxCtr;或者

CComboBox* comboboxCtr = (CComboBox*)GetDlgItem(BOX_ID);

 

2、點擊下拉框不顯示下拉式清單(問題)

對話方塊編輯器裡,把列表往下拖長一點即可。

3、設定展開下拉式清單、設定輸入框唯讀、清空列表內容

CComboBox* comboboxCtr = (CComboBox*)GetDlgItem(BOX_ID);

comboboxCtr->ShowDropDown(TRUE);

comboboxCtr->GetEditCtrl()->SetReadOnly(TRUE);

comboboxCtr->ResetContent();

4、 重設組合框,刪除所有的項 

 CComboBox m_mediaCombox;
 m_mediaCombox.ResetContent();

 

5、 擷取使用者當前在組合框中選擇的文本值

 int ichoosedIndex;  //選擇的id索引值
 CString strChoosed; 
ichoosedIndex = m_mediaCombox.GetCurSel();  //擷取使用者選擇的索引標號

 m_mediaCombox.GetLBText(ichoosedIndex,strChoosed);  //擷取相應的標號對應的文本值

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.