-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathwindow.go
183 lines (138 loc) · 3.51 KB
/
window.go
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
package xcgui
import (
"errors"
"fmt"
)
import (
"github.com/codyguo/xcgui/xc"
)
var (
hwnd2WindowBase = make(map[xc.HWND]*WindowBase)
)
type Window interface {
AsWindowBase() *WindowBase
Handle() xc.HWND
Show() error
WndProc(hwnd xc.HWND, msg uint32, wParam, lParam uintptr) uintptr
}
type WindowBase struct {
window Window
hWnd xc.HWND
hEle xc.HELE
name string
hWindow xc.HWINDOW
}
// WindowBase simply returns the receiver.
func (wb *WindowBase) AsWindowBase() *WindowBase {
return wb
}
func InitWindow(window, parent Window, width, height int, title string, style xc.Xc_window_style_) error {
wb := window.AsWindowBase()
wb.window = window
var hwndParent xc.HWND
if parent != nil {
hwndParent = parent.Handle()
}
if wb.hWnd != 0 {
return lastError("XWndCreate")
}
wb.hWindow = xc.XWnd_Create(
xc.CW_USEDEFAULT,
xc.CW_USEDEFAULT,
width,
height,
title,
// xc.StringToUTF16Ptr(title),
hwndParent,
xc.Xc_window_style_(style))
xc.CloseBtn(wb.hWindow)
if wb.hWindow == 0 {
return lastError("XWndCreate")
}
succeeded := false
go func() {
if !succeeded {
newErrorNoPanic("XWndCreate")
}
}()
succeeded = true
return nil
}
func (wb *WindowBase) Handle() xc.HWND {
wb.hWnd = xc.XWnd_GetHWND(wb.hWindow)
return wb.hWnd
}
func (wb *WindowBase) SetMinimumSize(min Size) error {
if min.Width < 0 || min.Height < 0 {
return newError("min must be positive")
}
xc.XWnd_SetMinimumSize(wb.hWindow, min.Width, min.Height)
return nil
}
// func NewMainWindow(width, height int, title string) *WindowBase {
// wb := new(WindowBase)
// wb.hWindow = xc.XWnd_Create(0, 0, width, height, title, 0, int(xc.XC_WINDOW_STYLE_DEFAULT))
// xc.CloseBtn(wb.hWindow)
// // 透明
// xc.XWnd_SetTransparentType(wb.hWindow, xc.WINDOW_TRANSPARENT_SIMPLE)
// // xc.XWnd_EnableDragBorder(wb.hWindow, xc.BoolToBOOL(false))
// // xc.XCDebugToFileInfo("2015")
// return wb
// }
func (wb *WindowBase) Show() (err error) {
ret := xc.XWnd_ShowWindow(wb.hWindow, xc.SW_RESTORE)
if !ret {
return errors.New("XWndShowWindow call filed.")
}
return nil
}
func (wb *WindowBase) GetHWindow() xc.HWINDOW {
return wb.hWindow
}
// func (wb *WindowBase) GetHWND() xc.HWND {
// return xc.HWND(mw.Hwnd)
// }
func (wb *WindowBase) Run() int {
// var msg xc.MSG
// for wb.hWindow != 0 {
// switch xc.GetMessage(&msg, 0, 0, 0) {
// case 0:
// return int(msg.WParam)
// case -1:
// return -1
// }
// xc.TranslateMessage(&msg)
// xc.DispatchMessage(&msg)
// }
xc.XRunXCGUI()
return 0
}
func (wb *WindowBase) Close() {
wb.SendMessage(xc.WM_CLOSE, 0, 0)
xc.XExitXCGUI()
}
// SendMessage sends a message to the window and returns the result.
func (wb *WindowBase) SendMessage(msg uint32, wParam, lParam uintptr) uintptr {
return xc.SendMessage(wb.hWnd, msg, wParam, lParam)
}
func windowFromHandle(hwnd xc.HWND) Window {
if wb := hwnd2WindowBase[hwnd]; wb != nil {
return wb.window
}
return nil
}
func defaultWndProc(hwnd xc.HWND, msg uint32, wParam, lParam uintptr) (result uintptr) {
wi := windowFromHandle(hwnd)
result = wi.WndProc(hwnd, msg, wParam, lParam)
return
}
func (wb *WindowBase) WndProc(hwnd xc.HWND, msg uint32, wparam, lparam uintptr) uintptr {
fmt.Println("WidgetBase.WndProc")
return uintptr(0)
}
func DebugToFileInfo(str string) {
// 中文转换为Ansi
buffer := make([]byte, len(str)+1)
xc.XC_UnicodeToAnsi(xc.StringToUTF16Ptr(str), len(str), &buffer[0], len(str)*2)
xc.XC_DebugToFileInfo(&buffer[0])
}