Skip to content

Commit 6c00b2d

Browse files
committed
+ wWinMain centering, escapekey, hotkey, morewindow
1 parent 250c362 commit 6c00b2d

4 files changed

Lines changed: 270 additions & 0 deletions

File tree

‎centering.c‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <windows.h>
2+
3+
#pragma comment(lib, "user32.lib")
4+
5+
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
6+
void CenterWindow(HWND);
7+
8+
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
9+
PWSTR pCmdLine, int nCmdShow)
10+
{
11+
MSG msg;
12+
13+
WNDCLASSW wc = {0};
14+
wc.lpszClassName = L"Center";
15+
wc.hInstance = hInstance;
16+
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
17+
wc.lpfnWndProc = WndProc;
18+
wc.hCursor = LoadCursor(0, IDC_ARROW);
19+
20+
RegisterClassW(&wc);
21+
CreateWindowW(wc.lpszClassName, L"Center",
22+
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
23+
100, 100, 400, 300, 0, 0, hInstance, 0);
24+
25+
while(GetMessage(&msg, NULL, 0, 0))
26+
{
27+
TranslateMessage(&msg);
28+
DispatchMessage(&msg);
29+
}
30+
31+
return (int)msg.wParam;
32+
}
33+
34+
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
35+
{
36+
switch(msg)
37+
{
38+
case WM_CREATE:
39+
CenterWindow(hwnd);
40+
break;
41+
case WM_DESTROY:
42+
PostQuitMessage(0);
43+
break;
44+
}
45+
return DefWindowProcW(hwnd, msg, wParam, lParam);
46+
}
47+
48+
void CenterWindow(HWND hwnd)
49+
{
50+
RECT rc = {0};
51+
52+
GetWindowRect(hwnd, &rc);
53+
int w = rc.right - rc.left;
54+
int h = rc.bottom - rc.top;
55+
56+
int screen_w = GetSystemMetrics(SM_CXSCREEN);
57+
int screen_h = GetSystemMetrics(SM_CYSCREEN);
58+
59+
SetWindowPos(hwnd, HWND_TOP, (screen_w - w)/2,
60+
(screen_h - h)/2, 0, 0, SWP_NOSIZE);
61+
}

‎escapekey.c‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <windows.h>
2+
#pragma comment(lib, "user32.lib")
3+
4+
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
5+
6+
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
7+
PWSTR pCmdLine, int nCmdShow)
8+
{
9+
MSG msg;
10+
11+
WNDCLASSW wc = {0};
12+
wc.lpszClassName = L"Escape";
13+
wc.hInstance = hInstance;
14+
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
15+
wc.lpfnWndProc = WndProc;
16+
17+
RegisterClassW(&wc);
18+
CreateWindowW(wc.lpszClassName, wc.lpszClassName,
19+
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
20+
100, 100, 400, 300, 0, 0, hInstance, 0);
21+
22+
while(GetMessage(&msg, NULL, 0, 0))
23+
{
24+
TranslateMessage(&msg);
25+
DispatchMessage(&msg);
26+
}
27+
return (int)msg.wParam;
28+
}
29+
30+
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
31+
{
32+
switch(msg)
33+
{
34+
case WM_KEYDOWN:
35+
if(wParam == VK_ESCAPE)
36+
{
37+
int ret = MessageBoxW(hwnd, L"Are you sure to quit?", L"Message", MB_OKCANCEL);
38+
if(IDOK == ret)
39+
SendMessage(hwnd, WM_CLOSE, 0, 0);
40+
}
41+
break;
42+
43+
case WM_DESTROY:
44+
PostQuitMessage(0);
45+
break;
46+
}
47+
48+
return DefWindowProcW(hwnd, msg, wParam, lParam);
49+
}

