WEB开发网
开发学院软件开发VC 一个简单的滑动控件CSlideTab类 阅读

一个简单的滑动控件CSlideTab类

 2008-11-13 19:30:08 来源:WEB开发网   
核心提示:看到论坛上有人提出,需要一个通用的滑动控件,一个简单的滑动控件CSlideTab类,考虑了一下,从CWnd继承了一个CSlideTab类,实现需要的界面效果,已经在内部实现双缓冲,实现一些演示功能, 使用方法:1. #include "slidetab.h" //在需要使用的窗口类的头文件中包含2.

看到论坛上有人提出,需要一个通用的滑动控件,考虑了一下,从CWnd继承了一个CSlideTab类,实现一些演示功能。

使用方法:

1. #include "slidetab.h"  //在需要使用的窗口类的头文件中包含

2. CSlideTab m_SlideTab;  //声明一个CSlideTab的成员变量

3. m_SlideTab.CreateTab(CRect(-1, -1, 50, 50), ID_SLIDETAB, this);  //在窗口初始化过程中调用CSlideTab::CreateTab(CRect& rcTab, UINT nID, CWnd *pWndParent)方法,其中ID_SLIDETAB为控件ID,需要在资源文件中定义。

4. m_SlideTab.SetHeight(400);  //设置为向下展开,展开高度为400

或者

m_SlideTab.SetWidth(400);  //设置为向右展开,展开宽度为400

以上两个只有最后调用的有效。

