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() 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); 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属性 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_strPath = filePath; //CheckNow(m_strPath); 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_strPath = filePath; //m_vectorFile.push_back(filePath); //break; } //DoAllCheck(); //CheckNow(m_strPath); UpdateData(FALSE); DragFinish(hDropInfo); } CDialog::OnDropFiles(hDropInfo); } 托盘图标 OnInitDialog() { NOTIFYICONDATA m_tnid; m_tnid.cbSize=sizeof(NOTIFYICONDATA);//设置结构大小// m_tnid.hWnd=this->m_hWnd;//设置图标对应的窗口 m_tnid.uFlags=NIF_MESSAGE|NIF_ICON|NIF_TIP;//图标属性 m_tnid.uCallbackMessage=WM_ICON_NOTIFY;//应用程序定义的回调消息ID CString szToolTip; szToolTip=_T("INeed 客户端程序"); _tcscpy(m_tnid.szTip, szToolTip);//帮助信息 m_tnid.uID=IDR_MAINFRAME;//应用程序图标 m_tnid.hIcon=m_hIcon;//图标句柄 PNOTIFYICONDATA m_ptnid=&m_tnid; ::Shell_NotifyIcon(NIM_ADD,m_ptnid);//增加图标到系统盘 } #define WM_ICON_NOTIFY WM_USER+100 ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification) LRESULT OnTrayNotification(WPARAM wParam, LPARAM lParam); LRESULT CXXXXDlg::OnTrayNotification( WPARAM wParam, LPARAM lParam ) { switch(lParam) { case WM_LBUTTONDOWN: { AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOWNORMAL); SetForegroundWindow(); break; } case WM_RBUTTONUP: { POINT point; HMENU hMenu, hSubMenu; GetCursorPos(&point); //鼠标位置 hMenu = LoadMenu(NULL, MAKEINTRESOURCE(IDR_MENU_TRAY)); // 加载菜单 hSubMenu = GetSubMenu(hMenu, 0);//得到子菜单(因为弹出式菜单是子菜单) SetForegroundWindow(); // 激活窗口并置前 TrackPopupMenu(hSubMenu, 0, point.x, point.y, 0, m_hWnd, NULL); } } return 1; } OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } if(nID == SC_MINIMIZE) { ShowWindow(FALSE); //隐藏窗口 } } OnClose() { NOTIFYICONDATA nd = {0}; nd.cbSize = sizeof(NOTIFYICONDATA); nd.hWnd = m_hWnd; nd.uID = IDR_MAINFRAME; nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP; nd.uCallbackMessage = WM_ICON_NOTIFY; nd.hIcon = m_hIcon; Shell_NotifyIcon(NIM_DELETE, &nd); } 注册热键 #define VK_S 0X53 #define VK_C 0X43 #define VK_Z 0X5A #define VK_Q 0X51 BOOL CNetMsgClientDlg::PreTranslateMessage(MSG* pMsg) { if (!bLogged) return CDialog::PreTranslateMessage(pMsg); if(pMsg->message==WM_KEYDOWN && pMsg->wParam == VK_RETURN && (GetKeyState(VK_CONTROL)&0x80)) { if (GetFocus() == GetDlgItem(IDC_EDIT_QUICKSND)) { OnButtonQsnd(); return TRUE; } else { OnButtonSend(); return TRUE; } } else if (pMsg->message==WM_KEYDOWN && pMsg->wParam == VK_Q && (GetKeyState(VK_CONTROL)&0x80)) { OnButtonQuicksnd(); return TRUE; } else if (pMsg->message == WM_HOTKEY) { if(pMsg->wParam == 1 && //注册的热键ID m_bSendShow == FALSE) { m_bSendShow = TRUE; OnButtonSend(); return TRUE; } else if(pMsg->wParam == 2 && //注册的热键ID m_bConfigShow == FALSE) { m_bConfigShow = TRUE; OnButtonConfig(); return TRUE; } else if(pMsg->wParam == 3) { if (m_isDlgMaximized) { m_isDlgMaximized = FALSE; ShowWindow(SW_RESTORE); SetForegroundWindow(); return TRUE; } else if (m_isDlgMaximized == FALSE) { //m_trayIcon.ShowIcon(); m_isDlgMaximized = TRUE; ShowWindow(SW_HIDE); //隐藏窗口 } } } return CDialog::PreTranslateMessage(pMsg); } //注册系统热键 RegisterHotKey( this->m_hWnd, 1, MOD_ALT, VK_S); RegisterHotKey( this->m_hWnd, 2, MOD_ALT, VK_C); RegisterHotKey( this->m_hWnd, 3, MOD_ALT, VK_Z); UnregisterHotKey(this->m_hWnd, 1); UnregisterHotKey(this->m_hWnd, 2); UnregisterHotKey(this->m_hWnd, 3);
BOOL g_bWillExit = FALSE; CXxxDlg::OnInitDialog() { CWinThread *m_pWinThread; HANDLE m_hEvent; g_bWillExit = FALSE; m_pWinThread = AfxBeginThread((AFX_THREADPROC)threadGetMsg, (LPVOID)this); m_pWinThread->SuspendThread(); m_pWinThread->m_bAutoDelete = FALSE; m_pWinThread->ResumeThread(); m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if (m_hEvent == NULL) { return FALSE; } } DWORD WINAPI threadGetMsg(PVOID param) { CINeedDlg *pMainDlg = (CINeedDlg *)param; DWORD dwTimeOut = 10*1000; while(g_bWillExit == FALSE) { WaitForSingleObject(pMainDlg->m_hEvent, dwTimeOut); ResetEvent(pMainDlg->m_hEvent); if (g_bWillExit == FALSE && pMainDlg->m_bPauseMsg == FALSE) { PostMessage(pMainDlg->m_hWnd, WM_SHOW_MSGDLG, 0, 0); } } return 0; } OnClose() { g_bWillExit = TRUE; SetEvent(m_hEvent); if (m_pWinThread) { WaitForSingleObject(m_pWinThread->m_hThread, INFINITE); delete m_pWinThread; m_pWinThread = NULL; } }