WinAPI test

 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
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
void init() {
srand(time(NULL));
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch(message) {
case WM_KEYDOWN:
//printf("%d\n", wParam);
if(wParam==123) PostQuitMessage(0);
if(wParam==17) Beep(2000, 1000);
if(wParam==16) Beep(1000, 1000);
if(wParam==46) Beep(500, 1000);
break;
case WM_SYSKEYDOWN:
//printf("%d\n", wParam);
if(wParam==18) Beep(3000, 1000);
break;
case WM_LBUTTONUP:
MessageBox(hWnd, TEXT("Click"), TEXT("action"), 0);
//PostQuitMessage(0);
break;
case WM_DESTROY:
//PostQuitMessage(0);
system("winapi.exe");
break;
case WM_MOUSEMOVE:
Beep(rand()%500, 100+rand()%100);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
ATOM RegMyWindowClass(HINSTANCE hInst, LPCSTR lpzClassName) {
WNDCLASS wcWindowClass={0};
wcWindowClass.lpfnWndProc=(WNDPROC)WndProc;
wcWindowClass.style=CS_HREDRAW|CS_VREDRAW;
wcWindowClass.hInstance=hInst;
wcWindowClass.lpszClassName=lpzClassName;
wcWindowClass.hCursor=LoadCursor(NULL, IDC_ARROW);
wcWindowClass.hbrBackground=(HBRUSH)COLOR_APPWORKSPACE;
return RegisterClass(&wcWindowClass);
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
init();
LPCTSTR lpzClass=TEXT("My Window Class!");
if(!RegMyWindowClass(hInstance, lpzClass)) return 1;
RECT screen_rect;
GetWindowRect(GetDesktopWindow(), &screen_rect);
int x=-100;
int y=-100;
int tx=GetSystemMetrics(SM_CXSCREEN);
int ty=GetSystemMetrics(SM_CYSCREEN);
HWND hWnd=CreateWindow(lpzClass, TEXT("Dialog Window"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, x, y, tx, ty, NULL, NULL, hInstance, NULL);
//HWND hWnd=CreateWindowEx(0, "name", "title", WS_POPUP|WS_SYSMENU, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), 0, 0, hInstance, 0);
if(!hWnd) return 2;
FreeConsole();
HMENU MenuHandle = GetSystemMenu(hWnd, false);
if(MenuHandle) DeleteMenu(MenuHandle, SC_CLOSE, MF_BYCOMMAND);
MSG msg={0};
int iGetOk=0;
while((iGetOk=GetMessage(&msg, NULL, 0, 0))!=0) {
if(iGetOk==-1) return 3;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
return 0;
}