PictsViewer.cpp
上传用户:maryhy001
上传日期:2007-05-02
资源大小:2317k
文件大小:15k
- // PictsViewer.cpp : implementation file
- #include "stdafx.h"
- #include "PictsViewer.h"
- #define MARGIN_LEFT 5
- #define MARGIN_TOP 5
- #define CX_EXPANDBTN 8
- #define CY_EXPANDBTN 4
- #define TIMER_ID_TRACKER 500
- //CPictsGroup class
- CPictsGroup::CPictsGroup()
- {
- this->m_pOwnerWnd = NULL;
- this->m_bOnlyShowCaption = FALSE;
- this->m_strCaption = "Picture Group";
- }
- CPictsGroup::~CPictsGroup()
- {
- this->m_imglstPicts.Detach();
- }
- void CPictsGroup::operator=(CPictsGroup &pgRight)
- {
- this->m_imglstPicts.Create(&pgRight.m_imglstPicts);
- this->m_strCaption = pgRight.m_strCaption;
- this->m_pOwnerWnd = pgRight.m_pOwnerWnd;
- this->m_bOnlyShowCaption = pgRight.m_bOnlyShowCaption;
- }
- void CPictsGroup::SetOwnerWnd(CWnd *pOwnerWnd)
- {
- ASSERT(pOwnerWnd && ::IsWindow(pOwnerWnd->GetSafeHwnd()));
-
- this->m_pOwnerWnd = pOwnerWnd;
- }
- void CPictsGroup::SetCaption(LPCSTR strCaption)
- {
- this->m_strCaption = strCaption;
- }
- int CPictsGroup::SetImageList(CImageList &ilImages)
- {
- this->m_imglstPicts.Detach();
- this->m_imglstPicts.Create(&ilImages);
- return this->m_imglstPicts.GetImageCount();
- }
- void CPictsGroup::SetOnlyShowCaption(BOOL bEnable)
- {
- this->m_bOnlyShowCaption = bEnable;
- if(this->m_pOwnerWnd)
- {
- this->m_pOwnerWnd->SendMessage(UWM_PICTSVIEWERREFRESH, 0, 0);
- }
- }
- int CPictsGroup::InsertImage(CBitmap &bmp, COLORREF crMask)
- {
- this->m_imglstPicts.Add(&bmp, crMask);
- return this->m_imglstPicts.GetImageCount();
- }
- void CPictsGroup::DeleteImage(const int index)
- {
- int count = this->m_imglstPicts.GetImageCount();
- if(index > -1 && index < count)
- {
- this->m_imglstPicts.Remove(index);
- }
- }
- // CPictsViewer dialog
- CPictsViewer::CPictsViewer(CWnd* pParent /*=NULL*/)
- : CDialog(CPictsViewer::IDD, pParent)
- {
- this->m_wndParent = NULL;
- this->m_hcHand = NULL;
-
- this->m_uColsPreRow = 8;
- this->m_hOrgiWnd = NULL;
- this->m_bHoverControl = FALSE;
- this->m_bHoverGroupCaption = FALSE;
- this->m_nPictsGroupIndex = -1;
- this->m_nImageListIndex = -1;
- this->m_nHoverGroupCaptionIndex = -1;
- this->m_crBrdColor = RGB(0, 0, 160);
- this->m_crBkColor = RGB(255, 255, 255);
- //{{AFX_DATA_INIT(CPictsViewer)
- //}}AFX_DATA_INIT
- }
- void CPictsViewer::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPictsViewer)
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CPictsViewer, CDialog)
- //{{AFX_MSG_MAP(CPictsViewer)
- ON_WM_PAINT()
- ON_WM_DESTROY()
- ON_WM_TIMER()
- ON_WM_ERASEBKGND()
- ON_WM_KILLFOCUS()
- ON_WM_MOUSEMOVE()
- ON_WM_SETCURSOR()
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- // CPictsViewer message handlers
- BOOL CPictsViewer::Create(CWnd *pParent)
- {
- //if you want to create modeless window,
- //you must invoke CDialog::Create function.
- if(!CDialog::Create(CPictsViewer::IDD, pParent))
- {
- return FALSE;
- }
- this->m_wndParent = pParent;
- this->ModifyStyle(WS_CAPTION, WS_MINIMIZEBOX, SWP_DRAWFRAME);
- this->m_pnPen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
- this->m_pnPen.GetLogPen(&m_logpen);
- this->m_ftFont.CreateFont(
- 16, // nHeight
- 0, // nWidth
- 0, // nEscapement
- 0, // nOrientation
- FW_BOLD, // nWeight
- FALSE, // bItalic
- FALSE, // bUnderline
- 0, // cStrikeOut
- ANSI_CHARSET, // nCharSet
- OUT_DEFAULT_PRECIS, // nOutPrecision
- CLIP_DEFAULT_PRECIS, // nClipPrecision
- DEFAULT_QUALITY, // nQuality
- DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
- "Arial");
-
- return TRUE;
- }
- void CPictsViewer::OnKillFocus(CWnd* pNewWnd)
- {
- CDialog::OnKillFocus(pNewWnd);
- //close self windows.
- this->DestroyWindow();
- }
- void CPictsViewer::OnDestroy()
- {
- CDialog::OnDestroy();
-
- if(NULL != m_hcHand)
- {
- ::DestroyCursor(this->m_hcHand);
- this->m_hcHand = NULL;
- }
- //delete GDI object.
- this->m_pnPen.DeleteObject();
- this->m_ftFont.DeleteObject();
-
- //free the memory.
- for(CPictsGroup *p = NULL; this->m_lstPictsGroups.GetCount() > 0; )
- {
- p = this->m_lstPictsGroups.GetHead();
- if(p) delete p;
- this->m_lstPictsGroups.RemoveHead();
- }
- this->FreeRcGralnkList();
- }
- void CPictsViewer::FreeRcGralnkList(void)
- {
- //1
- for(PRCLINKIMG prc = NULL; this->m_lstTxtRects.GetCount() > 0; )
- {
- prc = this->m_lstTxtRects.GetHead();
- if(prc) delete prc;
- this->m_lstTxtRects.RemoveHead();
- }
- this->m_lstTxtRects.RemoveAll();
- //2
- for(prc = NULL; this->m_lstGraphicRects.GetCount() > 0; )
- {
- prc = this->m_lstGraphicRects.GetHead();
- if(prc) delete prc;
- this->m_lstGraphicRects.RemoveHead();
- }
- this->m_lstGraphicRects.RemoveAll();
- //3
- for(prc = NULL; this->m_lstExpandBtnRects.GetCount() > 0; )
- {
- prc = this->m_lstExpandBtnRects.GetHead();
- if(prc) delete prc;
- this->m_lstExpandBtnRects.RemoveHead();
- }
- this->m_lstExpandBtnRects.RemoveAll();
- }
- int CPictsViewer::InsertPictsGroup(CPictsGroup &pgPictsGroup)
- {
- CPictsGroup *p = new CPictsGroup;
- *p = pgPictsGroup;
- this->m_lstPictsGroups.AddTail(p);
- this->AdjustWindowRect();
- return this->m_lstPictsGroups.GetCount();
- }
- void CPictsViewer::DeletePictsGroup(const int index)
- {
- POSITION pos = this->m_lstPictsGroups.FindIndex(index);
- if(NULL != pos)
- {
- CPictsGroup *p = this->m_lstPictsGroups.GetAt(pos);
- if(p) delete p;
- this->m_lstPictsGroups.RemoveAt(pos);
- this->AdjustWindowRect();
- }
- }
- BOOL CPictsViewer::PreCreateWindow(CREATESTRUCT& cs)
- {
- return CDialog::PreCreateWindow(cs);
- }
- void CPictsViewer::SetOrgWindow(HWND hwnd)
- {
- this->m_hOrgiWnd = hwnd;
- this->AdjustWindowRect();
- }
- void CPictsViewer::SetColsPreRow(const int nColsPreRow)
- {
- this->m_uColsPreRow = nColsPreRow;
- this->Invalidate();
- }
- void CPictsViewer::AdjustWindowRect(void)
- {
- CRect rcorgi;
- CDC *pDC = this->GetDC();
- int width = 0, height = 0;
-
- this->GetWindowRect(&rcorgi);
- width = rcorgi.Width(); height = rcorgi.Height();
- rcorgi.EqualRect(CRect(0, 0, 0, 0));
- if(NULL != this->m_hOrgiWnd)
- {
- ::GetWindowRect(this->m_hOrgiWnd, &rcorgi);
- }
- if(m_lstPictsGroups.GetCount() > 0)
- {
- CSize sizetxt;
- int cx = 0,
- cy = 0,
- count = 0,
- txtheight = 0,
- lineheight = 0;
- int nVisibleGroup = 0;
-
- width = height = 0;
- CPictsGroup *p = m_lstPictsGroups.GetHead();
- ImageList_GetIconSize(p->m_imglstPicts.GetSafeHandle(), &cx, &cy);
- for(POSITION pos = m_lstPictsGroups.GetHeadPosition(); pos != NULL; )
- {
- p = m_lstPictsGroups.GetNext(pos);
- if(!p->m_bOnlyShowCaption)
- {
- count += p->m_imglstPicts.GetImageCount();
- lineheight+= this->m_logpen.lopnWidth.x;
- nVisibleGroup++;
- }
- sizetxt = pDC->GetTextExtent(p->m_strCaption);
- txtheight += MARGIN_TOP + sizetxt.cy;
- }
- int rows = count / (this->m_uColsPreRow ? this->m_uColsPreRow : 1) + nVisibleGroup;
- width = this->m_uColsPreRow * (MARGIN_LEFT + cx) + MARGIN_LEFT;
- height = rows * (cy + MARGIN_TOP) + MARGIN_TOP + (txtheight + lineheight + MARGIN_TOP);
- }
- if(width > 0 && height > 0)
- {
- this->SetWindowPos(NULL, rcorgi.left, rcorgi.bottom + 1,
- width, height, SWP_SHOWWINDOW);
- this->Invalidate();
- }
- }
- BOOL CPictsViewer::OnEraseBkgnd(CDC* pDC)
- {
- return TRUE;
- }
- void CPictsViewer::OnPaint()
- {
- //device context for painting.
- CPaintDC dc(this);
-
- CRect rcclt,
- rctxt,
- rcexpbtn;
- COLORREF crfntcolor;
-
- HPEN hpen = (HPEN)dc.SelectObject(m_pnPen.GetSafeHandle());
- HFONT hfont = (HFONT)dc.SelectObject(m_ftFont.GetSafeHandle());
-
- int nbkmode = dc.SetBkMode(TRANSPARENT);
-
- this->GetClientRect(&rcclt);
-
- //RGB(213, 227, 255)
- CBrush brush;
-
- brush.CreateSolidBrush(m_crBrdColor);
- dc.FillSolidRect(&rcclt, m_crBkColor);
- dc.FrameRect(&rcclt, &brush);
-
- if(m_lstPictsGroups.GetCount() > 0)
- {
- int cx = 0, cy = 0;
- int left = MARGIN_LEFT, top = MARGIN_TOP;
- //delete prior rect-graphices infos.
- this->FreeRcGralnkList();
- this->m_nPictsGroupIndex = m_nImageListIndex = -1;
- CPictsGroup *p = m_lstPictsGroups.GetHead();
- ImageList_GetIconSize(p->m_imglstPicts.GetSafeHandle(), &cx, &cy);
- POSITION pos = m_lstPictsGroups.GetHeadPosition();
- for(int j = 0; pos != NULL; ++j)
- {
- p = this->m_lstPictsGroups.GetAt(pos);
-
- //get the caption text drawing rect.
- CSize sizetxt = dc.GetTextExtent(p->m_strCaption);
- rctxt.left = CX_EXPANDBTN + 2*MARGIN_LEFT;
- rctxt.top = top + MARGIN_TOP;
- rctxt.right = rctxt.left + this->m_uColsPreRow * cx;
- rctxt.bottom = rctxt.top + sizetxt.cy;
- //get the expanding button rect.
- rcexpbtn.left = MARGIN_LEFT;
- rcexpbtn.top = top + MARGIN_TOP;
- rcexpbtn.right = rcexpbtn.left + CX_EXPANDBTN;
- rcexpbtn.bottom = max(rctxt.bottom, rcexpbtn.top + CY_EXPANDBTN);
- rctxt.bottom = rcexpbtn.bottom;
- //make the caption rect info and insert it into memory list.
- PRCLINKIMG prclnk = new RCLINKIMG;
- prclnk->rc = (RECT)rctxt;
- prclnk->lparam = j;
-
- this->m_lstTxtRects.AddTail(prclnk);
-
- //make expanding button rect.
- prclnk = new RCLINKIMG;
- prclnk->rc = (RECT)rcexpbtn;
- prclnk->lparam = j;
-
- this->m_lstExpandBtnRects.AddTail(prclnk);
- //draw expanding button.
- if(p->m_bOnlyShowCaption)
- {
- crfntcolor = dc.SetTextColor(RGB(155, 50, 0));
- dc.DrawText("+", &rcexpbtn, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- dc.SetTextColor(crfntcolor);
- }
- else
- {
- crfntcolor = dc.SetTextColor(RGB(0, 0, 0));
- dc.DrawText("-", &rcexpbtn, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
- dc.SetTextColor(crfntcolor);
- }
- rcexpbtn.InflateRect(0, 1);
- dc.FrameRect(&rcexpbtn, &brush);
- //if current mouse is hovering on caption rect the change the caption text color.
- if(this->m_bHoverGroupCaption && j == this->m_nHoverGroupCaptionIndex)
- crfntcolor = dc.SetTextColor(RGB(0, 0, 255));
- else
- crfntcolor = dc.SetTextColor(RGB(0, 0, 0));
-
- dc.DrawText(p->m_strCaption, &rctxt, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
- dc.SetTextColor(crfntcolor);
-
- top = rctxt.bottom + MARGIN_TOP;
- //if user want to hide or show some pictures group then ...
- if(!p->m_bOnlyShowCaption)
- {
- //draw the line.
- dc.MoveTo(rctxt.left, rctxt.bottom + m_logpen.lopnWidth.x);
- dc.LineTo(rcclt.right - MARGIN_LEFT, rctxt.bottom + m_logpen.lopnWidth.x);
-
- //draw the imagelist elements and get every image's rect info.
- for(int i = 0; p && i < p->m_imglstPicts.GetImageCount(); ++i)
- {
- PRCLINKIMG prclnk = new RCLINKIMG;
-
- prclnk->rc = (RECT)CRect(left, top, left+cx, top+cy);
- prclnk->lparam = MAKELONG(i, j);
-
- this->m_lstGraphicRects.AddTail(prclnk);
-
- p->m_imglstPicts.DrawIndirect(&dc, i, CPoint(left, top),
- CSize(cx, cy), CPoint(0, 0), ILD_NORMAL, SRCCOPY);
-
- left += (cx + MARGIN_LEFT);
- if((i + 1) % (this->m_uColsPreRow ? this->m_uColsPreRow : 1) == 0)
- {
- left = MARGIN_LEFT;
- top += (cy + MARGIN_TOP);
- }
- }
- top = top + (cy + MARGIN_TOP);
- }
- p = m_lstPictsGroups.GetNext(pos);
-
- left = MARGIN_LEFT;
- }
- }
- brush.DeleteObject();
- dc.SelectObject(hfont);
- dc.SelectObject(hpen);
- dc.SetBkMode(nbkmode);
- }
- BOOL CPictsViewer::PtInPictsExpandButtonRect(CPoint point, int &nGroupIndex, CRect &rcExpandButton)
- {
- if(!this->m_bHoverControl || !this->m_lstExpandBtnRects.GetCount()) return FALSE;
- POSITION pos = this->m_lstExpandBtnRects.GetHeadPosition();
- for(PRCLINKIMG prclnk = NULL; pos != NULL; )
- {
- prclnk = this->m_lstExpandBtnRects.GetNext(pos);
- if(prclnk && ::PtInRect(&prclnk->rc, (POINT)point))
- {
- nGroupIndex = prclnk->lparam;
- rcExpandButton = prclnk->rc;
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CPictsViewer::PtInPictsGroupCaptionRect(CPoint point, int &nPictsGroupIndex, CRect &rcCaption)
- {
- if(!this->m_bHoverControl || !this->m_lstPictsGroups.GetCount()) return FALSE;
- nPictsGroupIndex = -1;
- POSITION pos = this->m_lstTxtRects.GetHeadPosition();
- for(PRCLINKIMG prclnk = NULL; pos != NULL; )
- {
- prclnk = this->m_lstTxtRects.GetNext(pos);
- if(prclnk && ::PtInRect(&prclnk->rc, (POINT)point))
- {
- nPictsGroupIndex = prclnk->lparam;
- rcCaption = prclnk->rc;
- return TRUE;
- }
- }
- return FALSE;
- }
- BOOL CPictsViewer::GetCellFromPoint(CPoint point, int &nGroupIndex, int &nImageIndex, CRect &rcGraphic)
- {
- if(!this->m_bHoverControl || !this->m_lstPictsGroups.GetCount()) return FALSE;
-
- nGroupIndex = nImageIndex = -1;
-
- POSITION pos = this->m_lstGraphicRects.GetHeadPosition();
- for(PRCLINKIMG prclnk = NULL; pos != NULL; )
- {
- prclnk = this->m_lstGraphicRects.GetNext(pos);
- if(prclnk && ::PtInRect(&prclnk->rc, (POINT)point))
- {
- rcGraphic = prclnk->rc;
- nGroupIndex = HIWORD(prclnk->lparam);
- nImageIndex = LOWORD(prclnk->lparam);
- return TRUE;
- }
- }
- return FALSE;
- }
- void CPictsViewer::OnMouseMove(UINT nFlags, CPoint point)
- {
- if(!this->m_bHoverControl)
- {
- this->m_bHoverControl = TRUE;
- this->Invalidate();
- this->SetTimer(TIMER_ID_TRACKER, 100, NULL);
- }
- CRect rcCaption, rcGraphic;
- BOOL bInCaptionRect = PtInPictsGroupCaptionRect(point, m_nHoverGroupCaptionIndex, rcCaption);
-
- this->m_bHoverGroupCaption = bInCaptionRect;
- this->InvalidateRect(&rcCaption);
- this->GetCellFromPoint(point, m_nPictsGroupIndex,
- m_nImageListIndex, rcGraphic);
-
- CDialog::OnMouseMove(nFlags, point);
- }
- void CPictsViewer::OnTimer(UINT nIDEvent)
- {
- CRect rcclt;
- CPoint ptcur;
-
- ::GetCursorPos(&ptcur);
- this->GetClientRect(&rcclt);
- this->ScreenToClient(&ptcur);
- if(!rcclt.PtInRect(ptcur))
- {
- this->m_bHoverControl = FALSE;
- this->KillTimer(TIMER_ID_TRACKER);
- this->Invalidate();
- }
- CDialog::OnTimer(nIDEvent);
- }
- BOOL CPictsViewer::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- if(NULL == this->m_hcHand)
- {
- this->m_hcHand = GetSysHandCursor();
- if(NULL == this->m_hcHand) return FALSE;
- }
- ::SetCursor(this->m_hcHand);
- return TRUE;
- }
- void CPictsViewer::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CRect rcExpanBtn,
- rcGraphic;
- POSITION pos;
-
- if(this->m_bHoverControl)
- {
- if(!this->m_bHoverGroupCaption)
- {
- if(this->GetCellFromPoint(point, m_nPictsGroupIndex, m_nImageListIndex, rcGraphic))
- {
- if(m_nPictsGroupIndex != -1 && m_nImageListIndex != -1)
- {
- if(this->m_wndParent) m_wndParent->SendMessage(UWM_PICTSVIEWERCLICKED, MAKEWORD(m_nImageListIndex, m_nPictsGroupIndex), NULL);
- }
- }
- else if(this->PtInPictsExpandButtonRect(point, m_nPictsGroupIndex, rcExpanBtn))
- {
- if(m_nPictsGroupIndex != -1)
- {
- m_nImageListIndex = -1;
- pos = this->m_lstPictsGroups.FindIndex(m_nPictsGroupIndex);
- if(pos != NULL)
- {
- CPictsGroup *p = this->m_lstPictsGroups.GetAt(pos);
- p->m_bOnlyShowCaption = !p->m_bOnlyShowCaption;
- this->AdjustWindowRect();
- this->Invalidate();
- }
- }
- }
- }
- else
- {
- //////////////////////////////////////////////////////////////////////////
- }
- }
- CDialog::OnLButtonDown(nFlags, point);
- }
- void CPictsViewer::OnRefreshMsg(WPARAM wparam, LPARAM lparam)
- {
- this->AdjustWindowRect();
- this->Invalidate();
- }