123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- using Newtonsoft.Json;
- using ScreenExtender.Models;
- using ScreenExtender.Utility;
- using ScreenExtender.Utility.Spy;
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Windows.Forms;
- namespace ScreenExtender
- {
- public partial class ScreenExtenderMainForm : Form
- {
- private static string MainWindowSizeJsonFilePath => Application.ExecutablePath + ".MainWindowSize.json";
- public byte[] FrameBytes;
- private Bitmap _frameBitmap;
- private Point _origPoint;
- private Point? _mouseDownPoint;
- private ScreenExtenderServer _server;
- public ScreenExtenderMainForm()
- {
- InitializeComponent();
- }
- /////////////////////////////////////////////
- private void UpdateStatus()
- {
- var vw = PreviewPictureBox.ClientSize;
- StatusLabel.Text = $"ViewSize:{vw.Width} x {vw.Height}";
- //TODO: Long Connection Count
- }
- private void UpdateListView()
- {
- foreach (ListViewItem item in CaptureListView.Items)
- {
- UpdateListViewItem(item);
- }
- }
- private void UpdateListViewItem(ListViewItem lvi)
- {
- var model = (WindowModel)lvi.Tag;
- lvi.Text = model.Window.Caption;
- if (lvi.SubItems.Count == 1) lvi.SubItems.Add(model.Location.ToCsvString());
- else lvi.SubItems[1].Text = model.Location.ToCsvString();
- }
- private void UpdateFrame()
- {
- var bmp = _frameBitmap;
- if (bmp == null) return;
- using var graphics = Graphics.FromImage(bmp);
- graphics.Clear(Color.Black);
- var models = CaptureListView.Items.Cast<ListViewItem>().Select(p => (WindowModel)p.Tag).ToArray();
- foreach (var item in models)
- {
- item.Window.PrintTo(graphics, item.Location);
- }
- graphics.Dispose();
- using var ms = new MemoryStream();
- bmp.Save(ms, ImageFormat.Png);
- FrameBytes = ms.ToArray();
- PreviewPictureBox.Invalidate();
- }
- ///////////////////////////////////////////////////////////
- private void ScreenExtenderMainForm_Shown(object sender, EventArgs e)
- {
- PreviewPictureBox_Resize(null, null);
- _server = new ScreenExtenderServer(this);
- if (ScreenExtenderProgram.Args.Contains("listen")) ListenCheckButton.Checked = true;
- if (ScreenExtenderProgram.Args.Contains("load"))
- {
- var items = SaveLoadManager.Load();
- if (items != null && items.Length != 0)
- {
- foreach (var model in items)
- {
- var wnd = SpiedWindow.FindByCaption(model.WindowTitle);
- if (wnd == null) continue;
- var lvi = CaptureListView.Items.Add(new ListViewItem { Tag = new WindowModel { Location = model.Location, Window = wnd } });
- UpdateListViewItem(lvi);
- }
- }
- }
- if (File.Exists(MainWindowSizeJsonFilePath))
- {
- var json = File.ReadAllText(MainWindowSizeJsonFilePath);
- Size = JsonConvert.DeserializeObject<Size>(json);
- }
- }
- private void ScreenExtenderMainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- WindowState = FormWindowState.Normal;
- }
- private void ScreenExtenderMainForm_FormClosed(object sender, FormClosedEventArgs e)
- {
- var json = JsonConvert.SerializeObject(Size);
- File.WriteAllText(MainWindowSizeJsonFilePath, json);
- }
- private void ListenCheckButton_CheckedChanged(object sender, EventArgs e)
- {
- if (ListenCheckButton.Checked) _server.Start(IPAddress.Parse(ListenTextBox.Text), (int)PortNumericUpDown.Value);
- else _server.Stop();
- }
- private void HvCheckBox_CheckedChanged(object sender, EventArgs e)
- {
- MainSplitContainer.Orientation = HvCheckBox.Checked
- ? Orientation.Horizontal
- : Orientation.Vertical
- ;
- }
- private void AddButton_Click(object sender, EventArgs e)
- {
- new SpyAgent(f =>
- {
- var model = new WindowModel
- {
- Window = f
- };
- var lvi = new ListViewItem { Tag = model };
- CaptureListView.Items.Add(lvi);
- CaptureListView.SelectedIndices.Clear();
- UpdateListViewItem(lvi);
- UpdateListView();
- lvi.Selected = true;
- });
- }
- private void RemoveButton_Click(object sender, EventArgs e)
- {
- var selectedItem = CaptureListView.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
- selectedItem?.Remove();
- }
- private void UpdateTimer_Tick(object sender, EventArgs e)
- {
- UpdateFrame();
- }
- private void PreviewPictureBox_Resize(object sender, EventArgs e)
- {
- var old = _frameBitmap;
- var sz = PreviewPictureBox.ClientSize;
- _frameBitmap = new Bitmap(sz.Width, sz.Height, PixelFormat.Format32bppRgb);
- old?.Dispose();
- UpdateStatus();
- }
- private void PreviewPictureBox_Paint(object sender, PaintEventArgs e)
- {
- var bmp = _frameBitmap;
- if (bmp != null) e.Graphics.DrawImage(bmp, Point.Empty);
- }
- private void PreviewPictureBox_MouseDown(object sender, MouseEventArgs e)
- {
- var selectedModel = (WindowModel)CaptureListView.SelectedItems.Cast<ListViewItem>().FirstOrDefault()?.Tag;
- if (selectedModel == null) return;
- _origPoint = selectedModel.Location;
- _mouseDownPoint = MousePosition;
- }
- private void PreviewPictureBox_MouseMove(object sender, MouseEventArgs e)
- {
- if (_mouseDownPoint.HasValue)
- {
- var selectedItem = CaptureListView.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
- if (selectedItem == null) return;
- var model = (WindowModel)selectedItem.Tag;
- var mdp = _mouseDownPoint.Value;
- var mmp = MousePosition;
- var mx = mmp.X - mdp.X;
- var my = mmp.Y - mdp.Y;
- var fx = _origPoint.X + mx;
- var fy = _origPoint.Y + my;
- model.Location = new Point(fx, fy);
- UpdateListViewItem(selectedItem);
- UpdateFrame();
- }
- }
- private void PreviewPictureBox_MouseUp(object sender, MouseEventArgs e)
- {
- _mouseDownPoint = null;
- }
- private void SaveButton_Click(object sender, EventArgs e)
- {
- var items = CaptureListView.Items
- .Cast<ListViewItem>()
- .Select(p => (WindowModel)p.Tag)
- .Where(p => string.IsNullOrWhiteSpace(p.Window.Caption) == false)
- .Select(p => new SaveLoadModel { Location = p.Location, WindowTitle = p.Window.Caption })
- .ToArray();
- SaveLoadManager.Save(items);
- MessageBox.Show("Saved");
- }
- private void LoadButton_Click(object sender, EventArgs e)
- {
- var items = SaveLoadManager.Load();
- if (items == null || items.Length == 0)
- {
- MessageBox.Show("No data to load");
- return;
- }
- foreach (var model in items)
- {
- var wnd = SpiedWindow.FindByCaption(model.WindowTitle);
- if (wnd == null) continue;
- var lvi = CaptureListView.Items.Add(new ListViewItem { Tag = new WindowModel { Location = model.Location, Window = wnd } });
- UpdateListViewItem(lvi);
- }
- }
- }
- }
|