TitledPicsWnd.cpp
上传用户:cuiyan8037
上传日期:2007-01-01
资源大小:215k
文件大小:17k
源码类别:
ListView/ListBox
开发平台:
Visual C++
- // TitledPicsWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "TitledPicsWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CTitledPicsWnd
- IMPLEMENT_DYNCREATE(CTitledPicsWnd, CScrollView)
- CTitledPicsWnd::CTitledPicsWnd()
- {
- m_bFocusIsDone = FALSE ;
- m_bStretchBitmaps = FALSE ;
- m_bWantsBestFit = TRUE ;
- m_dwWndBkgndColour = ::GetSysColor(COLOR_WINDOW);
- m_iSelectedItem = TPW_NONE ;
- m_sizSection.cx = TPW_SIZE_BESTFIT ;
- m_sizSection.cy = TPW_SIZE_BESTFIT ;
- m_uSpacing = TPW_SIZE_DEFAULT_SPACING ;
- m_uSectionsPerRow = 1 ;
- }
- CTitledPicsWnd::~CTitledPicsWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CTitledPicsWnd, CScrollView)
- //{{AFX_MSG_MAP(CTitledPicsWnd)
- ON_WM_DESTROY()
- ON_WM_LBUTTONDOWN()
- ON_WM_VSCROLL()
- ON_WM_LBUTTONDBLCLK()
- ON_WM_RBUTTONDOWN()
- ON_WM_SIZE()
- ON_WM_KILLFOCUS()
- ON_WM_SETFOCUS()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTitledPicsWnd drawing
- void CTitledPicsWnd::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- CSize sizeTotal;
- // TODO: calculate the total size of this view
- sizeTotal.cx = sizeTotal.cy = 100;
- SetScrollSizes(MM_TEXT, sizeTotal);
- }
- void CTitledPicsWnd::OnDraw(CDC* pDC)
- {
- BOOL bWasCreated ;
- CBitmap *pBmp, *pBmpOld ;
- CDC dcTemp ;
- CDocument *pDoc = GetDocument();
- CFont *pFntOld ;
- COLORREF dwColour ;
- CRect rctBmp, rctClip ;
- CSize sizBmp, sizFont ;
- CString strTitle ;
- int x, y ;
- UINT n, uSize, uID, uTextHeight ;
- // Update the stored window background colour, for when we need to remove the
- // focus rectangle.
- pDC->GetClipBox(&rctClip);
- dwColour = pDC->GetPixel(rctClip.TopLeft() );
- if (dwColour != (COLORREF)-1)
- m_dwWndBkgndColour = dwColour ;
- dcTemp.CreateCompatibleDC(NULL);
- if (m_sizSection.cx == TPW_SIZE_BESTFIT || m_sizSection.cy == TPW_SIZE_BESTFIT)
- CalculateBestFit();
- else
- CalculateFit();
- SetNewPageSize();
- // Now they have proper sizes.
- uSize = GetSize();
- for (n = 0 ; n < uSize ; n++)
- {
- bWasCreated = FALSE ;
- if (m_aryDwBitmapTypes.GetAt(n) == TPW_BITMAPTYPE_OBJECT)
- pBmp = (CBitmap *)m_aryDwBitmaps.GetAt(n);
- else
- {
- bWasCreated = TRUE ;
- pBmp = new CBitmap ;
- uID = (UINT)m_aryDwBitmaps.GetAt(n);
- pBmp->LoadBitmap(uID);
- }
- // Get top-left position for bitmap.
- sizBmp = GetBitmapSize(pBmp);
- sizFont = GetFontSize();
- uTextHeight = (TPW_NUM_LINESTEXT * sizFont.cy);
- x = n % m_uSectionsPerRow ;
- y = n / m_uSectionsPerRow ;
- if (m_bStretchBitmaps)
- {
- x = (x * m_sizSection.cx) + m_uSpacing ;
- y = (y * m_sizSection.cy) + m_uSpacing ;
- }
- else
- {
- x = (x * m_sizSection.cx) +
- ( (m_sizSection.cx - sizBmp.cx) / 2) +
- m_uSpacing ;
- y = (y * m_sizSection.cy) +
- ( (m_sizSection.cy - sizBmp.cy - uTextHeight) / 2) +
- m_uSpacing ;
- }
- // Paint the bitmap, if necessary.
- if (m_bStretchBitmaps)
- {
- rctBmp.SetRect(x, y, x + m_sizSection.cx - (2 * m_uSpacing),
- y + m_sizSection.cy - uTextHeight - (2 * m_uSpacing) );
- }
- else
- rctBmp.SetRect(x, y, x + sizBmp.cx, y + sizBmp.cy);
- if (pDC->RectVisible(&rctBmp) )
- {
- pBmpOld = (CBitmap *)dcTemp.SelectObject(pBmp);
- if (m_bStretchBitmaps)
- {
- pDC->StretchBlt(x, y, rctBmp.Width(), rctBmp.Height(),
- &dcTemp, 0, 0, sizBmp.cx, sizBmp.cy, SRCCOPY);
- }
- else
- {
- pDC->BitBlt(x, y, rctBmp.Width(), rctBmp.Height(), &dcTemp, 0,
- 0, SRCCOPY);
- }
- dcTemp.SelectObject(pBmpOld);
- }
- // Draw the title.
- strTitle = m_aryStrTitles.GetAt(n);
- rctBmp.SetRect(x, y + rctBmp.Height(), x + rctBmp.Width(),
- y + m_sizSection.cy);
- pFntOld = (CFont *)pDC->SelectObject(&m_fntSmall);
- pDC->DrawText(strTitle, rctBmp, DT_CENTER|DT_VCENTER|DT_WORDBREAK);
- pDC->SelectObject(pFntOld);
- if (bWasCreated)
- {
- pBmp->DeleteObject();
- delete pBmp ;
- }
- }
- // We may need to show the focus. We specifically should NOT show it if it has
- // already been dealt with elsewhere; in this case, scrolling already takes
- // care of replacing it, even though this routine gets called as part of a
- // scroll, and when this window gets the focus it is also taken care of.
- // However, an example of when we DO need to do it here is if the parent window
- // is resized - the focus is not actually lost, but repainting occurs and there
- // is nothing else to put the focus back. For these reasons we use a flag to
- // indicate if it is safe for us to put back the focus rectangle from here.
- if (!m_bFocusIsDone && GetFocus() == this)
- {
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem);
- }
- m_bFocusIsDone = FALSE ; // Reset the flag.
- dcTemp.DeleteDC();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTitledPicsWnd diagnostics
- #ifdef _DEBUG
- void CTitledPicsWnd::AssertValid() const
- {
- CScrollView::AssertValid();
- }
- void CTitledPicsWnd::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CTitledPicsWnd message handlers
- void CTitledPicsWnd::SetSectionSize(int cx, int cy) {
- if ( (cx < 0 && cx != TPW_SIZE_BESTFIT) ||
- (cy < 0 && cy != TPW_SIZE_BESTFIT) )
- {
- ASSERT(FALSE);
- }
- m_sizSection.cx = cx ;
- m_sizSection.cy = cy ;
- if (m_sizSection.cx == TPW_SIZE_BESTFIT ||
- m_sizSection.cy == TPW_SIZE_BESTFIT)
- {
- m_bWantsBestFit = TRUE ;
- }
- else m_bWantsBestFit = FALSE ;
- } int CTitledPicsWnd::Add(UINT uBmpID, CString strTitle) { int iIndex1, iIndex2, iIndex3 ;
- iIndex1 = m_aryDwBitmapTypes.Add(TPW_BITMAPTYPE_RESOURCE);
- iIndex2 = m_aryDwBitmaps.Add( (DWORD)uBmpID);
- iIndex3 = m_aryStrTitles.Add(strTitle);
- ASSERT(iIndex1 == iIndex2);
- ASSERT(iIndex2 == iIndex3);
- return iIndex1 ;
- }
- int CTitledPicsWnd::Add(CBitmap *pBmp, CString strTitle) { int iIndex1, iIndex2, iIndex3 ;
- iIndex1 = m_aryDwBitmapTypes.Add(TPW_BITMAPTYPE_OBJECT);
- iIndex2 = m_aryDwBitmaps.Add( (DWORD)pBmp);
- iIndex3 = m_aryStrTitles.Add(strTitle);
- ASSERT(iIndex1 == iIndex2);
- ASSERT(iIndex2 == iIndex3);
- return iIndex1 ;
- } void CTitledPicsWnd::RemoveAt(UINT uIndex, UINT uCount) { m_aryDwBitmapTypes.RemoveAt(uIndex, uCount);
- m_aryDwBitmaps.RemoveAt(uIndex, uCount);
- m_aryStrTitles.RemoveAt(uIndex, uCount);
- } void CTitledPicsWnd::RemoveAll(void) { m_aryDwBitmapTypes.RemoveAll();
- m_aryDwBitmaps.RemoveAll();
- m_aryStrTitles.RemoveAll();
- } void CTitledPicsWnd::SetSize(UINT uNewSize) { m_aryDwBitmapTypes.SetSize(uNewSize);
- m_aryDwBitmaps.SetSize(uNewSize);
- m_aryStrTitles.SetSize(uNewSize);
- } void CTitledPicsWnd::CalculateBestFit(void) {
- BOOL bWasCreated ;
- CBitmap *pBmp ;
- CRect rctClient ;
- CSize sizBmp, sizMax(0, 0), sizFont ;
- UINT uSize, n, uID, uWidth ;
- if (m_hWnd != NULL && GetSize() > 0)
- {
- GetClientRect(&rctClient);
- // Go through all the bitmaps and make a note of the widest and highest.
- uSize = GetSize();
- for (n = 0 ; n < uSize ; n++)
- {
- bWasCreated = FALSE ;
- if (m_aryDwBitmapTypes.GetAt(n) == TPW_BITMAPTYPE_OBJECT)
- pBmp = (CBitmap *)m_aryDwBitmaps.GetAt(n);
- else
- {
- bWasCreated = TRUE ;
- pBmp = new CBitmap ;
- uID = (UINT)m_aryDwBitmaps.GetAt(n);
- pBmp->LoadBitmap(uID);
- }
- sizBmp = GetBitmapSize(pBmp);
- if (sizBmp.cx > sizMax.cx)
- sizMax.cx = sizBmp.cx ;
- if (sizBmp.cy > sizMax.cy)
- sizMax.cy = sizBmp.cy ;
- if (bWasCreated)
- {
- pBmp->DeleteObject();
- delete pBmp ;
- }
- }
- // Now see how many whole bitmaps will fit into the width of the window, and
- // how much width each can take up.
- sizMax.cx += (2 * TPW_SIZE_DEFAULT_SPACING);
- sizMax.cy += (2 * TPW_SIZE_DEFAULT_SPACING);
- uWidth = rctClient.Width() - ::GetSystemMetrics(SM_CXVSCROLL);
- m_uSectionsPerRow = uWidth / sizMax.cx ;
- // This may be slightly larger than the maximum bitmap width if there was room
- // to spare.
- m_sizSection.cx = uWidth / m_uSectionsPerRow ;
- sizFont = GetFontSize();
- m_sizSection.cy = sizMax.cy + (TPW_NUM_LINESTEXT * sizFont.cy);
- } } UINT CTitledPicsWnd::GetSize(void) { return m_aryDwBitmapTypes.GetSize(); } CSize CTitledPicsWnd::GetBitmapSize(CBitmap *pBmp) {
- BITMAP sBmp ;
- CSize sizBmp(0, 0);
- if (pBmp != NULL)
- {
- pBmp->GetObject(sizeof(BITMAP), &sBmp);
- sizBmp.cx = sBmp.bmWidth ;
- sizBmp.cy = sBmp.bmHeight ;
- }
- return sizBmp ; } void CTitledPicsWnd::SetSpacing(UINT uSpacing) { m_uSpacing = uSpacing ; } CSize CTitledPicsWnd::GetFontSize(void) { CClientDC *pDC ;
- CFont *pFntOld ;
- CSize sizFont(0, 0);
- if (m_hWnd != NULL)
- {
- pDC = new CClientDC(this);
- pFntOld = (CFont *)pDC->SelectObject(&m_fntSmall);
- sizFont = pDC->GetTextExtent("X", 1);
- pDC->SelectObject(pFntOld);
- delete pDC ;
- }
- return sizFont ; } void CTitledPicsWnd::SetNewPageSize(void) { CSize sizPage, sizBigStep, sizLittleStep ;
- UINT uRows, uMax ;
- sizLittleStep = m_sizSection ;
- sizBigStep = m_sizSection ;
- uMax = GetSize();
- uRows = uMax / m_uSectionsPerRow ;
- if (m_uSectionsPerRow * uRows < uMax)
- uRows++ ;
- sizPage.cy = uRows * m_sizSection.cy ;
- sizPage.cx = m_uSectionsPerRow * m_sizSection.cx ;
- SetScrollSizes(MM_TEXT, sizPage, sizBigStep, sizLittleStep); } void CTitledPicsWnd::CalculateFit(void) {
- CRect rctClient ;
- if (m_hWnd != NULL && GetSize() > 0)
- {
- GetClientRect(&rctClient);
- m_uSectionsPerRow = rctClient.Width() / m_sizSection.cx ;
- } }
- BOOL CTitledPicsWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- BOOL bCreatedOK ;
- m_fntSmall.CreateFont(10, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
- ANSI_CHARSET, OUT_DEFAULT_PRECIS,
- CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
- VARIABLE_PITCH|FF_SWISS, NULL);
- bCreatedOK = CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
- if (bCreatedOK)
- OnInitialUpdate();
- return bCreatedOK ;
- }
- void CTitledPicsWnd::OnDestroy()
- {
- CScrollView::OnDestroy();
- m_fntSmall.DeleteObject();
- }
- void CTitledPicsWnd::StretchBitmaps(BOOL bStretch) { m_bStretchBitmaps = bStretch ; }
- void CTitledPicsWnd::OnLButtonDown(UINT nFlags, CPoint point)
- {
- BOOL bChanged = FALSE ;
- CPoint ptTopLeft ;
- CRect rctFocus ;
- int x, y, iIndex ;
- ptTopLeft = GetScrollPosition();
- point += ptTopLeft ;
- x = point.x / m_sizSection.cx ;
- y = point.y / m_sizSection.cy ;
- iIndex = (y * m_uSectionsPerRow) + x ;
- if (x < (int)m_uSectionsPerRow && iIndex < (int)GetSize() )
- {
- // Remove any existing focus.
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem, FALSE);
- if (m_iSelectedItem != iIndex)
- bChanged = TRUE ;
- m_iSelectedItem = iIndex ;
- DrawFocusOnItem(m_iSelectedItem);
- // Notify parent of the events.
- NotifyParent(TPWN_CLICK);
- if (bChanged)
- NotifyParent(TPWN_SELCHANGE);
- }
- CScrollView::OnLButtonDown(nFlags, point);
- SetFocus();
- }
- void CTitledPicsWnd::DrawFocusOnItem(UINT uItem, BOOL bDraw) {
- CClientDC dc(this);
- CPen penBkgnd, *pPenOld ;
- CRect rctFocus ;
- int x, y ;
- CScrollView::OnPrepareDC(&dc);
- if ( (int)uItem != TPW_NONE)
- {
- x = uItem % m_uSectionsPerRow ;
- y = uItem / m_uSectionsPerRow ;
- rctFocus.SetRect(x * m_sizSection.cx, y * m_sizSection.cy,
- (x + 1) * m_sizSection.cx, (y + 1) * m_sizSection.cy);
- if (bDraw)
- dc.DrawFocusRect(&rctFocus);
- else
- {
- penBkgnd.CreatePen(PS_SOLID, 1, m_dwWndBkgndColour);
- pPenOld = (CPen *)dc.SelectObject(&penBkgnd);
- dc.SelectStockObject(NULL_BRUSH);
- dc.Rectangle(rctFocus);
- dc.SelectObject(pPenOld);
- penBkgnd.DeleteObject();
- }
- }
- }
- void CTitledPicsWnd::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
- {
- // Prevent OnDraw() from interfering with the focus rectangle.
- m_bFocusIsDone = TRUE ;
- // Remove any existing focus.
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem, FALSE);
- CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem);
- // OnDraw() can revert to normal.
- m_bFocusIsDone = FALSE ;
- }
- int CTitledPicsWnd::GetSel(void) { return m_iSelectedItem ; } void CTitledPicsWnd::SetSel(int iIndex) {
- BOOL bChanged = FALSE ;
- if (iIndex == TPW_NONE || (iIndex >= 0 && iIndex < (int)GetSize() ) )
- {
- // Remove any existing focus.
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem, FALSE);
- if (m_iSelectedItem != iIndex)
- bChanged = TRUE ;
- m_iSelectedItem = iIndex ;
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem);
- // Notify parent of the event.
- if (bChanged)
- NotifyParent(TPWN_SELCHANGE);
- }
- }
- void CTitledPicsWnd::OnLButtonDblClk(UINT nFlags, CPoint point)
- {
- BOOL bChanged = FALSE ;
- CPoint ptTopLeft ;
- CRect rctFocus ;
- int x, y, iIndex ;
- ptTopLeft = GetScrollPosition();
- point += ptTopLeft ;
- x = point.x / m_sizSection.cx ;
- y = point.y / m_sizSection.cy ;
- iIndex = (y * m_uSectionsPerRow) + x ;
- if (x < (int)m_uSectionsPerRow && iIndex < (int)GetSize() )
- {
- // Remove any existing focus.
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem, FALSE);
- if (m_iSelectedItem != iIndex)
- bChanged = TRUE ;
- m_iSelectedItem = iIndex ;
- DrawFocusOnItem(m_iSelectedItem);
- // Notify parent of the events.
- NotifyParent(TPWN_DBLCLK);
- if (bChanged)
- NotifyParent(TPWN_SELCHANGE);
- }
- CScrollView::OnLButtonDblClk(nFlags, point);
- SetFocus();
- }
- void CTitledPicsWnd::NotifyParent(UINT uNotifyCode) { CWnd *pWndParent ;
- UINT uID ;
- pWndParent = GetParent();
- if (pWndParent != NULL)
- {
- uID = GetDlgCtrlID();
- pWndParent->PostMessage(WM_COMMAND,
- (WPARAM)MAKEWPARAM(uID, uNotifyCode),
- (LPARAM)m_hWnd);
- }
- }
- void CTitledPicsWnd::OnRButtonDown(UINT nFlags, CPoint point)
- {
- BOOL bChanged = FALSE ;
- CPoint ptTopLeft ;
- CRect rctFocus ;
- int x, y, iIndex ;
- ptTopLeft = GetScrollPosition();
- point += ptTopLeft ;
- x = point.x / m_sizSection.cx ;
- y = point.y / m_sizSection.cy ;
- iIndex = (y * m_uSectionsPerRow) + x ;
- if (x < (int)m_uSectionsPerRow && iIndex < (int)GetSize() )
- {
- // Remove any existing focus.
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem, FALSE);
- if (m_iSelectedItem != iIndex)
- bChanged = TRUE ;
- m_iSelectedItem = iIndex ;
- DrawFocusOnItem(m_iSelectedItem);
- // Notify parent of the events.
- NotifyParent(TPWN_RCLICK);
- if (bChanged)
- NotifyParent(TPWN_SELCHANGE);
- }
- CScrollView::OnRButtonDown(nFlags, point);
- SetFocus();
- }
- CString CTitledPicsWnd::GetSelectedTitle(void) { CString strTitle("");
- if (m_iSelectedItem != TPW_NONE)
- strTitle = m_aryStrTitles.GetAt(m_iSelectedItem);
- return strTitle ; } CBitmap * CTitledPicsWnd::GetSelectedBitmapPointer(void) { CBitmap *pBmp = NULL ;
- if (m_iSelectedItem != TPW_NONE)
- {
- if (m_aryDwBitmapTypes.GetAt(m_iSelectedItem) == TPW_BITMAPTYPE_OBJECT)
- pBmp = (CBitmap *)m_aryDwBitmaps.GetAt(m_iSelectedItem);
- }
- return pBmp ;
- } UINT CTitledPicsWnd::GetSelectedBitmapID(void) { UINT uBmpID = 0 ;
- if (m_iSelectedItem != TPW_NONE)
- {
- if (m_aryDwBitmapTypes.GetAt(m_iSelectedItem) == TPW_BITMAPTYPE_RESOURCE)
- uBmpID = (UINT)m_aryDwBitmaps.GetAt(m_iSelectedItem);
- }
- return uBmpID ;
- } UINT CTitledPicsWnd::GetSelectedBitmapType(void) { UINT uBmpType = 0 ;
- if (m_iSelectedItem != TPW_NONE)
- uBmpType = (UINT)m_aryDwBitmapTypes.GetAt(m_iSelectedItem);
- return uBmpType ; }
- void CTitledPicsWnd::OnSize(UINT nType, int cx, int cy)
- {
- CScrollView::OnSize(nType, cx, cy);
- // Since CalculateBestFit() changes the actual dimensions to real values, if
- // the user originally specified best fit we must recalculate the section
- // size - it will not happen automatically.
- if (m_bWantsBestFit)
- CalculateBestFit();
- }
- void CTitledPicsWnd::OnKillFocus(CWnd* pNewWnd)
- {
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem, FALSE);
- CScrollView::OnKillFocus(pNewWnd);
- }
- void CTitledPicsWnd::OnSetFocus(CWnd* pOldWnd)
- {
- CScrollView::OnSetFocus(pOldWnd);
- // Prevent OnDraw() from interfering with the focus rectangle.
- m_bFocusIsDone = TRUE ;
- // Make absolutely certain the window has been fully painted before we do this.
- UpdateWindow();
- if (m_iSelectedItem != TPW_NONE)
- DrawFocusOnItem(m_iSelectedItem);
- }