home *** CD-ROM | disk | FTP | other *** search
/ PC Administrator / spravce.iso / TaskModule / src / SnapWnd.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-01  |  4.8 KB  |  130 lines

  1. // SnapWnd.cpp: implementation of the CSnapWnd 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. // DD/MM/YYYY - Modified by - Change
  32. // 02/11/2001 - Anish Mistry, suggested by Chris (chrishermon@hotmail.com) - Now use
  33. // SystemParametersInfo to get the desktop area.  Handle WM_SETTINGCHANGE, and adjust
  34. // the size of the desktop.  Changed the left and top of the snap rectangle to
  35. // m_rDesktopRect.left and m_rDesktopRect.top respectively.
  36. //////////////////////////////////////////////////////////////////////
  37.  
  38. #include "stdafx.h"
  39. #include "SnapWnd.h"
  40.  
  41. //////////////////////////////////////////////////////////////////////
  42. // Construction/Destruction
  43. //////////////////////////////////////////////////////////////////////
  44.  
  45. CSnapWnd::CSnapWnd()
  46. {
  47.     m_szMoveOffset.cx = 0;
  48.     m_szMoveOffset.cy = 0;
  49.     // snap pixels
  50.     m_nSnapPixels = 5;
  51.     // get the desktop rectangle for snappyness
  52.     SystemParametersInfo(SPI_GETWORKAREA, NULL, &m_rDesktopRect, 0);
  53. }
  54.  
  55. CSnapWnd::~CSnapWnd()
  56. {
  57.  
  58. }
  59.  
  60. LRESULT CSnapWnd::WndProc(HWND hDlg,UINT nMessage,WPARAM wParam,LPARAM lParam)
  61. {// begin WndProc
  62.     switch (nMessage) 
  63.     {
  64.     case WM_MOVING:
  65.         OnMoving(wParam,lParam);
  66.         break;
  67.     case WM_NCLBUTTONDOWN:
  68.         OnNcLButtonDown(wParam,lParam);
  69.         break;
  70.     case WM_SETTINGCHANGE:
  71.     case WM_DISPLAYCHANGE:
  72.         OnDisplayChange();
  73.         break;
  74.     }
  75.     return CMyWindow::WndProc(hDlg,nMessage,wParam,lParam);
  76. }// end WndProc
  77.  
  78. void CSnapWnd::OnMoving(WPARAM &wParam,LPARAM &lParam)
  79. {// begin OnMoving
  80.     RECT *pRect = (RECT *)lParam;
  81.     // calculate window size
  82.     SIZE szSize = {pRect->right-pRect->left,pRect->bottom-pRect->top};
  83.     POINT pt = {NULL};
  84.     GetCursorPos(&pt);
  85.     pRect->left = pt.x-m_szMoveOffset.cx;
  86.     pRect->top = pt.y-m_szMoveOffset.cy;
  87.     pRect->right = pRect->left+szSize.cx;
  88.     pRect->bottom = pRect->top+szSize.cy;
  89.     // snap window to screen edges
  90.     if(pRect->top < m_nSnapPixels && pRect->top > -m_nSnapPixels)
  91.     {// begin snap to top
  92.         pRect->top = m_rDesktopRect.top;
  93.         pRect->bottom = szSize.cy;
  94.     }// end snap to top
  95.     if(pRect->left < m_nSnapPixels && pRect->left > -m_nSnapPixels)
  96.     {// begin snap to left
  97.         pRect->left = m_rDesktopRect.left;
  98.         pRect->right = szSize.cx;
  99.     }// end snap to left
  100.     if(pRect->right < m_rDesktopRect.right+m_nSnapPixels && pRect->right > m_rDesktopRect.right-m_nSnapPixels)
  101.     {// begin snap to right
  102.         pRect->right = m_rDesktopRect.right;
  103.         pRect->left = m_rDesktopRect.right-szSize.cx;
  104.     }// end snap to right
  105.     if(pRect->bottom < m_rDesktopRect.bottom+m_nSnapPixels && pRect->bottom > m_rDesktopRect.bottom-m_nSnapPixels)
  106.     {// begin snap to bottom
  107.         pRect->bottom = m_rDesktopRect.bottom;
  108.         pRect->top = m_rDesktopRect.bottom-szSize.cy;
  109.     }// end snap to bottom
  110. }// end OnMoving
  111.  
  112. void CSnapWnd::OnNcLButtonDown(WPARAM wParam,LPARAM lParam)
  113. {// begin OnNcLButtonDown
  114.     if(wParam == HTCAPTION)
  115.     {// begin save mouse coord
  116.         // calculate drag offset
  117.         POINT pt = {LOWORD(lParam),HIWORD(lParam)};
  118.         RECT rRect = {NULL};
  119.         GetWindowRect(m_hWnd,&rRect);
  120.         m_szMoveOffset.cx = pt.x-rRect.left;
  121.         m_szMoveOffset.cy = pt.y-rRect.top;
  122.     }// end save mouse coord
  123. }// end OnNcLButtonDown
  124.  
  125. void CSnapWnd::OnDisplayChange(void)
  126. {// begin OnDisplayChange
  127.     // get the new desktop rectangle
  128.     SystemParametersInfo(SPI_GETWORKAREA, NULL, &m_rDesktopRect, 0);
  129. }// end OnDisplayChange
  130.