📄 mvwnd.cpp
字号:
// mvwnd.cpp
#include <windows.h>
#include <limits.h>
#include "msc.h"
#include "mvwnd.h"
#define DEBUG_RECT 0
int GetMonitorRect ( HWND hWnd, RECT *pRect ) ;
static HWND GetParentWindow ( HWND hWnd ) ;
static int IsChildWindow ( HWND hWnd ) ;
static int DebugRect ( HWND hWnd, RECT *pRectParent, RECT *pRect, POINT *pPoint ) ;
////////////////////////////////////////////
// 昐暘棪偱堏摦 //
////////////////////////////////////////////
// 壓惪偗娭悢
// 昐暘棪偵偼 0 偐傜 100 偺抣傪巜掕偡傞
// 昐暘棪偵 -1 傪巜掕偟偨傜丄堏摦偟側偄
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
static int MoveWindowByRatio ( HWND hWnd, HWND hWndParent, int nHorizontalRatio, int nVerticalRatio ) {
RECT Rect, RectParent ;
POINT Point ;
if ( nHorizontalRatio < -1 || nHorizontalRatio > 100 ) return 1 ;
if ( nVerticalRatio < -1 || nVerticalRatio > 100 ) return 1 ;
if ( IsChildWindow ( hWnd ) ) return 1 ;
if ( ! GetWindowRect ( hWnd, & Rect ) ) return 1 ;
if ( hWndParent ) { if ( ! GetWindowRect ( hWndParent, & RectParent ) ) return 1 ; }
else { if ( GetMonitorRect ( hWnd, & RectParent ) ) return 1 ; }
Point.x = Rect.left ;
Point.y = Rect.top ;
if ( nHorizontalRatio >= 0 ) {
Point.x = ( RectParent.right - RectParent.left ) - ( Rect.right - Rect.left ) ;
if ( ! hWndParent && Point.x < 0 ) Point.x = 0 ;
Point.x = RectParent.left + ( ( Point.x * nHorizontalRatio ) / 100 ) ;
}
if ( nVerticalRatio >= 0 ) {
Point.y = ( RectParent.bottom - RectParent.top ) - ( Rect.bottom - Rect.top ) ;
if ( ! hWndParent && Point.y < 0 ) Point.y = 0 ;
Point.y = RectParent.top + ( ( Point.y * nVerticalRatio ) / 100 ) ;
}
if ( DEBUG_RECT ) DebugRect ( hWnd, & RectParent, & Rect, & Point ) ;
if ( Point.x != Rect.left || Point.y != Rect.top ) SetWindowPos ( hWnd, NULL, Point.x, Point.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER ) ;
return 0 ;
}
// 僂傿儞僪僂傪僨僗僋僩僢僾撪偱堏摦偡傞
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
int MoveWindowInDesktop ( HWND hWnd, int nHorizontalRatio, int nVerticalRatio ) {
return MoveWindowByRatio ( hWnd, NULL, nHorizontalRatio, nVerticalRatio ) ;
}
// 僂傿儞僪僂傪恊僂傿儞僪僂撪偱堏摦偡傞
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
int MoveWindowInParent ( HWND hWnd, int nHorizontalRatio, int nVerticalRatio ) {
return MoveWindowByRatio ( hWnd, GetParentWindow ( hWnd ), nHorizontalRatio, nVerticalRatio ) ;
}
////////////////////////////////////////////
// 僂傿儞僪僂傪撪懁偵堏摦 //
////////////////////////////////////////////
// 壓惪偗娭悢
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
static int MoveWindowInto ( HWND hWnd, HWND hWndParent ) {
RECT Rect, RectParent ;
POINT Point ;
if ( IsChildWindow ( hWnd ) ) return 1 ;
if ( ! GetWindowRect ( hWnd, & Rect ) ) return 1 ;
if ( hWndParent ) { if ( ! GetWindowRect ( hWndParent, & RectParent ) ) return 1 ; }
else { if ( GetMonitorRect ( hWnd, & RectParent ) ) return 1 ; }
Point.x = Rect.left ;
Point.y = Rect.top ;
if ( RectParent.right < Rect.right ) Point.x = RectParent.right - ( Rect.right - Rect.left ) ;
if ( RectParent.bottom < Rect.bottom ) Point.y = RectParent.bottom - ( Rect.bottom - Rect.top ) ;
if ( RectParent.left > Rect.left ) Point.x = RectParent.left ;
if ( RectParent.top > Rect.top ) Point.y = RectParent.top ;
if ( DEBUG_RECT ) DebugRect ( hWnd, & RectParent, & Rect, & Point ) ;
if ( Point.x != Rect.left || Point.y != Rect.top ) SetWindowPos ( hWnd, NULL, Point.x, Point.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER ) ;
return 0 ;
}
// 僂傿儞僪僂傪僨僗僋僩僢僾撪偵堏摦偡傞
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
int MoveWindowIntoDesktop ( HWND hWnd ) {
return MoveWindowInto ( hWnd, NULL ) ;
}
// 僂傿儞僪僂傪恊僂傿儞僪僂撪偵堏摦偡傞
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
int MoveWindowIntoParent ( HWND hWnd ) {
return MoveWindowInto ( hWnd, GetParentWindow ( hWnd ) ) ;
}
////////////////////////////////////////////
// 僞僀僩儖僶乕傪撪懁偵堏摦 //
////////////////////////////////////////////
// 壓惪偗娭悢
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
static int MoveWindowTitleInto ( HWND hWnd, HWND hWndParent ) {
RECT Rect, RectParent ;
POINT Point ;
if ( IsChildWindow ( hWnd ) ) return 1 ;
if ( ! GetWindowRect ( hWnd, & Rect ) ) return 1 ;
if ( hWndParent ) { if ( ! GetWindowRect ( hWndParent, & RectParent ) ) return 1 ; }
else { if ( GetMonitorRect ( hWnd, & RectParent ) ) return 1 ; }
Point.x = Rect.left ;
Point.y = Rect.top ;
RECT RectRim ;
POINT PointRim ;
RectRim = RectParent ;
PointRim.x = ( Rect.right - Rect.left ) - 200 ;
PointRim.y = ( Rect.bottom - Rect.top ) - 200 ;
if ( PointRim.x < 0 ) PointRim.x = 0 ;
if ( PointRim.y < 0 ) PointRim.y = 0 ;
RectRim.bottom += PointRim.y ;
RectRim.right += PointRim.x ;
RectRim.left -= PointRim.x ;
if ( RectRim.right < Rect.right ) Point.x = RectRim.right - ( Rect.right - Rect.left ) ;
if ( RectRim.bottom < Rect.bottom ) Point.y = RectRim.bottom - ( Rect.bottom - Rect.top ) ;
if ( RectRim.left > Rect.left ) Point.x = RectRim.left ;
if ( RectRim.top > Rect.top ) Point.y = RectRim.top ;
if ( DEBUG_RECT ) DebugRect ( hWnd, & RectParent, & Rect, & Point ) ;
if ( Point.x != Rect.left || Point.y != Rect.top ) SetWindowPos ( hWnd, NULL, Point.x, Point.y, 0, 0, SWP_NOSIZE | SWP_NOZORDER ) ;
return 0 ;
}
// 僂傿儞僪僂偺僞僀僩儖僶乕傪僨僗僋僩僢僾撪偵堏摦偡傞
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
int MoveWindowTitleIntoDesktop ( HWND hWnd ) {
return MoveWindowTitleInto ( hWnd, NULL ) ;
}
// 僂傿儞僪僂偺僞僀僩儖僶乕傪恊僂傿儞僪僂撪偵堏摦偡傞
// 惉岟偟偨傜 0, 幐攕偟偨傜 0 埲奜傪曉偡
int MoveWindowTitleIntoParent ( HWND hWnd ) {
return MoveWindowTitleInto ( hWnd, GetParentWindow ( hWnd ) ) ;
}
////////////////////////////////////////////
// 撪晹娭悢 //
////////////////////////////////////////////
// 恊僂傿儞僪僂偺僴儞僪儖傪庢摼偡傞
static HWND GetParentWindow ( HWND hWnd ) {
if ( IsNT () ) return (HWND) GetWindowLongPtrW ( hWnd, GWLP_HWNDPARENT ) ;
else return (HWND) GetWindowLongPtrA ( hWnd, GWLP_HWNDPARENT ) ;
}
// 巕僂傿儞僪僂側傜 0 埲奜傪曉偡
static int IsChildWindow ( HWND hWnd ) {
if ( IsNT () ) return ( ( GetWindowLongPtrW ( hWnd, GWL_STYLE ) ) & WS_CHILD ) ? 1 : 0 ;
else return ( ( GetWindowLongPtrA ( hWnd, GWL_STYLE ) ) & WS_CHILD ) ? 1 : 0 ;
}
static int DebugRect ( HWND hWnd, RECT *pRectParent, RECT *pRect, POINT *pPoint ) {
char szBuffer [ MAX_PATH * 2 ] ;
*szBuffer = 0 ;
if ( pRectParent ) wsprintf ( strend ( szBuffer ), "(PARENT)\nleft: %d\ntop: %d\nright: %d\nbottom: %d\n\n", pRectParent->left, pRectParent->top, pRectParent->right, pRectParent->bottom ) ;
if ( pRect ) wsprintf ( strend ( szBuffer ), "(CHILD)\nleft: %d\ntop: %d\nright: %d\nbottom: %d\n\n", pRect->left, pRect->top, pRect->right, pRect->bottom ) ;
if ( pPoint ) wsprintf ( strend ( szBuffer ), "(New)\nx: %d\ny: %d\n", pPoint->x, pPoint->y ) ;
const char szTitle [] = "mvwnd.cpp debug" ;
MessageBox ( hWnd, szBuffer, szTitle, MB_OK ) ;
return 0 ;
}
////////////////////////////////////////////
// 撪晹娭悢 (儅儖僠儌僯僞) //
////////////////////////////////////////////
#ifndef HMONITOR_DECLARED
typedef HANDLE HMONITOR ;
#define MONITOR_DEFAULTTONULL 0x00000000
#define MONITOR_DEFAULTTOPRIMARY 0x00000001
#define MONITOR_DEFAULTTONEAREST 0x00000002
typedef struct tagMONITORINFO {
DWORD cbSize;
RECT rcMonitor;
RECT rcWork;
DWORD dwFlags;
} MONITORINFO, *LPMONITORINFO;
#endif
typedef HMONITOR ( WINAPI *MONITORFROMWINDOW ) ( HWND hwnd, DWORD dwFlags ) ;
typedef BOOL ( WINAPI *GETMONITORINFOA ) ( HMONITOR hMonitor, LPMONITORINFO lpmi ) ;
typedef BOOL ( WINAPI *GETMONITORINFOW ) ( HMONITOR hMonitor, LPMONITORINFO lpmi ) ;
// 巜掕偝傟偨僂傿儞僪僂偑強懏偡傞儌僯僞乕偺嶌嬈椞堟偺嬮宍傪庢摼偡傞
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 0 埲奜傪曉偡
int GetMonitorRect ( HWND hWnd, RECT *pRect ) {
if ( IsNT () ) {
if ( hWnd ) {
int IsMultiMonitor = 1 ;
MONITORFROMWINDOW MonitorFromWindow = NULL ;
GETMONITORINFOW GetMonitorInfoW = NULL ;
HINSTANCE hUser32 = GetModuleHandleW ( L"USER32.DLL" ) ;
if ( GETPROCADDRESS ( hUser32, MONITORFROMWINDOW, MonitorFromWindow ) ) IsMultiMonitor = 0 ;
if ( GETPROCADDRESS ( hUser32, GETMONITORINFOW, GetMonitorInfoW ) ) IsMultiMonitor = 0 ;
if ( IsMultiMonitor ) {
HMONITOR hMonitor = MonitorFromWindow ( hWnd, MONITOR_DEFAULTTONEAREST ) ;
if ( hMonitor ) {
MONITORINFO MonitorInfo = { sizeof (MONITORINFO) } ;
if ( GetMonitorInfoW ( hMonitor, & MonitorInfo ) ) {
*pRect = MonitorInfo.rcWork ;
return 0 ;
}
}
}
}
return ! SystemParametersInfoW ( SPI_GETWORKAREA, 0, pRect, 0 ) ;
}
else {
if ( hWnd ) {
int IsMultiMonitor = 1 ;
MONITORFROMWINDOW MonitorFromWindow = NULL ;
GETMONITORINFOA GetMonitorInfoA = NULL ;
HINSTANCE hUser32 = GetModuleHandleA ( "USER32.DLL" ) ;
if ( GETPROCADDRESS ( hUser32, MONITORFROMWINDOW, MonitorFromWindow ) ) IsMultiMonitor = 0 ;
if ( GETPROCADDRESS ( hUser32, GETMONITORINFOA, GetMonitorInfoA ) ) IsMultiMonitor = 0 ;
if ( IsMultiMonitor ) {
HMONITOR hMonitor = MonitorFromWindow ( hWnd, MONITOR_DEFAULTTONEAREST ) ;
if ( hMonitor ) {
MONITORINFO MonitorInfo = { sizeof (MONITORINFO) } ;
if ( GetMonitorInfoA ( hMonitor, & MonitorInfo ) ) {
*pRect = MonitorInfo.rcWork ;
return 0 ;
}
}
}
}
return ! SystemParametersInfoA ( SPI_GETWORKAREA, 0, pRect, 0 ) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -