ScreenExtenderMainForm.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using Newtonsoft.Json;
  2. using ScreenExtender.Models;
  3. using ScreenExtender.Utility;
  4. using ScreenExtender.Utility.Spy;
  5. using System;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Windows.Forms;
  12. namespace ScreenExtender
  13. {
  14. public partial class ScreenExtenderMainForm : Form
  15. {
  16. private static string MainWindowSizeJsonFilePath => Application.ExecutablePath + ".MainWindowSize.json";
  17. public byte[] FrameBytes;
  18. private Bitmap _frameBitmap;
  19. private Point _origPoint;
  20. private Point? _mouseDownPoint;
  21. private ScreenExtenderServer _server;
  22. public ScreenExtenderMainForm()
  23. {
  24. InitializeComponent();
  25. }
  26. /////////////////////////////////////////////
  27. private void UpdateStatus()
  28. {
  29. var vw = PreviewPictureBox.ClientSize;
  30. StatusLabel.Text = $"ViewSize:{vw.Width} x {vw.Height}";
  31. //TODO: Long Connection Count
  32. }
  33. private void UpdateListView()
  34. {
  35. foreach (ListViewItem item in CaptureListView.Items)
  36. {
  37. UpdateListViewItem(item);
  38. }
  39. }
  40. private void UpdateListViewItem(ListViewItem lvi)
  41. {
  42. var model = (WindowModel)lvi.Tag;
  43. lvi.Text = model.Window.Caption;
  44. if (lvi.SubItems.Count == 1) lvi.SubItems.Add(model.Location.ToCsvString());
  45. else lvi.SubItems[1].Text = model.Location.ToCsvString();
  46. }
  47. private void UpdateFrame()
  48. {
  49. var bmp = _frameBitmap;
  50. if (bmp == null) return;
  51. using var graphics = Graphics.FromImage(bmp);
  52. graphics.Clear(Color.Black);
  53. var models = CaptureListView.Items.Cast<ListViewItem>().Select(p => (WindowModel)p.Tag).ToArray();
  54. foreach (var item in models)
  55. {
  56. item.Window.PrintTo(graphics, item.Location);
  57. }
  58. graphics.Dispose();
  59. using var ms = new MemoryStream();
  60. bmp.Save(ms, ImageFormat.Png);
  61. FrameBytes = ms.ToArray();
  62. PreviewPictureBox.Invalidate();
  63. }
  64. ///////////////////////////////////////////////////////////
  65. private void ScreenExtenderMainForm_Shown(object sender, EventArgs e)
  66. {
  67. PreviewPictureBox_Resize(null, null);
  68. _server = new ScreenExtenderServer(this);
  69. if (ScreenExtenderProgram.Args.Contains("listen")) ListenCheckButton.Checked = true;
  70. if (ScreenExtenderProgram.Args.Contains("load"))
  71. {
  72. var items = SaveLoadManager.Load();
  73. if (items != null && items.Length != 0)
  74. {
  75. foreach (var model in items)
  76. {
  77. var wnd = SpiedWindow.FindByCaption(model.WindowTitle);
  78. if (wnd == null) continue;
  79. var lvi = CaptureListView.Items.Add(new ListViewItem { Tag = new WindowModel { Location = model.Location, Window = wnd } });
  80. UpdateListViewItem(lvi);
  81. }
  82. }
  83. }
  84. if (File.Exists(MainWindowSizeJsonFilePath))
  85. {
  86. var json = File.ReadAllText(MainWindowSizeJsonFilePath);
  87. Size = JsonConvert.DeserializeObject<Size>(json);
  88. }
  89. }
  90. private void ScreenExtenderMainForm_FormClosing(object sender, FormClosingEventArgs e)
  91. {
  92. WindowState = FormWindowState.Normal;
  93. }
  94. private void ScreenExtenderMainForm_FormClosed(object sender, FormClosedEventArgs e)
  95. {
  96. var json = JsonConvert.SerializeObject(Size);
  97. File.WriteAllText(MainWindowSizeJsonFilePath, json);
  98. }
  99. private void ListenCheckButton_CheckedChanged(object sender, EventArgs e)
  100. {
  101. if (ListenCheckButton.Checked) _server.Start(IPAddress.Parse(ListenTextBox.Text), (int)PortNumericUpDown.Value);
  102. else _server.Stop();
  103. }
  104. private void HvCheckBox_CheckedChanged(object sender, EventArgs e)
  105. {
  106. MainSplitContainer.Orientation = HvCheckBox.Checked
  107. ? Orientation.Horizontal
  108. : Orientation.Vertical
  109. ;
  110. }
  111. private void AddButton_Click(object sender, EventArgs e)
  112. {
  113. new SpyAgent(f =>
  114. {
  115. var model = new WindowModel
  116. {
  117. Window = f
  118. };
  119. var lvi = new ListViewItem { Tag = model };
  120. CaptureListView.Items.Add(lvi);
  121. CaptureListView.SelectedIndices.Clear();
  122. UpdateListViewItem(lvi);
  123. UpdateListView();
  124. lvi.Selected = true;
  125. });
  126. }
  127. private void RemoveButton_Click(object sender, EventArgs e)
  128. {
  129. var selectedItem = CaptureListView.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
  130. selectedItem?.Remove();
  131. }
  132. private void UpdateTimer_Tick(object sender, EventArgs e)
  133. {
  134. UpdateFrame();
  135. }
  136. private void PreviewPictureBox_Resize(object sender, EventArgs e)
  137. {
  138. var old = _frameBitmap;
  139. var sz = PreviewPictureBox.ClientSize;
  140. _frameBitmap = new Bitmap(sz.Width, sz.Height, PixelFormat.Format32bppRgb);
  141. old?.Dispose();
  142. UpdateStatus();
  143. }
  144. private void PreviewPictureBox_Paint(object sender, PaintEventArgs e)
  145. {
  146. var bmp = _frameBitmap;
  147. if (bmp != null) e.Graphics.DrawImage(bmp, Point.Empty);
  148. }
  149. private void PreviewPictureBox_MouseDown(object sender, MouseEventArgs e)
  150. {
  151. var selectedModel = (WindowModel)CaptureListView.SelectedItems.Cast<ListViewItem>().FirstOrDefault()?.Tag;
  152. if (selectedModel == null) return;
  153. _origPoint = selectedModel.Location;
  154. _mouseDownPoint = MousePosition;
  155. }
  156. private void PreviewPictureBox_MouseMove(object sender, MouseEventArgs e)
  157. {
  158. if (_mouseDownPoint.HasValue)
  159. {
  160. var selectedItem = CaptureListView.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
  161. if (selectedItem == null) return;
  162. var model = (WindowModel)selectedItem.Tag;
  163. var mdp = _mouseDownPoint.Value;
  164. var mmp = MousePosition;
  165. var mx = mmp.X - mdp.X;
  166. var my = mmp.Y - mdp.Y;
  167. var fx = _origPoint.X + mx;
  168. var fy = _origPoint.Y + my;
  169. model.Location = new Point(fx, fy);
  170. UpdateListViewItem(selectedItem);
  171. UpdateFrame();
  172. }
  173. }
  174. private void PreviewPictureBox_MouseUp(object sender, MouseEventArgs e)
  175. {
  176. _mouseDownPoint = null;
  177. }
  178. private void SaveButton_Click(object sender, EventArgs e)
  179. {
  180. var items = CaptureListView.Items
  181. .Cast<ListViewItem>()
  182. .Select(p => (WindowModel)p.Tag)
  183. .Where(p => string.IsNullOrWhiteSpace(p.Window.Caption) == false)
  184. .Select(p => new SaveLoadModel { Location = p.Location, WindowTitle = p.Window.Caption })
  185. .ToArray();
  186. SaveLoadManager.Save(items);
  187. MessageBox.Show("Saved");
  188. }
  189. private void LoadButton_Click(object sender, EventArgs e)
  190. {
  191. var items = SaveLoadManager.Load();
  192. if (items == null || items.Length == 0)
  193. {
  194. MessageBox.Show("No data to load");
  195. return;
  196. }
  197. foreach (var model in items)
  198. {
  199. var wnd = SpiedWindow.FindByCaption(model.WindowTitle);
  200. if (wnd == null) continue;
  201. var lvi = CaptureListView.Items.Add(new ListViewItem { Tag = new WindowModel { Location = model.Location, Window = wnd } });
  202. UpdateListViewItem(lvi);
  203. }
  204. }
  205. }
  206. }