home *** CD-ROM | disk | FTP | other *** search
- // SnapWnd.cpp: implementation of the CSnapWnd class.
- //
- /*
- Copyright 2001 Anish Mistry. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
-
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
-
- THIS SOFTWARE IS PROVIDED BY ANISH MISTRY ``AS IS'' AND ANY EXPRESS OR IMPLIED
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
- AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
- OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
- OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- The views and conclusions contained in the software and documentation are those
- of the authors and should not be interpreted as representing official policies,
- either expressed or implied, of Anish Mistry or AM Productions.
-
- * Variation of the FreeBSD License. http://www.freebsd.org/copyright/freebsd-license.html
- */
- // DD/MM/YYYY - Modified by - Change
- // 02/11/2001 - Anish Mistry, suggested by Chris (chrishermon@hotmail.com) - Now use
- // SystemParametersInfo to get the desktop area. Handle WM_SETTINGCHANGE, and adjust
- // the size of the desktop. Changed the left and top of the snap rectangle to
- // m_rDesktopRect.left and m_rDesktopRect.top respectively.
- //////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "SnapWnd.h"
-
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
-
- CSnapWnd::CSnapWnd()
- {
- m_szMoveOffset.cx = 0;
- m_szMoveOffset.cy = 0;
- // snap pixels
- m_nSnapPixels = 5;
- // get the desktop rectangle for snappyness
- SystemParametersInfo(SPI_GETWORKAREA, NULL, &m_rDesktopRect, 0);
- }
-
- CSnapWnd::~CSnapWnd()
- {
-
- }
-
- LRESULT CSnapWnd::WndProc(HWND hDlg,UINT nMessage,WPARAM wParam,LPARAM lParam)
- {// begin WndProc
- switch (nMessage)
- {
- case WM_MOVING:
- OnMoving(wParam,lParam);
- break;
- case WM_NCLBUTTONDOWN:
- OnNcLButtonDown(wParam,lParam);
- break;
- case WM_SETTINGCHANGE:
- case WM_DISPLAYCHANGE:
- OnDisplayChange();
- break;
- }
- return CMyWindow::WndProc(hDlg,nMessage,wParam,lParam);
- }// end WndProc
-
- void CSnapWnd::OnMoving(WPARAM &wParam,LPARAM &lParam)
- {// begin OnMoving
- RECT *pRect = (RECT *)lParam;
- // calculate window size
- SIZE szSize = {pRect->right-pRect->left,pRect->bottom-pRect->top};
- POINT pt = {NULL};
- GetCursorPos(&pt);
- pRect->left = pt.x-m_szMoveOffset.cx;
- pRect->top = pt.y-m_szMoveOffset.cy;
- pRect->right = pRect->left+szSize.cx;
- pRect->bottom = pRect->top+szSize.cy;
- // snap window to screen edges
- if(pRect->top < m_nSnapPixels && pRect->top > -m_nSnapPixels)
- {// begin snap to top
- pRect->top = m_rDesktopRect.top;
- pRect->bottom = szSize.cy;
- }// end snap to top
- if(pRect->left < m_nSnapPixels && pRect->left > -m_nSnapPixels)
- {// begin snap to left
- pRect->left = m_rDesktopRect.left;
- pRect->right = szSize.cx;
- }// end snap to left
- if(pRect->right < m_rDesktopRect.right+m_nSnapPixels && pRect->right > m_rDesktopRect.right-m_nSnapPixels)
- {// begin snap to right
- pRect->right = m_rDesktopRect.right;
- pRect->left = m_rDesktopRect.right-szSize.cx;
- }// end snap to right
- if(pRect->bottom < m_rDesktopRect.bottom+m_nSnapPixels && pRect->bottom > m_rDesktopRect.bottom-m_nSnapPixels)
- {// begin snap to bottom
- pRect->bottom = m_rDesktopRect.bottom;
- pRect->top = m_rDesktopRect.bottom-szSize.cy;
- }// end snap to bottom
- }// end OnMoving
-
- void CSnapWnd::OnNcLButtonDown(WPARAM wParam,LPARAM lParam)
- {// begin OnNcLButtonDown
- if(wParam == HTCAPTION)
- {// begin save mouse coord
- // calculate drag offset
- POINT pt = {LOWORD(lParam),HIWORD(lParam)};
- RECT rRect = {NULL};
- GetWindowRect(m_hWnd,&rRect);
- m_szMoveOffset.cx = pt.x-rRect.left;
- m_szMoveOffset.cy = pt.y-rRect.top;
- }// end save mouse coord
- }// end OnNcLButtonDown
-
- void CSnapWnd::OnDisplayChange(void)
- {// begin OnDisplayChange
- // get the new desktop rectangle
- SystemParametersInfo(SPI_GETWORKAREA, NULL, &m_rDesktopRect, 0);
- }// end OnDisplayChange
-