BitmapStatic.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:7k
- // BitmapStatic.cpp : implementation file
- //
- #include "stdafx.h"
- #include "BitmapStatic.h"
- #define TIMER_ID 100
- //It loads a "hand" cursor from the winhlp32.exe module.
- HCURSOR WINAPI GetSysHandCursor(void)
- {
- CString strWndDir;
- GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
- strWndDir.ReleaseBuffer();
-
- strWndDir += _T("\winhlp32.exe");
-
- HMODULE hModule = LoadLibrary(strWndDir);
- if(hModule)
- {
- HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
- if (hHandCursor)
- {
- FreeLibrary(hModule);
- return CopyCursor(hHandCursor);
- }
- }
- FreeLibrary(hModule);
- return NULL;
- }
- // CBitmapStatic
- CBitmapStatic::CBitmapStatic()
- {
- this->m_hHandCursor = NULL;
- this->m_bHoverControl = FALSE;
- this->m_hBitmapHandle = NULL;
- this->m_strCaption.Empty();
- }
- CBitmapStatic::~CBitmapStatic()
- {
- this->DeAttachObject();
- }
- BEGIN_MESSAGE_MAP(CBitmapStatic, CStatic)
- //{{AFX_MSG_MAP(CBitmapStatic)
- ON_WM_TIMER()
- ON_WM_PAINT()
- ON_WM_MOUSEMOVE()
- ON_WM_SETCURSOR()
- ON_WM_ERASEBKGND()
- ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CBitmapStatic message handlers
- //free bitmap object.
- void CBitmapStatic::DeAttachObject(void)
- {
- if(NULL != this->m_hBitmapHandle)
- {
- ::DeleteObject(this->m_hBitmapHandle);
- this->m_hBitmapHandle = NULL;
- }
- }
- void CBitmapStatic::AdjustWindowRect(void)
- {
- if(NULL == this->m_hBitmapHandle || !GetSafeHwnd()) return ;
- CBitmap *pBitmap = CBitmap::FromHandle(this->m_hBitmapHandle);
- if(NULL != pBitmap)
- {
- BITMAP bmpinfo;
- pBitmap->GetBitmap(&bmpinfo);
-
- CRect rcclt;
- CSize sizetxt, sizeds;
- CDC *pdc = this->GetDC();
- sizeds.cx = 2;
- sizeds.cy = 2;
-
- this->GetClientRect(&rcclt);
- if(!this->m_strCaption.IsEmpty())
- {
- sizeds.cx = 2;
- sizeds.cy = 4;
-
- sizetxt = pdc->GetTextExtent(this->m_strCaption);
- sizeds.cy = (sizeds.cy + 2) * (2*sizetxt.cx / rcclt.Width() + 2);
- }
- this->GetWindowRect(&rcclt);
- CWnd *parwnd = this->GetParent();
- if(NULL != parwnd)
- {
- parwnd->ScreenToClient(&rcclt);
- }
- this->SetWindowPos(NULL, rcclt.left, rcclt.top,
- bmpinfo.bmWidth + sizeds.cx,
- bmpinfo.bmHeight + sizeds.cy,
- SWP_SHOWWINDOW);
- }
- }
- BOOL CBitmapStatic::SetBitmap(UINT uResourceId)
- {
- this->DeAttachObject();
-
- this->m_hBitmapHandle = ::LoadBitmap(::AfxGetResourceHandle(),
- MAKEINTRESOURCE(uResourceId));
-
- this->AdjustWindowRect();
- this->Invalidate();
- return (NULL != this->m_hBitmapHandle);
- }
- BOOL CBitmapStatic::SetBitmap(HBITMAP hBitmapHandle)
- {
- this->DeAttachObject();
-
- this->m_hBitmapHandle = hBitmapHandle;
- this->AdjustWindowRect();
- this->Invalidate();
-
- return (NULL != this->m_hBitmapHandle);
- }
- BOOL CBitmapStatic::SetBitmap(CBitmap &pBitmapObj)
- {
- this->DeAttachObject();
-
- this->m_hBitmapHandle = (HBITMAP)pBitmapObj.GetSafeHandle();
- this->AdjustWindowRect();
- this->Invalidate();
-
- return (NULL != this->m_hBitmapHandle);
- }
- void CBitmapStatic::SetCaption(const CString &strCaption)
- {
- this->m_strCaption.Empty();
- this->m_strCaption = strCaption;
- this->AdjustWindowRect();
- this->Invalidate();
- }
- BOOL CBitmapStatic::OnEraseBkgnd(CDC* pDC)
- {
- CRect rc;
- CBrush brushbrd;
- this->GetClientRect(&rc);
- if(this->m_bHoverControl)
- {
- brushbrd.CreateSolidBrush(RGB(0, 0, 160));
- pDC->FillSolidRect(&rc, RGB(213, 227, 255));
- pDC->FrameRect(&rc, &brushbrd);
- brushbrd.DeleteObject();
- }
- else
- {
- pDC->FillSolidRect(rc, ::GetSysColor(COLOR_3DFACE));
- }
- return TRUE;
- }
- void CBitmapStatic::OnMouseMove(UINT nFlags, CPoint point)
- {
- if(!this->m_bHoverControl)
- {
- this->m_bHoverControl = TRUE;
- this->Invalidate();
- this->SetTimer(TIMER_ID, 100, NULL);
- }
- CStatic::OnMouseMove(nFlags, point);
- }
- void CBitmapStatic::OnTimer(UINT nIDEvent)
- {
- CRect rc;
- CPoint pt;
- ::GetCursorPos(&pt);
- this->GetClientRect(&rc);
- this->ClientToScreen(&rc);
- if(!rc.PtInRect(pt))
- {
- this->m_bHoverControl = FALSE;
- this->KillTimer(TIMER_ID);
-
- this->Invalidate();
- }
- CStatic::OnTimer(nIDEvent);
- }
- void CBitmapStatic::OnPaint()
- {
- //device context for painting.
-
- CPaintDC dc(this);
- if(!m_hBitmapHandle) return ;
- CDC memdc;
- BITMAP bmpinfo;
- CRect rcclt;
-
- this->GetClientRect(&rcclt);
- memdc.CreateCompatibleDC(&dc);
- //get the bitmap information -- width / height.
- CBitmap *pbmp = CBitmap::FromHandle(m_hBitmapHandle);
- ASSERT(NULL != pbmp);
- pbmp->GetBitmap(&bmpinfo);
-
- //draw specialed bitmap.
- memdc.SelectObject(m_hBitmapHandle);
- memdc.SetBkMode(TRANSPARENT);
- memdc.SelectObject((HFONT)::GetStockObject(SYSTEM_FONT));
- dc.BitBlt(rcclt.left + 1, rcclt.top + 1,
- min(rcclt.Width() - 2, bmpinfo.bmWidth),
- min(rcclt.Height()- 2, bmpinfo.bmHeight),
- &memdc, 0, 0, SRCCOPY);
- //draw static caption text.
- if(!m_strCaption.IsEmpty())
- {
- dc.SetBkMode(TRANSPARENT);
- COLORREF crcolor;
- CRect rctxt;
- CSize sizetxt = dc.GetTextExtent(m_strCaption);
- this->GetClientRect(&rcclt); rctxt = rcclt;
- HFONT hfont = (HFONT)dc.SelectObject(::GetStockObject(SYSTEM_FONT));
- //make text out rect.
- rctxt.left = (rcclt.Width() - sizetxt.cx) / 2 + sizetxt.cx / 8;
- rctxt.top = rcclt.bottom - (rcclt.Height()- sizetxt.cy) / 4;
- rctxt.right = rcclt.left + min(rcclt.left + sizetxt.cx, rcclt.right);
- rctxt.bottom= rctxt.top + min(rcclt.top + sizetxt.cy, rcclt.bottom);
-
- //if the mouse is in window client rect then
- //change the text color and inflate textout rect.
- if(this->m_bHoverControl)
- {
- crcolor = dc.SetTextColor(RGB(255, 0, 0));
- rctxt.InflateRect(2, 1);
- }
- else
- {
- //use default font color -- black.
- crcolor = dc.SetTextColor(RGB(0, 0, 0));
- }
- dc.DrawText(m_strCaption, &rctxt, DT_CENTER | DT_VCENTER | DT_WORDBREAK);
- //restore the device context.
- dc.SetTextColor(crcolor);
- dc.SelectObject(hfont);
- }
- //delete GDI object.
- pbmp->Detach();
- memdc.DeleteDC();
- }
- void CBitmapStatic::PreSubclassWindow()
- {
- //we want to get mouse clicks via STN_CLICKED.
- DWORD dwStyle = this->GetStyle();
- ::SetWindowLong(this->GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
- this->m_hHandCursor = GetSysHandCursor();
-
- CStatic::PreSubclassWindow();
- }
- void CBitmapStatic::OnClicked()
- {
- CWnd *parWnd = this->GetParent();
- ASSERT(NULL != parWnd);
- parWnd->SendMessage(UWM_STATICCLICKED, (WPARAM)GetSafeHwnd(), 0);
- }
- BOOL CBitmapStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- if(this->m_bHoverControl && this->m_hHandCursor)
- {
- ::SetCursor(this->m_hHandCursor);
- return TRUE;
- }
- return FALSE;
- }