1+ // Contains code from: https://stackoverflow.com/questions/5028598/hosting-external-app-in-wpf-window
2+
3+ using System . ComponentModel ;
4+ using System . Runtime . CompilerServices ;
5+ using System . Windows ;
6+ using System . Windows . Controls ;
7+ using System ;
8+ using System . Windows . Threading ;
9+ using System . Diagnostics ;
10+ using NETworkManager . Utils ;
11+ using NETworkManager . Models . PuTTY ;
12+
13+ namespace NETworkManager . Controls
14+ {
15+ public partial class PuTTYControl : UserControl , INotifyPropertyChanged
16+ {
17+ #region PropertyChangedEventHandler
18+ public event PropertyChangedEventHandler PropertyChanged ;
19+
20+ protected virtual void OnPropertyChanged ( [ CallerMemberName ] string propertyName = null )
21+ {
22+ PropertyChanged ? . Invoke ( this , new PropertyChangedEventArgs ( propertyName ) ) ;
23+ }
24+ #endregion
25+
26+ #region Variables
27+ private bool _initialized = false ;
28+
29+ private PuTTYSessionInfo _puTTYSessionInfo ;
30+
31+ Process PuTTYProcess = null ;
32+ IntPtr AppWin ;
33+
34+ DispatcherTimer resizeTimer = new DispatcherTimer ( ) ;
35+ #endregion
36+
37+ #region Constructor, load
38+ public PuTTYControl ( PuTTYSessionInfo info )
39+ {
40+ InitializeComponent ( ) ;
41+ DataContext = this ;
42+
43+ _puTTYSessionInfo = info ;
44+
45+ resizeTimer . Tick += ResizeTimer_Tick ;
46+ resizeTimer . Interval = new TimeSpan ( 0 , 0 , 0 , 0 , 500 ) ;
47+
48+ Dispatcher . ShutdownStarted += Dispatcher_ShutdownStarted ;
49+ }
50+
51+ private void UserControl_Loaded ( object sender , RoutedEventArgs e )
52+ {
53+ // Connect after the control is drawn and only on the first init
54+ if ( ! _initialized )
55+ {
56+ Connect ( ) ;
57+ _initialized = true ;
58+ }
59+ }
60+
61+ private void Dispatcher_ShutdownStarted ( object sender , EventArgs e )
62+ {
63+ OnClose ( ) ;
64+ }
65+ #endregion
66+
67+ #region ICommands & Actions
68+
69+
70+
71+ #endregion
72+
73+ #region Methods
74+ private void Connect ( )
75+ {
76+ ProcessStartInfo info = new ProcessStartInfo
77+ {
78+ FileName = _puTTYSessionInfo . PuTTYLocation ,
79+ Arguments = string . Format ( "{0}" , _puTTYSessionInfo . Host )
80+ } ;
81+
82+ PuTTYProcess = Process . Start ( info ) ;
83+
84+ PuTTYProcess . WaitForInputIdle ( ) ;
85+
86+ AppWin = PuTTYProcess . MainWindowHandle ;
87+
88+ NativeMethods . SetParent ( AppWin , puTTYHost . Handle ) ;
89+
90+ // Show window before set style and resize
91+ NativeMethods . ShowWindow ( AppWin , NativeMethods . WindowShowStyle . Maximize ) ;
92+
93+ // Remove border etc.
94+ int style = ( int ) NativeMethods . GetWindowLong ( AppWin , NativeMethods . GWL_STYLE ) ;
95+ style &= ~ ( NativeMethods . WS_BORDER | NativeMethods . WS_THICKFRAME ) ;
96+ NativeMethods . SetWindowLongPtr ( AppWin , NativeMethods . GWL_STYLE , new IntPtr ( style ) ) ;
97+
98+ // Resize embedded application & refresh
99+ if ( PuTTYProcess != null )
100+ ResizeEmbeddedPuTTY ( ) ;
101+ }
102+
103+ public void Disconnect ( )
104+ {
105+ if ( PuTTYProcess != null && ! PuTTYProcess . HasExited )
106+ PuTTYProcess . Kill ( ) ;
107+ }
108+
109+ private void ResizeEmbeddedPuTTY ( )
110+ {
111+ try
112+ {
113+ NativeMethods . SetWindowPos ( PuTTYProcess . MainWindowHandle , IntPtr . Zero , 0 , 0 , puTTYHost . ClientSize . Width , puTTYHost . ClientSize . Height , NativeMethods . SWP_NOZORDER | NativeMethods . SWP_NOACTIVATE ) ;
114+ }
115+ catch ( Exception ex )
116+ {
117+ MessageBox . Show ( ex . Message ) ;
118+ }
119+ }
120+
121+ public void CloseTab ( )
122+ {
123+ Disconnect ( ) ;
124+ }
125+
126+ private void OnClose ( )
127+ {
128+ Disconnect ( ) ;
129+ }
130+ #endregion
131+
132+ #region Events
133+ private void PuTTYGrid_SizeChanged ( object sender , SizeChangedEventArgs e )
134+ {
135+ if ( PuTTYProcess != null )
136+ ResizeEmbeddedPuTTY ( ) ;
137+ }
138+
139+ private void ResizeTimer_Tick ( object sender , EventArgs e )
140+ {
141+ resizeTimer . Stop ( ) ;
142+
143+ ResizeEmbeddedPuTTY ( ) ;
144+ }
145+ #endregion
146+ }
147+ }
0 commit comments