1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . ComponentModel ;
4
+ using System . Data ;
5
+ using System . Drawing ;
6
+ using System . Linq ;
7
+ using System . Text ;
8
+ using System . Windows . Forms ;
9
+ using System . Runtime . InteropServices ;
10
+
11
+ namespace ST . Library . UI . NodeEditor
12
+ {
13
+ internal class FrmNodePreviewPanel : Form
14
+ {
15
+ public Color BorderColor { get ; set ; }
16
+ public bool AutoBorderColor { get ; set ; }
17
+
18
+ private bool m_bRight ;
19
+ private Point m_ptHandle ;
20
+ private int m_nHandleSize ;
21
+ private Rectangle m_rect_handle ;
22
+ private Rectangle m_rect_panel ;
23
+ private Rectangle m_rect_exclude ;
24
+ private Region m_region ;
25
+ private Type m_type ;
26
+ private STNode m_node ;
27
+ private STNodeEditor m_editor ;
28
+ private STNodePropertyGrid m_property ;
29
+
30
+ private Pen m_pen = new Pen ( Color . Black ) ;
31
+ private SolidBrush m_brush = new SolidBrush ( Color . Black ) ;
32
+ private static FrmNodePreviewPanel m_last_frm ;
33
+
34
+ [ DllImport ( "user32.dll" ) ]
35
+ private static extern int SetWindowRgn ( IntPtr hWnd , IntPtr hRgn , bool bRedraw ) ;
36
+
37
+ public FrmNodePreviewPanel ( Type stNodeType , Point ptHandle , int nHandleSize , bool bRight , STNodeEditor editor , STNodePropertyGrid propertyGrid ) {
38
+ this . SetStyle ( ControlStyles . UserPaint , true ) ;
39
+ this . SetStyle ( ControlStyles . ResizeRedraw , true ) ;
40
+ this . SetStyle ( ControlStyles . AllPaintingInWmPaint , true ) ;
41
+ this . SetStyle ( ControlStyles . OptimizedDoubleBuffer , true ) ;
42
+ this . SetStyle ( ControlStyles . SupportsTransparentBackColor , true ) ;
43
+
44
+ if ( m_last_frm != null ) m_last_frm . Close ( ) ;
45
+ m_last_frm = this ;
46
+
47
+ m_editor = editor ;
48
+ m_property = propertyGrid ;
49
+ m_editor . Size = new Size ( 200 , 200 ) ;
50
+ m_property . Size = new Size ( 200 , 200 ) ;
51
+ m_editor . Location = new Point ( 1 + ( bRight ? nHandleSize : 0 ) , 1 ) ;
52
+ m_property . Location = new Point ( m_editor . Right , 1 ) ;
53
+ m_property . InfoFirstOnDraw = true ;
54
+ this . Controls . Add ( m_editor ) ;
55
+ this . Controls . Add ( m_property ) ;
56
+ this . ShowInTaskbar = false ;
57
+ this . FormBorderStyle = System . Windows . Forms . FormBorderStyle . None ;
58
+ this . Size = new Size ( 402 + nHandleSize , 202 ) ;
59
+
60
+ m_type = stNodeType ;
61
+ m_ptHandle = ptHandle ;
62
+ m_nHandleSize = nHandleSize ;
63
+ m_bRight = bRight ;
64
+
65
+ this . AutoBorderColor = true ;
66
+ this . BorderColor = Color . DodgerBlue ;
67
+ }
68
+
69
+ protected override void OnLoad ( EventArgs e ) {
70
+ base . OnLoad ( e ) ;
71
+ m_node = ( STNode ) Activator . CreateInstance ( m_type ) ;
72
+ m_node . Left = 20 ; m_node . Top = 20 ;
73
+ m_editor . Nodes . Add ( m_node ) ;
74
+ m_property . SetNode ( m_node ) ;
75
+
76
+ m_rect_panel = new Rectangle ( 0 , 0 , 402 , 202 ) ;
77
+ m_rect_handle = new Rectangle ( m_ptHandle . X , m_ptHandle . Y , m_nHandleSize , m_nHandleSize ) ;
78
+ m_rect_exclude = new Rectangle ( 0 , m_nHandleSize , m_nHandleSize , this . Height - m_nHandleSize ) ;
79
+ if ( m_bRight ) {
80
+ this . Left = m_ptHandle . X ;
81
+ m_rect_panel . X = m_ptHandle . X + m_nHandleSize ;
82
+ } else {
83
+ this . Left = m_ptHandle . X - this . Width + m_nHandleSize ;
84
+ m_rect_exclude . X = this . Width - m_nHandleSize ;
85
+ m_rect_panel . X = this . Left ;
86
+ }
87
+ if ( m_ptHandle . Y + this . Height > Screen . GetWorkingArea ( this ) . Bottom ) {
88
+ this . Top = m_ptHandle . Y - this . Height + m_nHandleSize ;
89
+ m_rect_exclude . Y -= m_nHandleSize ;
90
+ } else this . Top = m_ptHandle . Y ;
91
+ m_rect_panel . Y = this . Top ;
92
+ m_region = new Region ( new Rectangle ( Point . Empty , this . Size ) ) ;
93
+ m_region . Exclude ( m_rect_exclude ) ;
94
+ using ( Graphics g = this . CreateGraphics ( ) ) {
95
+ IntPtr h = m_region . GetHrgn ( g ) ;
96
+ FrmNodePreviewPanel . SetWindowRgn ( this . Handle , h , false ) ;
97
+ m_region . ReleaseHrgn ( h ) ;
98
+ }
99
+
100
+ this . MouseLeave += Event_MouseLeave ;
101
+ m_editor . MouseLeave += Event_MouseLeave ;
102
+ m_property . MouseLeave += Event_MouseLeave ;
103
+ this . BeginInvoke ( new MethodInvoker ( ( ) => {
104
+ m_property . Focus ( ) ;
105
+ } ) ) ;
106
+ }
107
+
108
+ protected override void OnClosing ( CancelEventArgs e ) {
109
+ base . OnClosing ( e ) ;
110
+ this . Controls . Clear ( ) ;
111
+ m_editor . Nodes . Clear ( ) ;
112
+ m_editor . MouseLeave -= Event_MouseLeave ;
113
+ m_property . MouseLeave -= Event_MouseLeave ;
114
+ m_last_frm = null ;
115
+ }
116
+
117
+ void Event_MouseLeave ( object sender , EventArgs e ) {
118
+ Point pt = Control . MousePosition ;
119
+ if ( m_rect_panel . Contains ( pt ) || m_rect_handle . Contains ( pt ) ) return ;
120
+ this . Close ( ) ;
121
+ }
122
+
123
+ protected override void OnPaint ( PaintEventArgs e ) {
124
+ base . OnPaint ( e ) ;
125
+ Graphics g = e . Graphics ;
126
+ m_pen . Color = this . AutoBorderColor ? m_node . TitleColor : this . BorderColor ;
127
+ m_brush . Color = m_pen . Color ;
128
+ g . DrawRectangle ( m_pen , 0 , 0 , this . Width - 1 , this . Height - 1 ) ;
129
+ g . FillRectangle ( m_brush , m_rect_exclude . X - 1 , m_rect_exclude . Y - 1 , m_rect_exclude . Width + 2 , m_rect_exclude . Height + 2 ) ;
130
+
131
+ Rectangle rect = this . RectangleToClient ( m_rect_handle ) ;
132
+ rect . Y = ( m_nHandleSize - 14 ) / 2 ;
133
+ rect . X += rect . Y + 1 ;
134
+ rect . Width = rect . Height = 14 ;
135
+ m_pen . Width = 2 ;
136
+ g . DrawLine ( m_pen , rect . X + 4 , rect . Y + 3 , rect . X + 10 , rect . Y + 3 ) ;
137
+ g . DrawLine ( m_pen , rect . X + 4 , rect . Y + 6 , rect . X + 10 , rect . Y + 6 ) ;
138
+ g . DrawLine ( m_pen , rect . X + 4 , rect . Y + 11 , rect . X + 10 , rect . Y + 11 ) ;
139
+ g . DrawLine ( m_pen , rect . X + 7 , rect . Y + 7 , rect . X + 7 , rect . Y + 10 ) ;
140
+ m_pen . Width = 1 ;
141
+ g . DrawRectangle ( m_pen , rect . X , rect . Y , rect . Width - 1 , rect . Height - 1 ) ;
142
+ }
143
+ }
144
+ }
0 commit comments