‎hotkey.c‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <windows.h>
2+
3+
#pragma comment(lib, "user32.lib")
4+
5+
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
6+
void CenterWindow(HWND);
7+
8+
#define ID_HOTKEY 1
9+
10+
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
11+
PWSTR lpCmdLine, int nCmdShow)
12+
{
13+
HWND hwnd;
14+
MSG msg;
15+
16+
WNDCLASSW wc = {0};
17+
wc.lpszClassName = L"Application";
18+
wc.hInstance = hInstance;
19+
wc.lpfnWndProc = WndProc;
20+
21+
RegisterClassW(&wc);
22+
hwnd = CreateWindowW(wc.lpszClassName, L"Hot Key",
23+
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
24+
100, 100, 400, 300, 0, 0, 0, 0);
25+
26+
while(GetMessage(&msg, NULL, 0, 0))
27+
{
28+
TranslateMessage(&msg);
29+
DispatchMessage(&msg);
30+
}
31+
32+
return msg.wParam;
33+
}
34+
35+
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
36+
{
37+
switch(msg)
38+
{
39+
case WM_CREATE:
40+
RegisterHotKey(hwnd, ID_HOTKEY, MOD_CONTROL, 0x43);
41+
break;
42+
43+
case WM_HOTKEY:
44+
if((wParam) == ID_HOTKEY)
45+
CenterWindow(hwnd);
46+
break;
47+
48+
case WM_DESTROY:
49+
UnregisterHotKey(hwnd, ID_HOTKEY);
50+
PostQuitMessage(0);
51+
break;
52+
}
53+
54+
return DefWindowProcW(hwnd, msg, wParam, lParam);
55+
}
56+
57+
void CenterWindow(HWND hwnd) {
58+
59+
RECT rc = {0};
60+
61+
GetWindowRect(hwnd, &rc);
62+
int win_w = rc.right - rc.left;
63+
int win_h = rc.bottom - rc.top;
64+
65+
int screen_w = GetSystemMetrics(SM_CXSCREEN);
66+
int screen_h = GetSystemMetrics(SM_CYSCREEN);
67+
68+
SetWindowPos(hwnd, HWND_TOP, (screen_w - win_w)/2,
69+
(screen_h - win_h)/2, 0, 0, SWP_NOSIZE);
70+
}

‎morewindows.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <windows.h>
2+
#pragma comment(lib, "user32.lib")
3+
#pragma comment(lib, "gdi32.lib")
4+
5+
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
6+
LRESULT CALLBACK PanelProc(HWND, UINT, WPARAM, LPARAM);
7+
8+
void RegisterRedPanelClass(void);
9+
void RegisterBluePanelClass(void);
10+
11+
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
12+
PWSTR lpCmdLine, int nCmdShow)
13+
{
14+
MSG msg;
15+
16+
WNDCLASSW wc = {0};
17+
wc.lpszClassName = L"Windows";
18+
wc.hInstance = hInstance;
19+
wc.lpfnWndProc = WndProc;
20+
21+
RegisterClassW(&wc);
22+
CreateWindowW(wc.lpszClassName, L"Windows",
23+
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
24+
100, 100, 400, 300, 0, 0, hInstance, 0);
25+
26+
while(GetMessage(&msg, NULL, 0, 0))
27+
{
28+
TranslateMessage(&msg);
29+
DispatchMessage(&msg);
30+
}
31+
32+
return (int)msg.wParam;
33+
}
34+
35+
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
36+
WPARAM wParam, LPARAM lParam)
37+
{
38+
switch(msg)
39+
{
40+
case WM_CREATE:
41+
RegisterRedPanelClass();
42+
CreateWindowW(L"RedPanelClass", NULL,
43+
WS_CHILD | WS_VISIBLE,
44+
20, 20, 80, 80,
45+
hwnd, (HMENU)1, NULL, NULL);
46+
RegisterBluePanelClass();
47+
CreateWindowW(L"BluePanelClass", NULL,
48+
WS_CHILD | WS_VISIBLE,
49+
120, 20, 80, 80,
50+
hwnd, (HMENU)2, NULL, NULL);
51+
break;
52+
53+
case WM_DESTROY:
54+
PostQuitMessage(0);
55+
break;
56+
}
57+
return DefWindowProcW(hwnd, msg, wParam, lParam);
58+
}
59+
60+
LRESULT CALLBACK PanelProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
61+
{
62+
switch(msg)
63+
{
64+
case WM_LBUTTONUP:
65+
MessageBeep(MB_OK);
66+
break;
67+
}
68+
return DefWindowProcW(hwnd, msg, wParam, lParam);
69+
}
70+
71+
void RegisterRedPanelClass()
72+
{
73+
HBRUSH hb = CreateSolidBrush(RGB(255, 0, 0));
74+
WNDCLASSW wc = {0};
75+
wc.lpszClassName = L"RedPanelClass";
76+
wc.hbrBackground = hb;
77+
wc.lpfnWndProc = PanelProc;
78+
wc.hCursor = LoadCursor(0, IDC_ARROW);
79+
RegisterClassW(&wc);
80+
}
81+
82+
void RegisterBluePanelClass()
83+
{
84+
WNDCLASSW wc = {0};
85+
wc.lpszClassName = L"BluePanelClass";
86+
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 255));
87+
wc.lpfnWndProc = PanelProc;
88+
wc.hCursor = LoadCursor(0, IDC_ARROW);
89+
RegisterClassW(&wc);
90+
}

0 commit comments

Comments
 (0)