5. 可以重载这个类的DrawTab(CDC *pDC)虚拟成员函数,实现需要的界面效果。已经在内部实现双缓冲,直接在pDC上绘制即可。

  slidetab.h
  slidetab.h
  #pragma once
  #include "atltypes.h"
  // CSlideTab
  typedef enum _SLIDE_TAB_ACTION
  {
  SLIDE_TAB_UP = -1, SLIDE_TAB_STOP, SLIDE_TAB_DOWN
  }SLIDE_TAB_ACTION;
  class CSlideTab : public CWnd
  {
  DECLARE_DYNAMIC(CSlideTab)
  public:
  CSlideTab();
  virtual ~CSlideTab();
  protected:
  DECLARE_MESSAGE_MAP()
  public:
  void CreateTab(CRect& rcTab, UINT nID, CWnd *pWndParent);
  afx_msg void OnPaint();
  afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  virtual BOOL PreTranslateMessage(MSG* pMsg);
  public:
  static void CALLBACK OpenTab(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2);
  int SetHeight(int nHeight);
  int SetWidth(int nWidth);
  afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  virtual void DrawTab(CDC* pDC);
  afx_msg void OnDestroy();
  afx_msg void OnMove(int x, int y);
  private:
  UINT m_nTimerID;
  int m_nHeight;
  SLIDE_TAB_ACTION m_nAction;
  CRect m_rcWnd;
  BOOL m_fVert;
  };
  slidetab.cpp
  // SlideTab.cpp : 实现文件
  //
  #include "stdafx.h"
  #include "SlideTab.h"
  #include <Mmsystem.h>
  #include ".slidetab.h"
  #pragma comment(lib,"winmm.lib")
  // CSlideTab
  IMPLEMENT_DYNAMIC(CSlideTab, CWnd)
  CSlideTab::CSlideTab()
  : m_nTimerID(0)
  , m_nHeight(100)
  , m_nAction(SLIDE_TAB_STOP)
  , m_fVert(FALSE)
  {
  }
  CSlideTab::~CSlideTab()
  {
  }
  BEGIN_MESSAGE_MAP(CSlideTab, CWnd)
  ON_WM_PAINT()
  ON_WM_MOUSEMOVE()
  ON_WM_ERASEBKGND()
  ON_WM_DESTROY()
  ON_WM_MOVE()
  END_MESSAGE_MAP()
  // CSlideTab 消息处理程序
  void CSlideTab::CreateTab(CRect& rcTab, UINT nID, CWnd *pWndParent)
  {
  m_rcWnd.CopyRect(rcTab);
  Create(AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW, NULL, NULL, NULL), NULL, WS_VISIBLE | WS_CHILD, rcTab, pWndParent, nID);
  }
  void CSlideTab::OnPaint()
  {
  CPaintDC dc(this); // device context for painting
  // TODO: 在此处添加消息处理程序代码
  // 不为绘图消息调用 CWnd::OnPaint()
  CRect ClientRect;
  GetClientRect(ClientRect);
  CBitmap MemBmp;
  MemBmp.CreateCompatibleBitmap(&dc, ClientRect.Width(), ClientRect.Height());
  CDC MemDC;
  MemDC.CreateCompatibleDC(&dc);
  CBitmap *pBmpOld = MemDC.SelectObject(&MemBmp);
DrawTab(&MemDC);
  dc.BitBlt(ClientRect.left, ClientRect.top, ClientRect.Width(), ClientRect.Height(), &MemDC, 0, 0, SRCCOPY);
  MemDC.SelectObject(pBmpOld);
  MemDC.DeleteDC();
  MemBmp.DeleteObject();
  }
  void CSlideTab::OnMouseMove(UINT nFlags, CPoint point)
  {
  // TODO: 在此添加消息处理程序代码和/或调用默认值
  CRect rcTab;
  GetWindowRect(rcTab);
  if (m_nAction == SLIDE_TAB_UP)
  {
  m_nAction = SLIDE_TAB_DOWN;
  }
  else if (m_nAction == SLIDE_TAB_DOWN)
  {
  TRACKMOUSEEVENT mEvent;
  mEvent.cbSize = sizeof(TRACKMOUSEEVENT);
  mEvent.hwndTrack = this->GetSafeHwnd();
  mEvent.dwHoverTime = HOVER_DEFAULT;
  mEvent.dwFlags = TME_LEAVE;
  _TrackMouseEvent(&mEvent);
  }
  else if ((m_fVert && rcTab.Height() <= m_rcWnd.Height())
  || (!m_fVert && rcTab.Width() <= m_rcWnd.Width()))
  {
  TRACKMOUSEEVENT mEvent;
  mEvent.cbSize = sizeof(TRACKMOUSEEVENT);
  mEvent.hwndTrack = this->GetSafeHwnd();
  mEvent.dwHoverTime = HOVER_DEFAULT;
  mEvent.dwFlags = TME_HOVER;
  _TrackMouseEvent(&mEvent);
  }
  CWnd::OnMouseMove(nFlags, point);
  }
  BOOL CSlideTab::PreTranslateMessage(MSG* pMsg)
  {
  // TODO: 在此添加专用代码和/或调用基类
  if (pMsg->message == WM_MOUSEHOVER)
  {
  m_nAction = SLIDE_TAB_DOWN;
  if (!m_nTimerID)
  {
  m_nTimerID = timeSetEvent(5, 1, OpenTab, (DWORD_PTR)this, TIME_PERIODIC);
  }
  }
  else if (pMsg->message == WM_MOUSELEAVE)
  {
  m_nAction = SLIDE_TAB_UP;
  if (!m_nTimerID)
  {
  m_nTimerID = timeSetEvent(5, 1, OpenTab, (DWORD_PTR)this, TIME_PERIODIC);
  }
  }
  return CWnd::PreTranslateMessage(pMsg);
  }
  void CALLBACK CSlideTab::OpenTab(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
  {
  CSlideTab *pTab = (CSlideTab*)dwUser;
  CRect rcTab;
  pTab->GetWindowRect(rcTab);
  int nTabHeight, nWndHeight;
  LONG *pBottom, *pTop;
  if (pTab->m_fVert)
  {
  nTabHeight = rcTab.Height();
  nWndHeight = pTab->m_rcWnd.Height();
  pTop = &rcTab.top;
  pBottom = &rcTab.bottom;
  }
  else
  {
  nTabHeight = rcTab.Width();
  nWndHeight = pTab->m_rcWnd.Width();
  pTop = &rcTab.left;
  pBottom = &rcTab.right;
  }
  if (((nTabHeight > nWndHeight) && pTab->m_nAction == SLIDE_TAB_UP)
  || (pTab->m_nAction == SLIDE_TAB_DOWN) &&(nTabHeight < pTab->m_nHeight))
  {
  pTab->GetParent()->ScreenToClient(rcTab);
  *pBottom += 5 * pTab->m_nAction;
  pTab->MoveWindow(rcTab, FALSE);
  if (pTab->m_nAction == SLIDE_TAB_UP)
  {
  *pTop = *pBottom;
  *pBottom = *pTop + 5;
  pTab->GetParent()->InvalidateRect(rcTab);
  }
  pTab->Invalidate();
  }
  else
  {
  timeKillEvent(pTab->m_nTimerID);
  pTab->m_nTimerID = 0;
  pTab->m_nAction = SLIDE_TAB_STOP;
  }
  }
  int CSlideTab::SetHeight(int nHeight)
  {
  m_fVert = TRUE;
  if (nHeight > m_rcWnd.Height())
  m_nHeight = nHeight;
  else
  m_nHeight = m_rcWnd.Height();
  return m_nHeight;
  }
  int CSlideTab::SetWidth(int nWidth)
  {
  m_fVert = FALSE;
  if (nWidth > m_rcWnd.Height())
  m_nHeight = nWidth;
  else
  m_nHeight = m_rcWnd.Height();
  return m_nHeight;
  }
  BOOL CSlideTab::OnEraseBkgnd(CDC* pDC)
  {
  // TODO: 在此添加消息处理程序代码和/或调用默认值
  return TRUE;
  return CWnd::OnEraseBkgnd(pDC);
  }
  void CSlideTab::DrawTab(CDC* pDC)
  {
  CRect ClientRect;
  GetClientRect(ClientRect);
  pDC->Rectangle(ClientRect);
  }
  void CSlideTab::OnDestroy()
  {
  CWnd::OnDestroy();
  // TODO: 在此处添加消息处理程序代码
  if (m_nTimerID)
  timeKillEvent(m_nTimerID);
  }
  void CSlideTab::OnMove(int x, int y)
  {
  CWnd::OnMove(x, y);
  // TODO: 在此处添加消息处理程序代码
  }

Tags:一个 简单 滑动

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接