QZoomView.h
上传用户:xp9161
上传日期:2009-12-21
资源大小:70k
文件大小:7k
源码类别:

Windows编程

开发平台:

Visual C++

  1. #pragma once
  2. // =======================
  3. // QZoomView
  4. //
  5. // QZoomView is a CScrollView with zooming facilities.
  6. // Use like you would use a CScrollView.
  7. //
  8. // QZoomView has four modes of operation:
  9. // - ZoomViewOff QZoomView behaves like a normal CScrollView;
  10. // - ZoomViewZoomIn clicking with the mouse zooms in, dragging zooms to rectangle;
  11. // - ZoomViewZoomOut clicking zooms out, dragging is identical to ZoomViewZoomIn;
  12. // - ZoomViewDrag if big enough, dragging scrolls the document.
  13. // Switch modes with SetZoomMode(), retrieve the mode with GetZoomMode().
  14. //
  15. // In any mode, the mouse wheel with the Ctrl key pressed zooms the window.
  16. // In one of the zoom modes, pressing the space bar temporarely switches to
  17. // ZoomViewDrag mode.
  18. //
  19. // In any mode (even in ZoomViewOff), the zoom factor can explicitly be set with the
  20. // ZoomTo() method.
  21. // The zoom factor is a float between 0.05 and 20.0. 1.0 is neutral (100%).
  22. // The zoom factor can be retrieved with GetZoom().
  23. //
  24. // A range of preset zoom factors is defined. You can zoom stepwise through this range by
  25. // the member functions ZoomIn() and ZoomOut(). Use ZoomToPreset() to directly zoom
  26. // to a preset value.
  27. // CanZoomIn() and CanZoomOut() return a BOOL indicating whether zoom is possible.
  28. // Modify the preset table with the SetPresets() method.
  29. //
  30. // The ZoomToWindow() member function lets the document fill the window.
  31. //
  32. // Like CScrollView, QZoomView is an abstract base class. You must derive another
  33. // class from it and at least override OnDraw().
  34. //
  35. // QZoomView has one overrideable member function, OnZoom(). You may use this in derived
  36. // classes. It is called after zoom is processed, but before the window is invalidated.
  37. // The default does nothing.
  38. //
  39. // The static member functions LoadCursor() enables you to set the cursors QZoomView displays.
  40. // It can be called before any instance of QZoomView is created. In default mode,
  41. // QZoomView always displays the standard cursor.
  42. //===============================
  43. // Version 1.1, October 8, 2003
  44. // Version 1.0, August 29, 2003
  45. // (c) Sjaak Priester, Amsterdam
  46. // www.sjaakpriester.nl
  47. //
  48. // Freeware. Use at your own risk. Comments welcome.
  49. #include "QTracker.h"
  50. class QZoomView : public CScrollView
  51. {
  52. // Construction
  53. protected:
  54. QZoomView();           // protected constructor used by dynamic creation
  55. virtual ~QZoomView();
  56. public:
  57. // Initializing
  58. BOOL SetPresets(const float * pPresets, int count);
  59. // Initialize the list of preset zoom factors. If not initialized, QZoomView
  60. // uses a default list.
  61. // The preset zoom factors must be a list of increasing positive values
  62. // between 0.05f and 20.0f.
  63. // Return: TRUE if succeeded; FALSE if error, list not changed.
  64. enum CursorType
  65. {
  66. CursorLoupe,
  67. CursorLoupePlus,
  68. CursorLoupeMinus,
  69. CursorGripOpen,
  70. CursorGripClosed
  71. };
  72. static BOOL LoadCursor(CursorType type, UINT nResourceID, HINSTANCE hInst = NULL);
  73. static BOOL LoadCursor(int type, UINT nResourceID, HINSTANCE hInst = NULL)
  74. { return LoadCursor((CursorType) type, nResourceID, hInst) ; }
  75. // Load the cursors QZoomView shows. All instances of QZoomView share the same
  76. // cursors, so loading is only needed once.
  77. // Operation
  78. enum ZoomViewMode
  79. {
  80. ZoomViewOff,
  81. ZoomViewZoomIn,
  82. ZoomViewZoomOut,
  83. ZoomViewDrag
  84. };
  85. ZoomViewMode SetZoomMode(ZoomViewMode newMode)
  86. { ZoomViewMode temp(m_Mode); m_Mode = newMode; return temp; }
  87. // Set the mode. Returns previous mode.
  88. ZoomViewMode GetZoomMode() const { return m_Mode; }
  89. // Get the current mode.
  90. // Attributes
  91. float GetZoom() const { return m_Zoom; }
  92. // Get the current zoom factor.
  93. int GetPresetZoom() const { return m_PresetIndex; }
  94. // Get the current preset zoom. If -1, current zoom factor is not in the preset list.
  95. int GetPresetCount() const { return m_PresetCount; }
  96. // Get the number of available presets.
  97. CRect VisibleRect(void);
  98. // Get the visible rectangle in logical coordinates (added in v. 1.1_.
  99. // Methods
  100. float ZoomTo(float zoom);
  101. // Set the zoom factor. Returns previous zoom factor.
  102. // zoom may be any value between 0.05 and 20.0.
  103. void ZoomToPreset(int i, const LPPOINT pPoint = NULL);
  104. // Set the zoom factor to the i'th preset value.
  105. // If pPoint is not NULL, scrolls to pPoint (logical coordinates).
  106. void ZoomToRectangle(CRect rect);
  107. // Zoom to let rect conveniently fill the window. Logical coordinates.
  108. BOOL ZoomIn(const LPPOINT pPoint = NULL);
  109. BOOL CanZoomIn() const { return m_Presets && m_Zoom < m_Presets[m_PresetCount - 1]; }
  110. // Zoom In one step, if possible.
  111. // If pPoint is not NULL, scrolls to pPoint (logical coordinates).
  112. // Return: TRUE if zoomed, FALSE if end of range.
  113. BOOL ZoomOut(const LPPOINT pPoint = NULL);
  114. BOOL CanZoomOut() const { return m_Presets && m_Zoom > m_Presets[0]; }
  115. // Zoom Out one step, if possible.
  116. // If pPoint is not NULL, scrolls to pPoint (logical coordinates).
  117. // Return: TRUE if zoomed, FALSE if end of range.
  118. void ZoomToWindow(void);
  119. // Zoom to let the document conveniently fill the window.
  120. BOOL ScrollToCenter(CPoint point);
  121. // If possible, scroll point (logical coordinates) to the center of the window.
  122. // Return: TRUE if scrolled, FALSE if not.
  123. enum { NoBeep = 0xffff };
  124. UINT m_MessageBeep;
  125. // Set this value to the type of MessageBeep that QZoomView sounds to indicate the end
  126. // of zoom range. NoBeep to disable. Default is MB_ICONHAND.
  127. protected:
  128. // Overrideables
  129. virtual void OnZoom(float Zoom, float PreviousZoom);
  130. // Called when zoom factor is changed, before window is redrawn. Default does nothing.
  131. // Implementation
  132. // Overrides
  133. virtual void OnDraw(CDC* pDC) = 0;
  134. virtual void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo = NULL);
  135. // Message handlers
  136. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  137. afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
  138. afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
  139. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  140. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  141. void SetCursor(void);
  142. float m_Zoom;
  143. int m_PresetIndex;
  144. ZoomViewMode m_Mode;
  145. int m_PresetCount;
  146. float *m_Presets;
  147. int m_WheelDelta;
  148. static float m_DefaultPresets[];
  149. static HCURSOR m_Cursors[5];
  150. DECLARE_DYNAMIC(QZoomView)
  151. DECLARE_MESSAGE_MAP()
  152. // Diagnostics
  153. #ifdef _DEBUG
  154. public:
  155. virtual void AssertValid() const;
  156. virtual void Dump(CDumpContext& dc) const;
  157. #endif
  158. // Small helper class, based on my versatile QTracker class for tracking operations.
  159. class Dragger : public QTracker
  160. {
  161. public:
  162. Dragger(CView * pView) : QTracker(pView) { }
  163. int Track(CPoint point) { return QTracker::Track(NULL, 0, point, FALSE); }
  164. protected:
  165. virtual int OnMouseMessage(UINT msg, UINT nFlags, CPoint point);
  166. };
  167. };