MFC控件使用小结

CComoboBox:

CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_COMBO_TEST);
pCombo->InsertString(0, _T("男"));
pCombo->InsertString(1, _T("女"));
pCombo->InsertString(2, _T("秘密"));
pCombo->SetCurSel(1);

CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_COMBO_COUNTRY);
for(int i = 0; g_Country[i].country[0] != '\0'; i++)
{
pCombo->InsertString(i, g_Country[i].country);

}

CComboBox *pCombo = (CComboBox *)GetDlgItem(IDC_COMBO_CITY);
pCombo->GetLBText(pCombo->GetCurSel(), m_strCity);


CListCtrl m_listCtrl; //在控件属性设置中设置为:View:Report


static DWORD m_SortColum;
static BOOL m_bAs;

DWORD CSelectTopicDialog::m_SortColum = 0;
BOOL CSelectTopicDialog::m_bAs = TRUE;

static int CALLBACK MyListCompar(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

int   CALLBACK CSelectTopicDialog::MyListCompar(LPARAM   lParam1,   LPARAM   lParam2,   LPARAM   lParamSort) 

//通过传递的参数来得到CSortList对象指针,从而得到排序方式 
CListCtrl* pListCtrl = (CListCtrl*) lParamSort;

//通过ItemData来确定数据 

int   iCompRes; 
CString    szComp1 = pListCtrl->GetItemText(lParam1,m_SortColum);
CString    szComp2 = pListCtrl->GetItemText(lParam2,m_SortColum);

switch(m_SortColum) 

case(2): 
//以第一列为根据排序   编号
                //_ttol 
iCompRes=atof(szComp1) <=atof(szComp2)?-1:1; 
break; 

default: 
iCompRes=szComp1.Compare(szComp2); 
break; 

//根据当前的排序方式进行调整

if(m_bAs) 
return   iCompRes; 
else 
return   -iCompRes;


LVN_COLUMNCLICK:
void CSelectTopicDialog::OnColumnclickListTopicSelect(NMHDR* pNMHDR, LRESULT* pResult) 
{
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
      m_SortColum = pNMListView->iSubItem;
  m_bAs=!m_bAs;//升序还是降序
  

      int count = m_listCtrl.GetItemCount();   //行数
      for (int i=0; i<count;  i++)  
  {  
m_listCtrl.SetItemData(i,i);  
  }
  m_listCtrl.SortItems(MyListCompar, (LPARAM) &m_listCtrl);
      *pResult = 0;
}

CImageList m_imageList;


设置没项:

m_listCtrl.SetExtendedStyle( LVS_EX_FULLROWSELECT);
m_listCtrl.InsertColumn( 0, _T("网站来源"),LVCFMT_LEFT,85);
m_listCtrl.InsertColumn( 1, _T("产品"),LVCFMT_LEFT,110);
m_listCtrl.InsertColumn( 2, _T("价格"),LVCFMT_LEFT,80);

int iLine = m_listCtrl.GetItemCount();
m_listCtrl.InsertItem( iLine,_T("SOHU"));
m_listCtrl.SetItemText( iLine, 1,_T("IPHONE") );
m_listCtrl.SetItemText( iLine, 2,_T("5000") );

iLine = m_listCtrl.GetItemCount();
m_listCtrl.InsertItem( iLine,_T("TAOBAO"));
m_listCtrl.SetItemText( iLine, 1,_T("奔驰") );
m_listCtrl.SetItemText( iLine, 2,_T("500000") );

iLine = m_listCtrl.GetItemCount();
m_listCtrl.InsertItem( iLine,_T("GOOGLE"));
m_listCtrl.SetItemText( iLine, 1,_T("宝马") );
m_listCtrl.SetItemText( iLine, 2,_T("600000") );
清空:
//清空列表记录
while(m_listCtrl.DeleteItem(0));
选择:NM_CLICK
void CSelectTopicDialog::OnClickListTopicSelect(NMHDR* pNMHDR, LRESULT* pResult) 
{
// TODO: Add your control notification handler code here

int istat=m_listCtrl.GetSelectionMark();//用istat存放当前选定的是第几项
m_Tid = atol(m_listCtrl.GetItemText(istat,0));//获取当前项中的数据,0代表是第0列
m_strTopic = m_listCtrl.GetItemText(istat,1);

*pResult = 0;
}

右键:NM_RCLICK
void CPageAllNeed::OnNMRClickListNeed(NMHDR *pNMHDR, LRESULT *pResult)
{

int istat=m_listCtrl.GetSelectionMark();//用istat存放当前选定的是第几项
m_strSelectedTopic =m_listCtrl.GetItemText(istat,1);//获取当前项中的数据,0代表是第0列

POINT point;
HMENU hMenu, hSubMenu;
GetCursorPos(&point); //鼠标位置
hMenu = LoadMenu(NULL, 
MAKEINTRESOURCE(IDR_MENU_TOPICR)); // 加载菜单
hSubMenu = GetSubMenu(hMenu, 0);//得到子菜单(因为弹出式菜单是子菜单)

SetMenuDefaultItem(hSubMenu, -1, FALSE);//设置缺省菜单项,-1为无缺省项
SetForegroundWindow(); // 激活窗口并置前

TrackPopupMenu(hSubMenu, 0, 
point.x, point.y, 0, m_hWnd, NULL);
*pResult = 0;
}


窗口总在最前面:
::BringWindowToTop(m_hWnd);
::SetWindowPos(
m_hWnd, 
HWND_TOPMOST, 
0, 0, 0, 0, 
SWP_NOMOVE | SWP_NOSIZE);


CTreeCtrl  m_configTree;

m_basicPage.Create(IDD_DIALOG_BASICCONFIG, this);
m_cartPage.Create(IDD_DIALOG_CARTCONFIG, this);

CDC *pDc = GetDlgItem(IDC_STATIC_AREA)->GetWindowDC();
pDc->SetBkMode(TRANSPARENT);

CRect rcDlgs;
GetDlgItem(IDC_STATIC_AREA)->GetWindowRect(rcDlgs);
ScreenToClient(rcDlgs);
rcDlgs.top += 12;
rcDlgs.bottom -= 5;
rcDlgs.left += 5;
rcDlgs.right -= 5;
m_basicPage.MoveWindow(rcDlgs);  
m_basicPage.ShowWindow(SW_SHOW);
m_cartPage.MoveWindow(rcDlgs);  
m_cartPage.ShowWindow(SW_HIDE);


HTREEITEM  hItem1;
hItem1 = m_configTree.InsertItem("基本设置", 0, 1, TVI_ROOT, TVI_LAST);
        m_configTree.SetItemData(hItem1, (DWORD)&m_basicPage);

hItem1 = m_configTree.InsertItem("购物车", 0, 1, TVI_ROOT, TVI_LAST);
        m_configTree.SetItemData(hItem1, (DWORD)&m_cartPage);

TVN_SELCHANGED:
void CSysConfigDlg::OnSelchangedTreeSysconfig(NMHDR* pNMHDR, LRESULT* pResult) 
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here

HTREEITEM hCurrent = m_configTree.GetSelectedItem();
        if(hCurrent)
{
((CPropertyPage*)m_configTree.GetItemData(hCurrent))->ShowWindow(SW_SHOW);
UpdateWindow();
}

*pResult = 0;
}

