-
Notifications
You must be signed in to change notification settings - Fork 5
/
User32.lua
214 lines (163 loc) · 4.1 KB
/
User32.lua
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
local ffi = require "ffi"
local user32_ffi = require ("user32_ffi");
local User32Lib = ffi.load("User32");
--[=[
ffi.cdef[[
typedef struct _WindowClass {
WNDPROC MessageProc;
ATOM Registration;
HINSTANCE AppInstance;
char * ClassName;
int X;
int Y;
int Width;
int Height;
char *Title;
} User32WindowClass;
]]
--]=]
ffi.cdef[[
typedef struct _User32Window {
HWND Handle;
} NativeWindow, *PNativeWindow;
]]
NativeWindow = ffi.typeof("NativeWindow")
NativeWindow_mt = {
__index = {
Show = function(self)
User32Lib.ShowWindow(self.Handle, C.SW_SHOW)
end,
Update = function(self)
User32Lib.UpdateWindow(self.Handle)
end,
GetTitle = function(self)
local buf = ffi.new("char[?]", 256)
local lbuf = ffi.cast("intptr_t", buf)
if User32Lib.SendMessageA(self.WindowHandle, C.WM_GETTEXT, 255, lbuf) ~= 0 then
return ffi.string(buf)
end
end,
OnCreate = function(self)
print("User32Window:OnCreate")
return 0
end,
}
}
NativeWindow = ffi.metatype(NativeWindow, NativeWindow_mt)
function User32_MsgProc(hwnd, msg, wparam, lparam)
--print("User32_MsgProc: ", msg)
if (msg == C.WM_CREATE) then
--print("WM_CREATE")
local crstruct = ffi.cast("LPCREATESTRUCTA", lparam)
--print(crstruct.lpCreateParams)
local win = ffi.cast("PUser32Window", crstruct.lpCreateParams)
return win:OnCreate()
elseif (msg == C.WM_DESTROY) then
--print("WM_DESTROY")
C.PostQuitMessage(0)
return 0
end
local retValue = User32Lib.DefWindowProcA(hwnd, msg, wparam, lparam)
return retValue;
end
User32MSGHandler = {}
User32MSGHandler_mt = {
__index = User32MSGHandler,
}
User32MSGHandler.new = function(classname, msgproc, classStyle)
local appInstance = kernel32.GetModuleHandleA(nil)
msgproc = msgproc or User32_MsgProc
classStyle = classStyle or bit.bor(user32_ffi.CS_HREDRAW, user32_ffi.CS_VREDRAW, user32_ffi.CS_OWNDC);
local self = {}
self.AppInstance = appInstance
self.ClassName = ffi.cast("const char *", classname)
self.MessageProc = msgproc
setmetatable(self, User32MSGHandler_mt);
local winClass = ffi.new('WNDCLASSEXA', {
cbSize = ffi.sizeof("WNDCLASSEXA");
style = classStyle;
lpfnWndProc = self.MessageProc;
cbClsExtra = 0;
cbWndExtra = 0;
hInstance = self.AppInstance;
hIcon = nil;
hCursor = nil;
hbrBackground = nil;
lpszMenuName = nil;
lpszClassName = self.ClassName;
hIconSm = nil;
})
self.Registration = User32Lib.RegisterClassExA(winClass)
if (self.Registration == 0) then
print("Registration error")
--print(C.GetLastError())
end
return self
end
User32MSGHandler.CreateHandler = function(self, title, x, y, width, height, windowStyle)
x = x or 10
y = y or 10
width = width or 320
height = height or 240
windowStyle = windowStyle or C.WS_OVERLAPPEDWINDOW
self.Title = ffi.cast("const char *", title)
local dwExStyle = bit.bor(user32_ffi.WS_EX_APPWINDOW, user32_ffi.WS_EX_WINDOWEDGE)
local win = ffi.new("NativeWindow")
local hWnd = User32Lib.CreateWindowExA(
0,
self.ClassName,
self.Title,
windowStyle,
x,
y,
width,
height,
nil,
nil,
self.AppInstance,
win)
if hWnd == nil then
print("unable to create window")
else
win.Handle = hWnd
end
return win
end
ffi.cdef[[
typedef struct {
HWINSTA Handle;
} WindowStation;
]]
local WindowStation = ffi.typeof("WindowStation");
local WindowStation_mt = {
__gc = function(self)
end,
__new = function(ct, params)
end,
__index = {
Close = function(self)
return (User32Lib.CloseWindowStation(self.Handle) ~= 0) or false, User32Lib.GetLastError();
end,
},
}
ffi.metatype(WindowStation, WindowStation_mt);
--[[
Some functions, reflecting what's in the ffi interface
--]]
local SendInput = function(nInputs, pInputs, cbSize)
local res = User32Lib.SendInput(nInputs,pInputs,cbSize);
-- If the number of events inserted was zero,
-- then there was an error
if res == 0 then
return nil, User32Lib.GetLastError();
end
-- return the number of events that were inserted
return res
end
return {
FFI = user32_ffi,
Lib = User32Lib,
User32MSGHandler = User32MSGHandler,
NativeWindow = NativeWindow,
SendInput = SendInput,
}