1 
  2 #ifdef    BTNST_USE_BCMENU
  3 LRESULT CButtonST::OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu) 
  4 {
  5     LRESULT lResult;
  6     if (BCMenu::IsMenu(pMenu))
  7         lResult = BCMenu::FindKeyboardShortcut(nChar, nFlags, pMenu);
  8     else
  9         lResult = CButton::OnMenuChar(nChar, nFlags, pMenu);
 10     return lResult;
 11 // End of OnMenuChar
 12 #endif
 13 
 14 #ifdef    BTNST_USE_BCMENU
 15 void CButtonST::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
 16 {
 17     BOOL bSetFlag = FALSE;
 18     if (lpMeasureItemStruct->CtlType == ODT_MENU)
 19     {
 20         if (IsMenu((HMENU)lpMeasureItemStruct->itemID) && BCMenu::IsMenu((HMENU)lpMeasureItemStruct->itemID))
 21         {
 22             m_menuPopup.MeasureItem(lpMeasureItemStruct);
 23             bSetFlag = TRUE;
 24         } // if
 25     } // if
 26     if (!bSetFlag) CButton::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
 27 // End of OnMeasureItem
 28 #endif
 29 
 30 void CButtonST::OnEnable(BOOL bEnable) 
 31 {
 32     CButton::OnEnable(bEnable);
 33     
 34     if (bEnable == FALSE)    
 35     {
 36         CWnd*    pWnd = GetParent()->GetNextDlgTabItem(this);
 37         if (pWnd)
 38             pWnd->SetFocus();
 39         else
 40             GetParent()->SetFocus();
 41 
 42         CancelHover();
 43     } // if
 44 // End of OnEnable
 45 
 46 void CButtonST::OnKillFocus(CWnd * pNewWnd)
 47 {
 48     CButton::OnKillFocus(pNewWnd);
 49     CancelHover();
 50 // End of OnKillFocus
 51 
 52 void CButtonST::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
 53 {
 54     CButton::OnActivate(nState, pWndOther, bMinimized);
 55     if (nState == WA_INACTIVE)    CancelHover();
 56 // End of OnActivate
 57 
 58 void CButtonST::OnCancelMode() 
 59 {
 60     CButton::OnCancelMode();
 61     CancelHover();
 62 // End of OnCancelMode
 63 
 64 BOOL CButtonST::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
 65 {
 66     // If a cursor was specified then use it!
 67     if (m_hCursor != NULL)
 68     {
 69         ::SetCursor(m_hCursor);
 70         return TRUE;
 71     } // if
 72 
 73     return CButton::OnSetCursor(pWnd, nHitTest, message);
 74 // End of OnSetCursor
 75 
 76 void CButtonST::CancelHover()
 77 {
 78     // Only for flat buttons
 79     if (m_bIsFlat)
 80     {
 81         if (m_bMouseOnButton)
 82         {
 83             m_bMouseOnButton = FALSE;
 84             Invalidate();
 85         } // if
 86     } // if
 87 // End of CancelHover
 88 
 89 void CButtonST::OnMouseMove(UINT nFlags, CPoint point)
 90 {
 91     CWnd*                wndUnderMouse = NULL;
 92     CWnd*                wndActive = this;
 93     TRACKMOUSEEVENT        csTME;
 94 
 95     CButton::OnMouseMove(nFlags, point);
 96 
 97     ClientToScreen(&point);
 98     wndUnderMouse = WindowFromPoint(point);
 99 
100     // If the mouse enter the button with the left button pressed then do nothing
101     if (nFlags & MK_LBUTTON && m_bMouseOnButton == FALSE) return;
102 
103     // If our button is not flat then do nothing
104     if (m_bIsFlat == FALSE) return;
105 
106     if (m_bAlwaysTrack == FALSE)    wndActive = GetActiveWindow();
107 
108     if (wndUnderMouse && wndUnderMouse->m_hWnd == m_hWnd && wndActive)
109     {
110         if (!m_bMouseOnButton)
111         {
112             m_bMouseOnButton = TRUE;
113 
114             Invalidate();
115 
116 #ifdef    BTNST_USE_SOUND
117             // Play sound ?
118             if (m_csSounds[0].lpszSound)
119                 ::PlaySound(m_csSounds[0].lpszSound, m_csSounds[0].hMod, m_csSounds[0].dwFlags);
120 #endif
121 
122             csTME.cbSize = sizeof(csTME);
123             csTME.dwFlags = TME_LEAVE;
124             csTME.hwndTrack = m_hWnd;
125             ::_TrackMouseEvent(&csTME);
126         } // if
127     } else CancelHover();
128 // End of OnMouseMove
129 
130 // Handler for WM_MOUSELEAVE
131 LRESULT CButtonST::OnMouseLeave(WPARAM wParam, LPARAM lParam)
132 {
133     CancelHover();
134     return 0;
135 // End of OnMouseLeave
136 
137 BOOL CButtonST::OnClicked() 
138 {    
139     SetFocus();
140 
141 #ifdef    BTNST_USE_SOUND
142     // Play sound ?
143     if (m_csSounds[1].lpszSound)
144         ::PlaySound(m_csSounds[1].lpszSound, m_csSounds[1].hMod, m_csSounds[1].dwFlags);
145 #endif
146 
147     if (m_bIsCheckBox)
148     {
149         m_nCheck = !m_nCheck;
150         Invalidate();
151     } // if
152     else
153     {
154         // Handle the menu (if any)
155 #ifdef    BTNST_USE_BCMENU
156         if (m_menuPopup.m_hMenu)
157 #else
158         if (m_hMenu)
159 #endif
160         {
161             CRect    rWnd;
162             GetWindowRect(rWnd);
163 
164             m_bMenuDisplayed = TRUE;
165             Invalidate();
166 
167 #ifdef    BTNST_USE_BCMENU
168             BCMenu* psub = (BCMenu*)m_menuPopup.GetSubMenu(0);
169             if (m_csCallbacks.hWnd)    ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)psub, m_csCallbacks.lParam);
170             DWORD dwRetValue = psub->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, this, NULL);
171 #else
172             HMENU hSubMenu = ::GetSubMenu(m_hMenu, 0);
173             if (m_csCallbacks.hWnd)    ::SendMessage(m_csCallbacks.hWnd, m_csCallbacks.nMessage, (WPARAM)hSubMenu, m_csCallbacks.lParam);
174             DWORD dwRetValue = ::TrackPopupMenuEx(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rWnd.left, rWnd.bottom, m_hParentWndMenu, NULL);
175 #endif
176 
177             m_bMenuDisplayed = FALSE;
178             Invalidate();
179 
180             if (dwRetValue)
181                 ::PostMessage(m_hParentWndMenu, WM_COMMAND, MAKEWPARAM(dwRetValue, 0), (LPARAM)NULL);
182         } // if
183         else
184         {
185             // Handle the URL (if any)
186             if (_tcslen(m_szURL) > 0)
187             {
188                 SHELLEXECUTEINFO    csSEI;
189 
190                 memset(&csSEI, 0sizeof(csSEI));
191                 csSEI.cbSize = sizeof(SHELLEXECUTEINFO);
192                 csSEI.fMask = SEE_MASK_FLAG_NO_UI;
193                 csSEI.lpVerb = _T("open");
194                 csSEI.lpFile = m_szURL;
195                 csSEI.nShow = SW_SHOWMAXIMIZED;
196                 ::ShellExecuteEx(&csSEI);
197             } // if
198         } // else
199     } // else
200 
201     return FALSE;
202 // End of OnClicked
203 
204 void CButtonST::DrawItem(LPDRAWITEMSTRUCT lpDIS)
205 {
206     CDC*    pDC = CDC::FromHandle(lpDIS->hDC);
207 
208     // Checkbox?
209     if (m_bIsCheckBox)
210     {
211         m_bIsPressed  =  (lpDIS->itemState & ODS_SELECTED) || (m_nCheck != 0);
212     } // if
213     else    // Normal button OR other button style 
214     {
215         m_bIsPressed = (lpDIS->itemState & ODS_SELECTED);
216 
217         // If there is a menu and it's displayed, draw the button as pressed
218         if (
219 #ifdef    BTNST_USE_BCMENU
220             m_menuPopup.m_hMenu 
221 #else
222             m_hMenu 
223 #endif
224             && m_bMenuDisplayed)    m_bIsPressed = TRUE;
225     } // else
226 
227     m_bIsFocused  = (lpDIS->itemState & ODS_FOCUS);
228     m_bIsDisabled = (lpDIS->itemState & ODS_DISABLED);
229 
230     CRect itemRect = lpDIS->rcItem;
231 
232     pDC->SetBkMode(TRANSPARENT);
233 
234     // Prepare draw paint button background
235 
236     // Draw transparent?
237     if (m_bDrawTransparent)
238         PaintBk(pDC);
239     else
240         OnDrawBackground(pDC, &itemRect);
241 
242     // Draw button border
243     OnDrawBorder(pDC, &itemRect);
244 
245     // Read the button's title
246     CString sTitle;
247     GetWindowText(sTitle);
248 
249     CRect captionRect = lpDIS->rcItem;
250 
251     // Draw the icon
252     if (m_csIcons[0].hIcon)
253     {
254         DrawTheIcon(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
255     } // if
256 
257     if (m_csBitmaps[0].hBitmap)
258     {
259         pDC->SetBkColor(RGB(255,255,255));
260         DrawTheBitmap(pDC, !sTitle.IsEmpty(), &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
261     } // if
262 
263     // Write the button title (if any)
264     if (sTitle.IsEmpty() == FALSE)
265     {
266         DrawTheText(pDC, (LPCTSTR)sTitle, &lpDIS->rcItem, &captionRect, m_bIsPressed, m_bIsDisabled);
267     } // if
268 
269     if (m_bIsFlat == FALSE || (m_bIsFlat && m_bDrawFlatFocus))
270     {
271         // Draw the focus rect
272         if (m_bIsFocused)
273         {
274             CRect focusRect = itemRect;
275             focusRect.DeflateRect(33);
276             pDC->DrawFocusRect(&focusRect);
277         } // if
278     } // if
279 // End of DrawItem
280 
281 void CButtonST::PaintBk(CDC* pDC)
282 {
283     CClientDC clDC(GetParent());
284     CRect rect;
285     CRect rect1;
286 
287     GetClientRect(rect);
288 
289     GetWindowRect(rect1);
290     GetParent()->ScreenToClient(rect1);
291 
292     if (m_dcBk.m_hDC == NULL)
293     {
294         m_dcBk.CreateCompatibleDC(&clDC);
295         m_bmpBk.CreateCompatibleBitmap(&clDC, rect.Width(), rect.Height());
296         m_pbmpOldBk = m_dcBk.SelectObject(&m_bmpBk);
297         m_dcBk.BitBlt(00, rect.Width(), rect.Height(), &clDC, rect1.left, rect1.top, SRCCOPY);
298     } // if
299 
300     pDC->BitBlt(00, rect.Width(), rect.Height(), &m_dcBk, 00, SRCCOPY);
301 // End of PaintBk
Posted on 2007-04-12 11:46 艾凡赫 阅读(976) 评论(0)  编辑 收藏 引用 所属分类: MFC技术

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理