TVN_SELCHANGING:
void CSysConfigDlg::OnSelchangingTreeSysconfig(NMHDR* pNMHDR, LRESULT* pResult) 
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here

HTREEITEM hCurrent = m_configTree.GetSelectedItem();
        if(hCurrent)
{
((CPropertyPage*)m_configTree.GetItemData(hCurrent))->ShowWindow(SW_HIDE);
//UpdateWindow();
}


*pResult = 0;
}

CTabCtrl m_tabCtrl;
       //创建3个子对话框,Border为 None,style:child,分别为:

CPageAllNeed m_allNeedPage;//1
CPageFavor m_favorPage;//2
CPageMessage m_msgPage;//3
CDialog *m_pDialog[MAX_DLG_PAGE];
int m_CurSelTab;

m_tabCtrl.InsertItem(0, _T("进程"));
m_tabCtrl.InsertItem(1, _T("网络"));
m_tabCtrl.InsertItem(2, _T("文件"));

m_tabCtrl.SetPadding(19);
m_tabCtrl.SetMinTabWidth(50);
m_tabCtrl.SetItemSize(CSize(50,25));

m_allNeedPage.Create(IDD_PAGE_ALLNEED, &m_tabCtrl);
m_favorPage.Create(IDD_PAGE_FAVOR, &m_tabCtrl);
m_msgPage.Create(IDD_PAGE_MESSAGE, &m_tabCtrl);

