home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / RunModule / src / SnapDlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-13  |  4.3 KB  |  121 lines

  1. // SnapDlg.cpp: implementation of the CSnapDlg class.
  2. //
  3. /*
  4. Copyright 2001 Anish Mistry. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8.  
  9.    1. Redistributions of source code must retain the above copyright notice, 
  10.    this list of conditions and the following disclaimer.
  11.    2. Redistributions in binary form must reproduce the above copyright notice,
  12.    this list of conditions and the following disclaimer in the documentation 
  13.    and/or other materials provided with the distribution.
  14.  
  15. THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED 
  16. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  17. AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS 
  18. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  19. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  22. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24.  
  25. The views and conclusions contained in the software and documentation are those
  26. of the authors and should not be interpreted as representing official policies,
  27. either expressed or implied, of Anish Mistry or AM Productions.
  28.  
  29. * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
  30. */
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. #include "stdafx.h"
  34. #include "SnapDlg.h"
  35.  
  36. //////////////////////////////////////////////////////////////////////
  37. // Construction/Destruction
  38. //////////////////////////////////////////////////////////////////////
  39.  
  40. CSnapDlg::CSnapDlg()
  41. {
  42.     m_szMoveOffset.cx = 0;
  43.     m_szMoveOffset.cy = 0;
  44.     // snap pixels
  45.     m_nSnapPixels = 5;
  46.     // get the desktop rectangle for snappyness
  47.     GetWindowRect(GetDesktopWindow(),&m_rDesktopRect);
  48. }
  49.  
  50. CSnapDlg::~CSnapDlg()
  51. {
  52.  
  53. }
  54.  
  55. LRESULT CSnapDlg::DlgProc(HWND hDlg,UINT nMessage,WPARAM wParam,LPARAM lParam)
  56. {// begin DlgProc
  57.     switch (nMessage) 
  58.     {
  59.     case WM_MOVING:
  60.         OnMoving(wParam,lParam);
  61.         break;
  62.     case WM_NCLBUTTONDOWN:
  63.         OnNcLButtonDown(wParam,lParam);
  64.         break;
  65.     }
  66.     return CMyDialog::DlgProc(hDlg,nMessage,wParam,lParam);
  67. }// end DlgProc
  68.  
  69. void CSnapDlg::OnMoving(WPARAM &wParam,LPARAM &lParam)
  70. {// begin OnMoving
  71.     RECT *pRect = (RECT *)lParam;
  72.     // calculate window size
  73.     SIZE szSize = {pRect->right-pRect->left,pRect->bottom-pRect->top};
  74.     POINT pt = {NULL};
  75.     GetCursorPos(&pt);
  76.     pRect->left = pt.x-m_szMoveOffset.cx;
  77.     pRect->top = pt.y-m_szMoveOffset.cy;
  78.     pRect->right = pRect->left+szSize.cx;
  79.     pRect->bottom = pRect->top+szSize.cy;
  80.     // snap window to screen edges
  81.     if(pRect->top < m_nSnapPixels && pRect->top > -m_nSnapPixels)
  82.     {// begin snap to top
  83.         pRect->top = 0;
  84.         pRect->bottom = szSize.cy;
  85.     }// end snap to top
  86.     if(pRect->left < m_nSnapPixels && pRect->left > -m_nSnapPixels)
  87.     {// begin snap to left
  88.         pRect->left = 0;
  89.         pRect->right = szSize.cx;
  90.     }// end snap to left
  91.     if(pRect->right < m_rDesktopRect.right+m_nSnapPixels && pRect->right > m_rDesktopRect.right-m_nSnapPixels)
  92.     {// begin snap to right
  93.         pRect->right = m_rDesktopRect.right;
  94.         pRect->left = m_rDesktopRect.right-szSize.cx;
  95.     }// end snap to right
  96.     if(pRect->bottom < m_rDesktopRect.bottom+m_nSnapPixels && pRect->bottom > m_rDesktopRect.bottom-m_nSnapPixels)
  97.     {// begin snap to bottom
  98.         pRect->bottom = m_rDesktopRect.bottom;
  99.         pRect->top = m_rDesktopRect.bottom-szSize.cy;
  100.     }// end snap to bottom
  101. }// end OnMoving
  102.  
  103. void CSnapDlg::OnNcLButtonDown(WPARAM wParam,LPARAM lParam)
  104. {// begin OnNcLButtonDown
  105.     if(wParam == HTCAPTION)
  106.     {// begin save mouse coord
  107.         // calculate drag offset
  108.         POINT pt = {LOWORD(lParam),HIWORD(lParam)};
  109.         RECT rRect = {NULL};
  110.         GetWindowRect(m_hDlg,&rRect);
  111.         m_szMoveOffset.cx = pt.x-rRect.left;
  112.         m_szMoveOffset.cy = pt.y-rRect.top;
  113.     }// end save mouse coord
  114. }// end OnNcLButtonDown
  115.  
  116. void CSnapDlg::OnDisplayChange(void)
  117. {// begin OnDisplayChange
  118.     // get the new desktop rectangle
  119.     GetWindowRect(GetDesktopWindow(),&m_rDesktopRect);
  120. }// end OnDisplayChange
  121.