學習VS2010 -------- 桌面信息提示信息編程

桌面信息提示信息編程

   目的在系統運行有各種各樣的信息提示,直接從桌面的右下角彈出對話框給出提示,該提示對話框可以採用系統信息提示對話框,也可以採用自定義對話框進行提示。

(1)      利用VS2010生成對話框形式的工程文件;

(2)      在對話框中加入與桌面提示信息相關的控件,包括外觀類型、動畫類型、透明度、動畫速度、自動關閉、關閉時間等;

(3)       


(4)      爲各個列表框添加數據

其中,外觀類型列表框數據爲MS Office 2000;MS Office XP;MS Office2003;MS VS.NET 2005;Windows XP;MSN Messenger;MS Office 2007 Blue;MS Office 2007Black;MS Office 2007 Silver;MS Office 2007 Aqua;

動畫類型列表框數據包括:Noanimation;Unfold;Slide;Fade;

(5)      自繪圖標列表控件

通常的列表控件只能顯示字符串,爲了能夠使得列表中既能顯示字符,又能顯示圖標,需要對列表控件進行重載,建立以CComboBox爲基類的繼承類CIconComboBox,在其中重載構造函數,CompareItem,DrawItem,MeasureItem等函數,實現列表控件的自繪。重點的需要重載以下類

a)      構造函數的重載

      CIconComboBox(CMFCToolBarImages& Icons); 以便以引用的方式調用位圖

CIconComboBox::CIconComboBox(CMFCToolBarImages&Icons) :

                 m_Icons (Icons)

{

}

b)      重寫MeasureItem()函數,便於控制列表類的行間距

void CIconComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)

{

     lpMIS->itemHeight = 25;

     lpMIS->itemWidth = 25;

}

c)      最重要的,重載DrawItem()函數

void CIconComboBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)

{

         CDC* pDC= CDC::FromHandle(lpDIS->hDC);

         ASSERT_VALID (pDC);

         HBRUSH           brBackground;

         COLORREF      clText;

         if(lpDIS->itemState & ODS_SELECTED) 

         {

                   brBackground =GetSysColorBrush (COLOR_HIGHLIGHT);

                   clText =afxGlobalData.clrTextHilite;

         }

         else

         {

                   brBackground =GetSysColorBrush (COLOR_WINDOW);

                   clText =afxGlobalData.clrWindowText;

         }

         CRect rectItem = lpDIS->rcItem;

         ::FillRect (lpDIS->hDC,&rectItem, brBackground);

         intnIndex = lpDIS->itemID;

         if(nIndex > 0)

         {

                   CAfxDrawState ds;

                   m_Icons.SetTransparentColor(afxGlobalData.clrBarFace);

                   m_Icons.PrepareDrawImage(ds);

                   m_Icons.Draw (pDC,rectItem.left + 1, rectItem.top + 1, nIndex - 1);

                   m_Icons.EndDrawImage (ds);

         }

         CString strText;

         GetLBText (nIndex, strText);

         CRect rectText = rectItem;

         rectText.left += 20;

         CFont* pOldFont = pDC->SelectObject(&afxGlobalData.fontRegular);

         pDC->SetBkMode (TRANSPARENT);

         pDC->SetTextColor (clText);

         pDC->DrawText (strText, rectText,DT_SINGLELINE | DT_VCENTER);

         pDC->SelectObject (pOldFont);

}

d)      重載CompareItem()實現排序規則,由於本例中控件屬性已經選擇不排序,所以此處不再起作用

e)       #include"IconComboBox.h"加入StdAfx.h

f)        定義變量CIconComboBoxm_IconCtrl;

g)      將控件屬性的Owner Draw屬性設置爲Variable,將屬性Hasstrings設置爲TRUE

(6)      利用ClassWizard建立對話框中的各種變量

(7)      初始化

m_TransCtrl.SetRange(10,255);

         m_CloseTimeCtrl.SetRange(1,5);

         m_AnimSpeedCtrl.SetRange(1,100);

m_Icons.SetImageSize(CSize(32,32));

         m_Icons.Load(IDB_ICONS);

         m_IconsSmall.SetImageSize(CSize(16,16));

         m_IconsSmall.Load(IDB_ICONS_SMALL);

 

         for(inti=0;i<m_Icons.GetCount();i++)

         {

                   CStringstrText;

                   if(i==0)

                            strText= _T("No Icon");

                   else

                   {

                            strText.Format(_T("Icon %d"),i);

                   }

 

                   m_IconCtrl.AddString(strText);

         }

       UpdateData(FALSE);  //手動調用自繪控件的DrawItem()函數

(8)      在顯示按鈕上進行程序設計,設置信息提示界面參數並顯示信息提示框

   1)  設置界面風格特性,VS2010提供了多種界面風格供系統選擇

     switch(m_LookStyle)

         {

         case 0:

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManager));

                   break;

         case 1:

CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerOfficeXP));

                   break;

         case 2:

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerOffice2003));

                   break;

         case 3:

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerVS2005));

                   break;

         case 4:

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerWindows));

                   break;

         case 5:

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMSNVisualManager));

                   break;

         case 6:

                   CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue);

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerOffice2007));

                   break;

         case 7:

                   CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_ObsidianBlack);

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerOffice2007));

                   break;

         case 8:

                   CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Silver);

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerOffice2007));

                   break;

         case 9:

                   CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_Aqua);

                   CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS (CMFCVisualManagerOffice2007));

                   break;

         }

   2)設置參數

CMFCDesktopAlertWnd*pPopup = new CMFCDesktopAlertWnd;

         pPopup->SetAnimationType((CMFCPopupMenu::ANIMATION_TYPE)m_AnimType);

         pPopup->SetAnimationSpeed(m_AnimSpeed);

         pPopup->SetTransparency((BYTE)m_Trans);

         pPopup->SetSmallCaption(m_IsSmallCaption);

         pPopup->SetAutoCloseTime(m_AotuClose ? m_CloseTime * 1000 : 0);

3)  自繪風格的提示界面

    a) 利用資源建立對話框,注意將屬性設置爲Child,透明屬性設置爲TRUE

b) 對該對話框建立類,基類爲CMFCDesktopAlertDialog

c) 調用

pPopup->Create(this, IDD_DIALOG1,

                                     NULL,

                                     CPoint(-1,-1),

                                     RUNTIME_CLASS(CMyPopDlg));

 4 ) 系統信息提示界面的調用

        CMFCDesktopAlertWndInfo params;

                   if(m_IconSel>0)

                   {

                            params.m_hIcon= m_Icons.ExtractIconW(m_IconSel-1);

                            params.m_strText= m_Text;

                            params.m_strURL= m_LinkText;

                            params.m_nURLCmdID= 101;

                            pPopup->Create(this, params, NULL, CPoint(-1,-1));

                   }

 5)設置ICON及標題

HICONhIcon = (HICON) ::LoadImage (::AfxGetResourceHandle (),

                   MAKEINTRESOURCE(IDR_MAINFRAME),

                                     IMAGE_ICON,::GetSystemMetrics (SM_CXSMICON), ::GetSystemMetrics (SM_CYSMICON), 0);

         pPopup->SetIcon(hIcon, FALSE);

         pPopup->SetWindowText(_T("信息"));

(9)      最後運行界面

 

選擇顯示將看到屏幕右下角彈出相應的提示信息框。

 

發佈了26 篇原創文章 · 獲贊 11 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章