#include <windows.h>
int alignType; //全局变量,用于记录窗体停靠状态
enum
{
ALIGN_NONE, //不停靠
ALIGN_TOP, //停靠上边
ALIGN_LEFT, //停靠左边
ALIGN_RIGHT //停靠右边
};
#define NEAR_SIZE 20 //定义自动停靠有效距离
#define NEAR_SIDE 5 //窗体隐藏后在屏幕上保留的像素,以使鼠标可以触及
int ScreenX=GetSystemMetrics(SM_CXSCREEN);
void HideSide(HWND hWnd, BOOL hide) ;
void OnMoving(HWND hWnd, LPRECT pRect) ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
void NearSide(HWND hWnd) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("窗口自动隐藏"), // window caption
WS_OVERLAPPEDWINDOW, // window style
ScreenX-300, // initial x position
100, // initial y position
200, // initial x size
500, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_TIMER: //定时器消息
{
POINT pt;
RECT rc;
GetCursorPos(&pt);
GetWindowRect(hWnd, &rc);
if (!PtInRect(&rc, pt)) //若鼠标不在窗体内,隐藏窗体.
{
KillTimer(hWnd, WM_TIMER);
HideSide(hWnd, TRUE);
}
break;
}
case WM_CREATE:
case WM_INITDIALOG: //初始化消息
{
//SetWindowPos(hWnd,NULL,rc.left,rc.top, 0, 0, SWP_NOSIZE | SWP_NOSENDCHANGING); //程序保存窗体上次靠位置,在这里恢复.
NearSide(hWnd);
break;
}
//这两个消息是在窗体移动开始时和结束时产生的,我们在窗体开始移动时关闭定时器,移动结束后再打开,这样避免窗体移动时隐藏,金山快译的浮动条就有这种情况出现.
case WM_ENTERSIZEMOVE:
{
KillTimer(hWnd, WM_TIMER);
break;
}
case WM_EXITSIZEMOVE:
{
SetTimer(hWnd, WM_TIMER, 500, NULL);
break;
}
case WM_MOUSEMOVE: //受到窗体移动消息时,判断窗体是否显示,
{
RECT rc;
GetWindowRect(hWnd, &rc);
if (rc.left< 0 || rc.top < 0 || rc.right > ScreenX) //未显示
HideSide(hWnd, FALSE);
break;
}
case WM_MOVING: //处理窗体移动时消息,实现自动停靠
{
OnMoving(hWnd, (LPRECT) lParam);
break;
}
case WM_MOVE:
{
//NearSide(hWnd);
//reak;
//保存窗体位置
}
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
void OnMoving(HWND hWnd, LPRECT pRect)
{
//未靠边界由pRect测试
if (alignType == ALIGN_NONE)
{
if (pRect->top < NEAR_SIZE) //在上边有效距离内,自动靠拢。
{
alignType = ALIGN_TOP;
pRect->bottom -= pRect->top;
pRect->top = 0;
}
if (pRect->left < NEAR_SIZE) //在左边有效距离内
{
alignType = ALIGN_LEFT;
pRect->right -= pRect->left;
pRect->left = 0;
}
else if (pRect->right + NEAR_SIZE > ScreenX) //在右边有效距离内,ScreenX为屏幕宽度,可由GetSystemMetrics(SM_CYSCREEN)得到。
{
alignType = ALIGN_RIGHT;
pRect->left += (ScreenX - pRect->right);
pRect->right = ScreenX;
}
}
else
{
//靠边界由鼠标测试
POINT pt;
GetCursorPos(&pt);
if (alignType == ALIGN_TOP)
{
if (pt.y > NEAR_SIZE) //由于我们移动窗体时,鼠标在标题栏内,当鼠标位置超过有效距离后,我们可以考虑用户要向下拖动鼠标。我们便解除上部停靠。
{
alignType = ALIGN_NONE;
pRect->bottom += NEAR_SIZE;
pRect->top = NEAR_SIZE;
}
else
{
pRect->bottom -= pRect->top;
pRect->top = 0;
if (pRect->left < NEAR_SIZE) //在上部停靠时,我们也考虑左右边角。
{
pRect->right -= pRect->left;
pRect->left = 0;
}
else if (pRect->right + NEAR_SIZE > ScreenX)
{
pRect->left += (ScreenX - pRect->right);
pRect->right = ScreenX;
}
}
}
if (alignType == ALIGN_LEFT)
{
if (pt.x - pRect->right > 0) //鼠标可以在整个标题条来回移动,所以我们不能简单用左边界和鼠标的距离来解除停靠,这里我们在鼠标离开右边界时解除停靠。
{
alignType = ALIGN_NONE;
pRect->right += NEAR_SIZE;
pRect->left = NEAR_SIZE;
}
else
{
pRect->right -= pRect->left;
pRect->left = 0;
if (pRect->top < NEAR_SIZE) //考虑左上角。
{
pRect->bottom -= pRect->top;
pRect->top = 0;
}
}
}
else if (alignType == ALIGN_RIGHT)
{
if (pt.x < pRect->left) //当鼠标离开左边界时,解除停靠。
{
alignType = ALIGN_NONE;
pRect->left -= NEAR_SIZE;
pRect->right -= NEAR_SIZE;
}
else
{
pRect->left += (ScreenX - pRect->right);
pRect->right = ScreenX;
if (pRect->top < NEAR_SIZE) //考虑右上角。
{
pRect->bottom -= pRect->top;
pRect->top = 0;
}
}
}
}
}
void NearSide(HWND hWnd)
{
int change = 0;
RECT rect;
GetWindowRect(hWnd, &rect);
alignType=ALIGN_NONE;
if (rect.left < NEAR_SIZE)
{
alignType = ALIGN_LEFT;
if((rect.left != 0) && rect.right!=NEAR_SIDE)
{
rect.right -= rect.left;
rect.left = 0;
change = 1;
}
}
else if(rect.right > ScreenX - NEAR_SIZE)
{
alignType = ALIGN_RIGHT;
if (rect.right != ScreenX && rect.left != ScreenX - NEAR_SIDE)
{
rect.left += (ScreenX - rect.right);
rect.right = ScreenX;
change = 1;
}
}
//调整上
else if (rect.top < NEAR_SIZE)
{
alignType = ALIGN_TOP;
if (rect.top != 0 && rect.bottom != NEAR_SIDE)
{
rect.bottom -= rect.top;
rect.top = 0;
change = 1;
}
}
if (change)
{
MoveWindow(hWnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE);
}
}
void HideSide(HWND hWnd, BOOL hide)
{
RECT rc;
int
- 1
- 2
- 3
- 4
- 5
前往页