@@ -21,21 +21,61 @@ private let SwiftLabelProc: SUBCLASSPROC = { (hWnd, uMsg, wParam, lParam, uIdSub
21
21
}
22
22
23
23
public class Label : Control {
24
- private static let `class` : WindowClass = WindowClass ( named: WC_STATIC)
25
- private static let style : WindowStyle = ( base: WS_TABSTOP | DWORD ( SS_NOTIFY) , extended: 0 )
24
+ private static let `class` : WindowClass =
25
+ WindowClass ( hInst: GetModuleHandleW ( nil ) , name: " Swift.Label " )
26
+ private static let style : WindowStyle = ( base: WS_TABSTOP, extended: 0 )
27
+
28
+ private var staticHWnd : HWND !
29
+
30
+ public var text : String ? {
31
+ get {
32
+ let szLength : Int32 = GetWindowTextLengthW ( self . staticHWnd)
33
+ let buffer : [ WCHAR ] = Array < WCHAR > ( unsafeUninitializedCapacity: Int ( szLength) + 1 ) {
34
+ $1 = Int ( GetWindowTextW ( self . staticHWnd, $0. baseAddress!, CInt ( $0. count) ) )
35
+ }
36
+ return String ( decodingCString: buffer, as: UTF16 . self)
37
+ }
38
+ set ( value) { _ = SetWindowTextW ( self . staticHWnd, value? . wide) }
39
+ }
26
40
27
41
public override var font : Font ! {
28
- get { return super. font }
29
- set ( value) { super. font = value }
42
+ didSet {
43
+ SendMessageW ( self . staticHWnd, UINT ( WM_SETFONT) ,
44
+ unsafeBitCast ( self . font? . hFont. value, to: WPARAM . self) ,
45
+ LPARAM ( 1 ) )
46
+ }
30
47
}
31
48
32
- @_Win32WindowText
33
- public var text : String ?
49
+ public override var frame : Rect {
50
+ didSet {
51
+ let rect = GetRect ( hWnd: self . hWnd)
52
+ _ = SetWindowPos ( self . staticHWnd, nil ,
53
+ CInt ( rect. origin. x) , CInt ( rect. origin. y) ,
54
+ CInt ( rect. size. width) , CInt ( rect. size. height) ,
55
+ UINT ( SWP_NOZORDER | SWP_FRAMECHANGED) )
56
+ }
57
+ }
34
58
35
59
public init ( frame: Rect ) {
36
60
super. init ( frame: frame, class: Label . class, style: Label . style)
37
61
_ = SetWindowSubclass ( hWnd, SwiftLabelProc, UINT_PTR ( 1 ) ,
38
62
unsafeBitCast ( self as AnyObject , to: DWORD_PTR . self) )
63
+
64
+ let rect = GetRect ( hWnd: self . hWnd)
65
+ self . staticHWnd = CreateWindowExW ( 0 , WC_STATIC . wide, nil , 0 ,
66
+ 0 , 0 ,
67
+ Int32 ( rect. size. width) ,
68
+ Int32 ( rect. size. height) ,
69
+ nil , nil , GetModuleHandleW ( nil ) , nil ) !
70
+
71
+ _ = SetWindowLongW ( self . staticHWnd, WinSDK . GWL_STYLE, WS_CHILD)
72
+ _ = SetParent ( self . staticHWnd, self . hWnd)
73
+
74
+ self . font = Font . systemFont ( ofSize: Font . systemFontSize)
75
+ }
76
+
77
+ deinit {
78
+ DestroyWindow ( self . staticHWnd)
39
79
}
40
80
41
81
// ContentSizeCategoryAdjusting
0 commit comments