windows_gui 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4e7b9bc05401af4e22a70c7291a2f93a842543f
4
+ data.tar.gz: 78463395031583b1bf39ab74bfdc1ec1ccd4d858
5
+ SHA512:
6
+ metadata.gz: 6e228db38f722bbf973f2a61d00fb8f63bbeba846103f4d2d21a44e4f97b696286b9d572089651464f2cb720d896fd04d39dd643c267e09942c40375ec20c78e
7
+ data.tar.gz: 8d5e01c2459b05b14975755fccb9d771343306cc166c9fd65ddefee22835351e31a6dd4119836b6a34c16fbf1915e1e87f5938d8f46a84c2deb95a1b948e11a7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2017 Radoslav Peev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # windows_gui
2
+
3
+ Ruby FFI (x86) bindings to essential GUI related Windows APIs
4
+
5
+ ![Screenshot](./screenshot.png)
6
+
7
+ ## Install
8
+
9
+ gem install windows_gui
10
+
11
+ ## Use
12
+
13
+ See examples folder
data/RELNOTES.md ADDED
@@ -0,0 +1,9 @@
1
+ # Release Notes
2
+
3
+ ## 2.0.0
4
+
5
+ Rename library to windows_gui and ensure it works with recent ruby
6
+
7
+ ## 1.0.2
8
+
9
+ Recover source from gem
@@ -0,0 +1,261 @@
1
+ require 'windows_gui'
2
+
3
+ include WindowsGUI
4
+
5
+ WndExtra = Struct.new(
6
+ :haccel,
7
+ :hmf
8
+ )
9
+
10
+ def OnCreate(hwnd,
11
+ cs
12
+ )
13
+ xtra = Util::Id2Ref[GetWindowLong(hwnd, GWL_USERDATA)]
14
+
15
+ hsys = GetSystemMenu(hwnd, 0)
16
+ InsertMenu(hsys, SC_CLOSE, MF_STRING, SYSCMD[:ITEM1], L("Item&1\tAlt+S"))
17
+ InsertMenu(hsys, SC_CLOSE, MF_SEPARATOR, 0, nil)
18
+
19
+ hbar = CreateMenu()
20
+ hmenu1 = CreatePopupMenu()
21
+ AppendMenu(hmenu1, MF_STRING, CMD[:ITEM1], L("Item&1\tAlt+I"))
22
+ AppendMenu(hbar, MF_POPUP, hmenu1.to_i, L('Menu&1'))
23
+ SetMenu(hwnd, hbar)
24
+
25
+ accels = [
26
+ [FVIRTKEY | FALT, 'S'.ord, SYSCMD[:ITEM1]],
27
+ [FVIRTKEY | FALT, 'I'.ord, CMD[:ITEM1]]
28
+ ]
29
+
30
+ FFI::MemoryPointer.new(ACCEL, accels.count) { |paccels|
31
+ accels.each_with_index { |data, i|
32
+ accel = ACCEL.new(paccels + i * ACCEL.size)
33
+
34
+ accel[:fVirt], accel[:key], accel[:cmd] = data
35
+ }
36
+
37
+ xtra[:haccel] = CreateAcceleratorTable(paccels, accels.count)
38
+ }
39
+
40
+ NONCLIENTMETRICS.new { |ncm|
41
+ ncm[:cbSize] = ncm.size
42
+
43
+ SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.size, ncm, 0);
44
+ xtra[:hmf] = CreateFontIndirect(ncm[:lfMenuFont])
45
+ }
46
+
47
+ hbtn1 = CreateWindowEx(
48
+ 0, L('Button'), L('&Button1'), WS_CHILD | WS_CLIPSIBLINGS |
49
+ WS_VISIBLE | WS_TABSTOP,
50
+ *DPIAwareXY(10, 10, 100, 25),
51
+ hwnd, FFI::Pointer.new(CMD[:BUTTON1]), GetModuleHandle(nil), nil
52
+ )
53
+
54
+ SendMessage(hbtn1, WM_SETFONT, xtra[:hmf].to_i, 1)
55
+
56
+ CreateWindowEx(
57
+ 0, L('Static'), L(''), WS_CHILD | WS_CLIPSIBLINGS,
58
+ 0, 0, 0, 0,
59
+ hwnd, FFI::Pointer.new(CMD[:FOCUS]), GetModuleHandle(nil), nil
60
+ )
61
+
62
+ 0
63
+ end
64
+
65
+ def OnDestroy(hwnd)
66
+ xtra = Util::Id2Ref[GetWindowLong(hwnd, GWL_USERDATA)]
67
+
68
+ DestroyAcceleratorTable(xtra[:haccel])
69
+ DeleteObject(xtra[:hmf])
70
+
71
+ PostQuitMessage(0); 0
72
+ end
73
+
74
+ def OnActivate(hwnd,
75
+ state, minimized,
76
+ hother
77
+ )
78
+ SetFocus(GetDlgItem(hwnd, CMD[:FOCUS])) if state != WA_INACTIVE
79
+
80
+ 0
81
+ end
82
+
83
+ def OnSysItem1(lParam,
84
+ hwnd
85
+ )
86
+ MessageBox(hwnd,
87
+ L(__method__.to_s),
88
+ APPNAME,
89
+ MB_ICONINFORMATION
90
+ )
91
+
92
+ 0
93
+ end
94
+
95
+ def OnItem1(verb,
96
+ hctl, hwnd
97
+ )
98
+ MessageBox(hwnd,
99
+ L(__method__.to_s),
100
+ APPNAME,
101
+ MB_ICONINFORMATION
102
+ )
103
+
104
+ =begin
105
+ verb:
106
+ 0 - menu
107
+ 1 - accelerator
108
+ =end
109
+
110
+ raise 'WM_COMMAND hctl must be NULL for menu/accelerator' unless
111
+ hctl.null?
112
+
113
+ EnableWindow(GetDlgItem(hwnd, CMD[:BUTTON1]), 1)
114
+ EnableMenuItem(GetMenu(hwnd), CMD[:ITEM1], MF_GRAYED)
115
+
116
+ 0
117
+ end
118
+
119
+ def OnButton1(verb,
120
+ hctl, hwnd
121
+ )
122
+ MessageBox(hwnd,
123
+ L(__method__.to_s),
124
+ APPNAME,
125
+ MB_ICONINFORMATION
126
+ )
127
+
128
+ =begin
129
+ verb:
130
+ BN_xxx
131
+ =end
132
+
133
+ raise 'WM_COMMAND hctl must NOT be NULL for control' if
134
+ hctl.null?
135
+
136
+ EnableMenuItem(GetMenu(hwnd), CMD[:ITEM1], MF_ENABLED)
137
+ EnableWindow(hctl, 0)
138
+
139
+ 0
140
+ end
141
+
142
+ WindowProc = FFI::Function.new(:long,
143
+ [:pointer, :uint, :uint, :long],
144
+ convention: :stdcall
145
+ ) { |hwnd, uMsg, wParam, lParam|
146
+ begin
147
+ result = case uMsg
148
+ when WM_NCCREATE
149
+ DefWindowProc(hwnd, uMsg, wParam, lParam)
150
+
151
+ SetWindowLong(hwnd,
152
+ GWL_USERDATA,
153
+ CREATESTRUCT.new(FFI::Pointer.new(lParam))[:lpCreateParams].to_i
154
+ )
155
+
156
+ 1
157
+ when WM_CREATE
158
+ OnCreate(hwnd, CREATESTRUCT.new(FFI::Pointer.new(lParam)))
159
+ when WM_DESTROY
160
+ OnDestroy(hwnd)
161
+
162
+ when WM_ACTIVATE
163
+ OnActivate(hwnd,
164
+ LOWORD(wParam), HIWORD(wParam) != 0,
165
+ FFI::Pointer.new(lParam)
166
+ )
167
+
168
+ when WM_SYSCOMMAND
169
+ id = wParam & 0xfff0
170
+
171
+ case id
172
+ when SYSCMD[:ITEM1]
173
+ OnSysItem1(lParam, hwnd)
174
+ end
175
+ when WM_COMMAND
176
+ id, verb = LOWORD(wParam), HIWORD(wParam)
177
+ hctl = FFI::Pointer.new(lParam)
178
+
179
+ case id
180
+ when CMD[:ITEM1]
181
+ OnItem1(verb, hctl, hwnd)
182
+ when CMD[:BUTTON1]
183
+ OnButton1(verb, hctl, hwnd)
184
+ end
185
+ end
186
+
187
+ result || DefWindowProc(hwnd, uMsg, wParam, lParam)
188
+ rescue SystemExit => ex
189
+ PostQuitMessage(ex.status)
190
+ rescue
191
+ case MessageBox(hwnd,
192
+ L(Util.FormatException($!)),
193
+ APPNAME,
194
+ MB_ABORTRETRYIGNORE | MB_ICONERROR
195
+ )
196
+ when IDABORT
197
+ PostQuitMessage(2)
198
+ when IDRETRY
199
+ retry
200
+ end
201
+ end
202
+ }
203
+
204
+ def WinMain
205
+ Util.Id2RefTrack(xtra = WndExtra.new)
206
+
207
+ WNDCLASSEX.new { |wc|
208
+ wc[:cbSize] = wc.size
209
+ wc[:lpfnWndProc] = WindowProc
210
+ wc[:cbWndExtra] = FFI::Type::Builtin::POINTER.size
211
+ wc[:hInstance] = GetModuleHandle(nil)
212
+ wc[:hIcon] = LoadIcon(nil, IDI_APPLICATION)
213
+ wc[:hCursor] = LoadCursor(nil, IDC_ARROW)
214
+ wc[:hbrBackground] = FFI::Pointer.new(COLOR_WINDOW + 1)
215
+
216
+ PWSTR(APPNAME) { |className|
217
+ wc[:lpszClassName] = className
218
+
219
+ DetonateLastError(0, :RegisterClassEx,
220
+ wc
221
+ )
222
+ }
223
+ }
224
+
225
+ hwnd = CreateWindowEx(
226
+ 0, APPNAME, APPNAME, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
227
+ CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
228
+ nil, nil, GetModuleHandle(nil), FFI::Pointer.new(xtra.object_id)
229
+ )
230
+
231
+ raise "CreateWindowEx failed (last error: #{GetLastError()})" if
232
+ hwnd.null? && GetLastError() != 0
233
+
234
+ exit(0) if hwnd.null?
235
+
236
+ ShowWindow(hwnd, SW_SHOWNORMAL)
237
+ UpdateWindow(hwnd)
238
+
239
+ MSG.new { |msg|
240
+ until DetonateLastError(-1, :GetMessage,
241
+ msg, nil, 0, 0
242
+ ) == 0
243
+ if TranslateAccelerator(hwnd, xtra[:haccel], msg) == 0 &&
244
+ IsDialogMessage(hwnd, msg) == 0
245
+
246
+ TranslateMessage(msg)
247
+ DispatchMessage(msg)
248
+ end
249
+ end
250
+
251
+ exit(msg[:wParam])
252
+ }
253
+ rescue
254
+ MessageBox(hwnd,
255
+ L(Util.FormatException($!)),
256
+ APPNAME,
257
+ MB_ICONERROR
258
+ ); exit(1)
259
+ end
260
+
261
+ WinMain()
@@ -0,0 +1,9 @@
1
+ require 'windows_gui'
2
+
3
+ include WindowsGUI
4
+
5
+ MessageBox(nil,
6
+ L('Hello, world!'),
7
+ L('Hello'),
8
+ MB_ICONINFORMATION
9
+ )
@@ -0,0 +1,12 @@
1
+ WINDOWS_GUI_VISUAL_STYLES = false
2
+ WINDOWS_GUI_DPI_AWARE = false
3
+
4
+ require 'windows_gui'
5
+
6
+ include WindowsGUI
7
+
8
+ MessageBox(nil,
9
+ L('Hello, world!'),
10
+ L('Hello'),
11
+ MB_ICONINFORMATION
12
+ )
@@ -0,0 +1,19 @@
1
+ require 'windows_gui'
2
+
3
+ include WindowsGUI
4
+
5
+ unless respond_to?(:MessageBoxTimeout)
6
+ MessageBox(nil,
7
+ L('MessageBoxTimeout is not supported'),
8
+ nil,
9
+ MB_ICONERROR
10
+ ); exit(-1)
11
+ end
12
+
13
+ MessageBoxTimeout(nil,
14
+ L("Hello, world!\n\n(Disappearing in 3 seconds...)"),
15
+ L('Hello'),
16
+ MB_ICONINFORMATION,
17
+ 0,
18
+ 3000
19
+ )
@@ -0,0 +1,96 @@
1
+ require 'windows_gui'
2
+
3
+ include WindowsGUI
4
+
5
+ def OnCreate(hwnd,
6
+ cs
7
+ )
8
+ SetLayeredWindowAttributes(hwnd, 0, 128, LWA_ALPHA)
9
+
10
+ 0
11
+ end
12
+
13
+ def OnDestroy(hwnd)
14
+ PostQuitMessage(0); 0
15
+ end
16
+
17
+ WindowProc = FFI::Function.new(:long,
18
+ [:pointer, :uint, :uint, :long],
19
+ convention: :stdcall
20
+ ) { |hwnd, uMsg, wParam, lParam|
21
+ begin
22
+ result = case uMsg
23
+ when WM_CREATE
24
+ OnCreate(hwnd, CREATESTRUCT.new(FFI::Pointer.new(lParam)))
25
+ when WM_DESTROY
26
+ OnDestroy(hwnd)
27
+ end
28
+
29
+ result || DefWindowProc(hwnd, uMsg, wParam, lParam)
30
+ rescue SystemExit => ex
31
+ PostQuitMessage(ex.status)
32
+ rescue
33
+ case MessageBox(hwnd,
34
+ L(Util.FormatException($!)),
35
+ APPNAME,
36
+ MB_ABORTRETRYIGNORE | MB_ICONERROR
37
+ )
38
+ when IDABORT
39
+ PostQuitMessage(2)
40
+ when IDRETRY
41
+ retry
42
+ end
43
+ end
44
+ }
45
+
46
+ def WinMain
47
+ WNDCLASSEX.new { |wc|
48
+ wc[:cbSize] = wc.size
49
+ wc[:lpfnWndProc] = WindowProc
50
+ wc[:hInstance] = GetModuleHandle(nil)
51
+ wc[:hIcon] = LoadIcon(nil, IDI_APPLICATION)
52
+ wc[:hCursor] = LoadCursor(nil, IDC_ARROW)
53
+ wc[:hbrBackground] = FFI::Pointer.new(COLOR_WINDOW + 1)
54
+
55
+ PWSTR(APPNAME) { |className|
56
+ wc[:lpszClassName] = className
57
+
58
+ DetonateLastError(0, :RegisterClassEx,
59
+ wc
60
+ )
61
+ }
62
+ }
63
+
64
+ hwnd = CreateWindowEx(
65
+ WS_EX_LAYERED, APPNAME, APPNAME, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
66
+ CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
67
+ nil, nil, GetModuleHandle(nil), nil
68
+ )
69
+
70
+ raise "CreateWindowEx failed (last error: #{GetLastError()})" if
71
+ hwnd.null? && GetLastError() != 0
72
+
73
+ exit(0) if hwnd.null?
74
+
75
+ ShowWindow(hwnd, SW_SHOWNORMAL)
76
+ UpdateWindow(hwnd)
77
+
78
+ MSG.new { |msg|
79
+ until DetonateLastError(-1, :GetMessage,
80
+ msg, nil, 0, 0
81
+ ) == 0
82
+ TranslateMessage(msg)
83
+ DispatchMessage(msg)
84
+ end
85
+
86
+ exit(msg[:wParam])
87
+ }
88
+ rescue
89
+ MessageBox(hwnd,
90
+ L(Util.FormatException($!)),
91
+ APPNAME,
92
+ MB_ICONERROR
93
+ ); exit(1)
94
+ end
95
+
96
+ WinMain()
@@ -0,0 +1,123 @@
1
+ require 'windows_gui'
2
+
3
+ include WindowsGUI
4
+
5
+ def OnCreate(hwnd,
6
+ cs
7
+ )
8
+ answer = MessageBox(nil,
9
+ L('Create?'),
10
+ cs[:lpszName],
11
+ MB_YESNO | MB_ICONQUESTION
12
+ )
13
+
14
+ return -1 if answer == IDNO
15
+
16
+ 0
17
+ end
18
+
19
+ def OnClose(hwnd)
20
+ answer = MessageBox(hwnd,
21
+ L('Close?'),
22
+ APPNAME,
23
+ MB_YESNO | MB_ICONQUESTION |
24
+ MB_DEFBUTTON2
25
+ )
26
+
27
+ DestroyWindow(hwnd) if answer == IDYES
28
+
29
+ 0
30
+ end
31
+
32
+ def OnDestroy(hwnd)
33
+ MessageBox(nil,
34
+ L(__method__.to_s),
35
+ APPNAME,
36
+ MB_ICONINFORMATION
37
+ )
38
+
39
+ PostQuitMessage(0); 0
40
+ end
41
+
42
+ WindowProc = FFI::Function.new(:long,
43
+ [:pointer, :uint, :uint, :long],
44
+ convention: :stdcall
45
+ ) { |hwnd, uMsg, wParam, lParam|
46
+ begin
47
+ result = case uMsg
48
+ when WM_CREATE
49
+ OnCreate(hwnd, CREATESTRUCT.new(FFI::Pointer.new(lParam)))
50
+ when WM_CLOSE
51
+ OnClose(hwnd)
52
+ when WM_DESTROY
53
+ OnDestroy(hwnd)
54
+ end
55
+
56
+ result || DefWindowProc(hwnd, uMsg, wParam, lParam)
57
+ rescue SystemExit => ex
58
+ PostQuitMessage(ex.status)
59
+ rescue
60
+ case MessageBox(hwnd,
61
+ L(Util.FormatException($!)),
62
+ APPNAME,
63
+ MB_ABORTRETRYIGNORE | MB_ICONERROR
64
+ )
65
+ when IDABORT
66
+ PostQuitMessage(2)
67
+ when IDRETRY
68
+ retry
69
+ end
70
+ end
71
+ }
72
+
73
+ def WinMain
74
+ WNDCLASSEX.new { |wc|
75
+ wc[:cbSize] = wc.size
76
+ wc[:lpfnWndProc] = WindowProc
77
+ wc[:hInstance] = GetModuleHandle(nil)
78
+ wc[:hIcon] = LoadIcon(nil, IDI_APPLICATION)
79
+ wc[:hCursor] = LoadCursor(nil, IDC_ARROW)
80
+ wc[:hbrBackground] = FFI::Pointer.new(COLOR_WINDOW + 1)
81
+
82
+ PWSTR(APPNAME) { |className|
83
+ wc[:lpszClassName] = className
84
+
85
+ DetonateLastError(0, :RegisterClassEx,
86
+ wc
87
+ )
88
+ }
89
+ }
90
+
91
+ hwnd = CreateWindowEx(
92
+ 0, APPNAME, APPNAME, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
93
+ CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
94
+ nil, nil, GetModuleHandle(nil), nil
95
+ )
96
+
97
+ raise "CreateWindowEx failed (last error: #{GetLastError()})" if
98
+ hwnd.null? && GetLastError() != 0
99
+
100
+ exit(0) if hwnd.null?
101
+
102
+ ShowWindow(hwnd, SW_SHOWNORMAL)
103
+ UpdateWindow(hwnd)
104
+
105
+ MSG.new { |msg|
106
+ until DetonateLastError(-1, :GetMessage,
107
+ msg, nil, 0, 0
108
+ ) == 0
109
+ TranslateMessage(msg)
110
+ DispatchMessage(msg)
111
+ end
112
+
113
+ exit(msg[:wParam])
114
+ }
115
+ rescue
116
+ MessageBox(hwnd,
117
+ L(Util.FormatException($!)),
118
+ APPNAME,
119
+ MB_ICONERROR
120
+ ); exit(1)
121
+ end
122
+
123
+ WinMain()