//设定在Tab内显示的范围
CRect rc;
m_tabCtrl.GetClientRect(rc);
  rc.top += 25;
// rc.bottom -= 8;
// rc.left += 8;
// rc.right -= 8;

m_allNeedPage.MoveWindow(&rc);
m_favorPage.MoveWindow(&rc);
m_msgPage.MoveWindow(&rc);

//m_allNeedPage.SetListCtrlSize();
//m_favorPage.SetListCtrlSize();
//m_msgPage.SetListCtrlSize();

//把对话框对象指针保存起来
m_pDialog[0] = &m_allNeedPage;
m_pDialog[1] = &m_favorPage;
m_pDialog[2] = &m_msgPage;
//显示初始页面
m_pDialog[0]->ShowWindow(SW_SHOW);
m_pDialog[1]->ShowWindow(SW_HIDE);
m_pDialog[2]->ShowWindow(SW_HIDE);

//保存当前选择
m_CurSelTab = 0;

TCN_SELCHANGE:

void CINeedDlg::OnTcnSelchangeTabMain(NMHDR *pNMHDR, LRESULT *pResult)
{
m_pDialog[m_CurSelTab]->ShowWindow(SW_HIDE);
m_CurSelTab = m_tabCtrl.GetCurSel();
if (m_pDialog[m_CurSelTab])
{
m_pDialog[m_CurSelTab]->ShowWindow(SW_SHOW);
}
*pResult = 0;
}

CEdit m_edtCtrl;//m_strPath
CStatic //GetDlgItem(IDC_STATIC_XXX)->SetWindowText();



CChecked:
        直接添加一个成员变量布尔类型
Radio:
直接添加一个INT类型的成员变量,但需要第一个radio和最后一个radio的下一个控件设置为group属性.i = 0,1,2,3分别代表第几个被选中了。怎么确定谁是radio的下一个控件呢?Format-->tab order

控件的移动操作:

GetWindowRect(rcRect);
rcRect.DeflateRect(0, 0, 0, -50);
MoveWindow(&rcRect);
CRect rcbtn;
m_btnUpPic.GetWindowRect(&rcbtn);
ScreenToClient(&rcbtn);
rcbtn.DeflateRect(0,50,0,-50);
m_btnUpPic.MoveWindow(&rcbtn);

m_btnClose.GetWindowRect(&rcbtn);
ScreenToClient(&rcbtn);
rcbtn.DeflateRect(0,50,0,-50);
m_btnClose.MoveWindow(&rcbtn);

m_btnSend.GetWindowRect(&rcbtn);
ScreenToClient(&rcbtn);
rcbtn.DeflateRect(0,50,0,-50);
m_btnSend.MoveWindow(&rcbtn);

打开文件:

static TCHAR BASED_CODE szFilter[] = _T("JPG (*.jpg)|*.jpg|")
_T("BMP文件 (*.bmp)|*.bmp|")
_T("PNG文件 (*.png)|*.png|")
/*_T("All Files (*.*)|*.*||")*/;

CFileDialog Filedlg(TRUE,_T(""), NULL, 
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
szFilter, NULL);
if (Filedlg.DoModal() != IDOK)
{
return;
}
m_szPicFile = Filedlg.GetPathName();


//设置加入计时器
WM_TIMER:参见POPUP弹窗 ON_WM_TIMER()
#define TIMER_ELAPSE_ID 100

SetTimer(TIMER_ELAPSE_ID, 1*1000, NULL);

