diff --git a/container.go b/container.go index 40fec22e..ccfaf227 100644 --- a/container.go +++ b/container.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build windows // +build windows package walk @@ -195,12 +196,8 @@ func (cb *ContainerBase) RestoreState() error { }) } -func (cb *ContainerBase) doPaint() error { - var ps win.PAINTSTRUCT - - hdc := win.BeginPaint(cb.hWnd, &ps) - defer win.EndPaint(cb.hWnd, &ps) - +func (cb *ContainerBase) doPaint(ps *win.PAINTSTRUCT) error { + hdc := ps.Hdc canvas, err := newCanvasFromHDC(hdc) if err != nil { return err @@ -281,9 +278,26 @@ func (cb *ContainerBase) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintp break } - // If it fails, what can we do about it? Panic? That's extreme. So just ignore it. - _ = cb.doPaint() + var ps win.PAINTSTRUCT + if hdc := win.BeginPaint(cb.hWnd, &ps); hdc == 0 { + break + } + defer win.EndPaint(cb.hWnd, &ps) + + cb.doPaint(&ps) + return 0 + + case win.WM_PRINTCLIENT: + if FocusEffect == nil && InteractionEffect == nil && ValidationErrorEffect == nil { + break + } + ps := win.PAINTSTRUCT{ + Hdc: win.HDC(wParam), + } + if win.GetClientRect(cb.hWnd, &ps.RcPaint) { + cb.doPaint(&ps) + } return 0 case win.WM_COMMAND: diff --git a/customwidget.go b/customwidget.go index 3b0ee00f..d180431f 100644 --- a/customwidget.go +++ b/customwidget.go @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +//go:build windows // +build windows package walk @@ -110,6 +111,26 @@ func (cw *CustomWidget) SetPaintMode(value PaintMode) { cw.paintMode = value } +func (cw *CustomWidget) doPaint(ps *win.PAINTSTRUCT) error { + hdc := ps.Hdc + canvas, err := newCanvasFromHDC(hdc) + if err != nil { + return newError("newCanvasFromHDC failed") + } + defer canvas.Dispose() + + bounds := rectangleFromRECT(ps.RcPaint) + if cw.paintMode == PaintBuffered { + err = cw.bufferedPaint(canvas, bounds) + } else if cw.paintPixels != nil { + err = cw.paintPixels(canvas, bounds) + } else { + err = cw.paint(canvas, RectangleTo96DPI(bounds, cw.DPI())) + } + + return err +} + func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr { switch msg { case win.WM_PAINT: @@ -119,44 +140,12 @@ func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintpt } var ps win.PAINTSTRUCT - - var hdc win.HDC - if wParam == 0 { - hdc = win.BeginPaint(cw.hWnd, &ps) - } else { - hdc = win.HDC(wParam) - } - if hdc == 0 { + if hdc := win.BeginPaint(cw.hWnd, &ps); hdc == 0 { newError("BeginPaint failed") break } - defer func() { - if wParam == 0 { - win.EndPaint(cw.hWnd, &ps) - } - }() - - canvas, err := newCanvasFromHDC(hdc) - if err != nil { - newError("newCanvasFromHDC failed") - break - } - defer canvas.Dispose() - - bounds := rectangleFromRECT(ps.RcPaint) - if cw.paintMode == PaintBuffered { - err = cw.bufferedPaint(canvas, bounds) - } else if cw.paintPixels != nil { - err = cw.paintPixels(canvas, bounds) - } else { - err = cw.paint(canvas, RectangleTo96DPI(bounds, cw.DPI())) - } - - if err != nil { - newError("paint failed") - break - } - + defer win.EndPaint(cw.hWnd, &ps) + cw.doPaint(&ps) return 0 case win.WM_ERASEBKGND: @@ -165,7 +154,18 @@ func (cw *CustomWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintpt } case win.WM_PRINTCLIENT: - win.SendMessage(hwnd, win.WM_PAINT, wParam, lParam) + if cw.paint == nil && cw.paintPixels == nil { + newError("paint(Pixels) func is nil") + break + } + + ps := win.PAINTSTRUCT{ + Hdc: win.HDC(wParam), + } + if win.GetClientRect(cw.hWnd, &ps.RcPaint) { + cw.doPaint(&ps) + } + return 0 case win.WM_WINDOWPOSCHANGED: wp := (*win.WINDOWPOS)(unsafe.Pointer(lParam))