1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
| #include <windows.h>
#define SNAP_THRESHOLD 20 #define BUTTON_1 1001
int MOVE_SWITCH = 1;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(szCmdLine);
const TCHAR szAppName[] = TEXT("BorderlessWindow"); HWND hwnd; MSG msg; WNDCLASS wndclass = {0};
wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass)) { MessageBox(NULL, TEXT("Registration Failed!"), szAppName, MB_ICONERROR); return 0; }
hwnd = CreateWindow( szAppName, TEXT("Borderless Window"), WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 220, 600, NULL, NULL, hInstance, NULL ); CreateWindow( TEXT("BUTTON"), MOVE_SWITCH ? TEXT("开启") : TEXT("关闭"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 10,10,100,30, hwnd, (HMENU)BUTTON_1, hInstance, NULL );
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) { static POINT ptLastMousePos; static BOOL bDragging = FALSE; static RECT rcWindow;
switch (message) { case WM_LBUTTONDOWN: if (MOVE_SWITCH == 1){ bDragging = TRUE; SetCapture(hwnd); ptLastMousePos.x = (short)LOWORD(lParam); ptLastMousePos.y = (short)HIWORD(lParam); }; break;
case WM_LBUTTONUP: bDragging = FALSE; ReleaseCapture(); break;
case WM_MOUSEMOVE: if (bDragging && MOVE_SWITCH == 1) { const int screen_width = GetSystemMetrics(SM_CXSCREEN);
POINT ptCurrent = {(short)LOWORD(lParam), (short)HIWORD(lParam)}; int new_x = rcWindow.left + (ptCurrent.x - ptLastMousePos.x); int new_y = rcWindow.top + (ptCurrent.y - ptLastMousePos.y); const int window_width = rcWindow.right - rcWindow.left;
if (new_x <= SNAP_THRESHOLD && new_x >= -SNAP_THRESHOLD) { new_x = 0; } else if ((new_x + window_width) >= (screen_width - SNAP_THRESHOLD)) { new_x = screen_width - window_width; } if (new_y <= SNAP_THRESHOLD && new_y >= -SNAP_THRESHOLD) { new_y = 0; } SetWindowPos( hwnd, NULL, new_x, new_y, 0, 0, SWP_NOSIZE | SWP_NOZORDER ); } break; case WM_COMMAND: if (LOWORD(wParam) == BUTTON_1){ MOVE_SWITCH = !MOVE_SWITCH; SetWindowText(GetDlgItem(hwnd, BUTTON_1), MOVE_SWITCH ? TEXT("开启拖动") : TEXT("关闭拖动")); }; break; case WM_DESTROY: PostQuitMessage(0); break;
default: return DefWindowProc(hwnd, message, wParam, lParam); }
return 0; }
|