2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
5
+ //go:build windows
5
6
// +build windows
6
7
7
8
package walk
@@ -110,6 +111,26 @@ func (cw *CustomWidget) SetPaintMode(value PaintMode) {
110
111
cw .paintMode = value
111
112
}
112
113
114
+ func (cw * CustomWidget ) doPaint (ps * win.PAINTSTRUCT ) error {
115
+ hdc := ps .Hdc
116
+ canvas , err := newCanvasFromHDC (hdc )
117
+ if err != nil {
118
+ return newError ("newCanvasFromHDC failed" )
119
+ }
120
+ defer canvas .Dispose ()
121
+
122
+ bounds := rectangleFromRECT (ps .RcPaint )
123
+ if cw .paintMode == PaintBuffered {
124
+ err = cw .bufferedPaint (canvas , bounds )
125
+ } else if cw .paintPixels != nil {
126
+ err = cw .paintPixels (canvas , bounds )
127
+ } else {
128
+ err = cw .paint (canvas , RectangleTo96DPI (bounds , cw .DPI ()))
129
+ }
130
+
131
+ return err
132
+ }
133
+
113
134
func (cw * CustomWidget ) WndProc (hwnd win.HWND , msg uint32 , wParam , lParam uintptr ) uintptr {
114
135
switch msg {
115
136
case win .WM_PAINT :
@@ -119,44 +140,12 @@ func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintpt
119
140
}
120
141
121
142
var ps win.PAINTSTRUCT
122
-
123
- var hdc win.HDC
124
- if wParam == 0 {
125
- hdc = win .BeginPaint (cw .hWnd , & ps )
126
- } else {
127
- hdc = win .HDC (wParam )
128
- }
129
- if hdc == 0 {
143
+ if hdc := win .BeginPaint (cw .hWnd , & ps ); hdc == 0 {
130
144
newError ("BeginPaint failed" )
131
145
break
132
146
}
133
- defer func () {
134
- if wParam == 0 {
135
- win .EndPaint (cw .hWnd , & ps )
136
- }
137
- }()
138
-
139
- canvas , err := newCanvasFromHDC (hdc )
140
- if err != nil {
141
- newError ("newCanvasFromHDC failed" )
142
- break
143
- }
144
- defer canvas .Dispose ()
145
-
146
- bounds := rectangleFromRECT (ps .RcPaint )
147
- if cw .paintMode == PaintBuffered {
148
- err = cw .bufferedPaint (canvas , bounds )
149
- } else if cw .paintPixels != nil {
150
- err = cw .paintPixels (canvas , bounds )
151
- } else {
152
- err = cw .paint (canvas , RectangleTo96DPI (bounds , cw .DPI ()))
153
- }
154
-
155
- if err != nil {
156
- newError ("paint failed" )
157
- break
158
- }
159
-
147
+ defer win .EndPaint (cw .hWnd , & ps )
148
+ cw .doPaint (& ps )
160
149
return 0
161
150
162
151
case win .WM_ERASEBKGND :
@@ -165,7 +154,18 @@ func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintpt
165
154
}
166
155
167
156
case win .WM_PRINTCLIENT :
168
- win .SendMessage (hwnd , win .WM_PAINT , wParam , lParam )
157
+ if cw .paint == nil && cw .paintPixels == nil {
158
+ newError ("paint(Pixels) func is nil" )
159
+ break
160
+ }
161
+
162
+ ps := win.PAINTSTRUCT {
163
+ Hdc : win .HDC (wParam ),
164
+ }
165
+ if win .GetClientRect (cw .hWnd , & ps .RcPaint ) {
166
+ cw .doPaint (& ps )
167
+ }
168
+ return 0
169
169
170
170
case win .WM_WINDOWPOSCHANGED :
171
171
wp := (* win .WINDOWPOS )(unsafe .Pointer (lParam ))
0 commit comments