void CPopupDlg::OnTimer(UINT nIDEvent) 
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case TIMER_ELAPSE_ID:
UpdateData(TRUE);
m_szTime.Format(_T("还剩: %2d 秒"), --m_lefttime);
UpdateData(FALSE);
if (m_lefttime == 0)
{
KillTimer(nIDEvent);
UpdateData(TRUE);
CDialog::OnOK();
}
break;
default:
break;
}

CDialog::OnTimer(nIDEvent);
}

KillTimer(TIMER_ELAPSE_ID);

将窗口移动到右下角:

// 获得桌面大小
CRect rectWorkArea;
SystemParametersInfo(SPI_GETWORKAREA,0,&rectWorkArea,SPIF_SENDCHANGE);   

// 获得对话框大小
CRect rectDlg;

m_showMsgDlg.GetWindowRect(&rectDlg);//m_showMsgDlg是你要显示在右下角的对话框
int nW = rectDlg.Width();
int nH = rectDlg.Height();

// 将窗口设置到右下角
::SetWindowPos(m_showMsgDlg.m_hWnd, HWND_BOTTOM,
rectWorkArea.right-nW-10,rectWorkArea.bottom-nH,nW,nH,SWP_NOZORDER);
//::AnimateWindow(m_showMsgDlg.m_hWnd,1000,AW_VER_NEGATIVE|AW_SLIDE); 
//m_showMsgDlg.ShowWindow(SW_SHOW);

// 将主界面也挪到右下角

GetWindowRect(&rectDlg);
nW = rectDlg.Width();
nH = rectDlg.Height();
::SetWindowPos(m_hWnd, HWND_BOTTOM,
rectWorkArea.right-nW-15,rectWorkArea.bottom-nH,nW,nH,SWP_NOZORDER);

模态对话框:
CXxxDlg dlg;
dlg.DoModal()==IDOK

非模态对话框:
CXxxDlg  m_showMsgDlg; 
m_showMsgDlg.Create(IDD_DIALOG_SHOWMSG, NULL);
m_showMsgDlg.ShowWindow(SW_SHOW);

绘图功能 WM_DRAWITEM();
#include <atlimage.h>
m_stShowPic.ModifyStyle(SS_TYPEMASK,SS_OWNERDRAW); 
void CShowPicMsgDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
if (nIDCtl == IDC_STATIC_SHOWPICMSG)
{
CRect rcClient;
CImage image;

if (PathFileExists(m_szPicPath))
{

CDC* pDC = GetDlgItem(IDC_STATIC_SHOWPICMSG)->GetWindowDC();

GetDlgItem(IDC_STATIC_SHOWPICMSG)->GetClientRect(&rcClient);

image.Load(m_szPicPath);
SetWindowPos(NULL, 0, 0, image.GetWidth(), image.GetHeight(), SWP_NOMOVE);
m_stShowPic.MoveWindow(0, 0, image.GetWidth(), image.GetHeight());


image.Draw( pDC->m_hDC,rcClient.left, 
rcClient.top, rcClient.Width(), 
rcClient.Height());


ReleaseDC(pDC);
}
return;
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}

拖动功能:

选中对话框中的acceptFiles属性
添加WM_DROPFILES
void CImageBaseCheckerDlg::OnDropFiles(HDROP hDropInfo) 
{
// TODO: Add your message handler code here and/or call default
UINT count;
TCHAR filePath[MAX_PATH] = {0};

count = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
if(count == 1)
{
DragQueryFile(hDropInfo, 0, filePath, sizeof(filePath));
m_szPath = filePath;
UpdateData(FALSE);
DragFinish(hDropInfo);

CDialog::OnDropFiles(hDropInfo);
return;

}
else
{   
//m_vectorFile.clear();
for(UINT i=0; i<count; i++)
{
int pathLen = DragQueryFile(hDropInfo, i, filePath, sizeof(filePath));
m_szPath = filePath;
//m_vectorFile.push_back(filePath);
//break;
}

UpdateData(FALSE);
DragFinish(hDropInfo);
}


CDialog::OnDropFiles(hDropInfo);
}

原文链接: MFC控件使用小结 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-468.html )

发表评论

0则评论给“MFC控件使用小结”