Skip to content

Commit 885771e

Browse files
authored
Views and Controls: support background colours
Generate a solid brush when we assign a background colour to a view. This will then be used to erase the background to a solid fill when rendering the view. This is sufficient in some cases but not in others. The application of the null brush should give us a transparent view but seems to not function properly.
1 parent 205ab85 commit 885771e

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Sources/SwiftWin32/Views and Controls/View.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ private let SwiftViewProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSubc
3838

3939
return 0
4040

41+
case UINT(WM_ERASEBKGND):
42+
guard let view = view, let brush = view.hbrBackground else{ break }
43+
44+
let hDC: DeviceContextHandle =
45+
.init(referencing: HDC(bitPattern: UInt(wParam)))
46+
47+
var rc: RECT = RECT()
48+
_ = GetClientRect(view.hWnd, &rc)
49+
_ = FillRect(hDC.value, &rc, brush.value)
50+
51+
return 1
52+
4153
case UINT(WM_COMMAND):
4254
// TODO: handle menu actions
4355
break
@@ -499,8 +511,19 @@ public class View: Responder {
499511

500512
// MARK - Configuring a View's Visual Appearance
501513

514+
internal var hbrBackground: BrushHandle?
515+
502516
/// The view's background color.
503-
public var backgroundColor: Color?
517+
public var backgroundColor: Color? {
518+
didSet {
519+
switch self.backgroundColor {
520+
case .some(let color):
521+
self.hbrBackground = .init(owning: CreateSolidBrush(color.COLORREF))
522+
case .none:
523+
self.hbrBackground = nil
524+
}
525+
}
526+
}
504527

505528
/// A boolean that determines if the view is hidden.
506529
public var isHidden: Bool {

0 commit comments

Comments
 (0)