From 58167f32a678032168668a765bdcf05ab710936c Mon Sep 17 00:00:00 2001 From: MaxXor Date: Tue, 28 Jul 2015 23:48:39 +0200 Subject: [PATCH] Small fixes --- Server/Controls/PictureBoxEx.Designer.cs | 59 ------- Server/Controls/PictureBoxEx.cs | 181 -------------------- Server/Controls/PictureBoxEx.resx | 126 -------------- Server/Controls/RapidPictureBox.cs | 195 ++++++++++++++++++++++ Server/Core/Utilities/FrameCounter.cs | 6 +- Server/Forms/FrmRemoteDesktop.Designer.cs | 13 +- Server/Forms/FrmRemoteDesktop.cs | 36 ++-- Server/Server.csproj | 8 +- 8 files changed, 219 insertions(+), 405 deletions(-) delete mode 100644 Server/Controls/PictureBoxEx.Designer.cs delete mode 100644 Server/Controls/PictureBoxEx.cs delete mode 100644 Server/Controls/PictureBoxEx.resx create mode 100644 Server/Controls/RapidPictureBox.cs diff --git a/Server/Controls/PictureBoxEx.Designer.cs b/Server/Controls/PictureBoxEx.Designer.cs deleted file mode 100644 index bc884415..00000000 --- a/Server/Controls/PictureBoxEx.Designer.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace xServer.Controls -{ - partial class PictureBoxEx - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - - // Stop running. - this.Stop(); - } - - base.Dispose(disposing); - } - - #region Component Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.picDesktop = new System.Windows.Forms.PictureBox(); - ((System.ComponentModel.ISupportInitialize)(this.picDesktop)).BeginInit(); - this.SuspendLayout(); - // - // picDesktop - // - this.picDesktop.BackColor = System.Drawing.Color.Black; - this.picDesktop.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.picDesktop.Dock = System.Windows.Forms.DockStyle.Fill; - this.picDesktop.Location = new System.Drawing.Point(0, 0); - this.picDesktop.Name = "picDesktop"; - this.picDesktop.Size = new System.Drawing.Size(797, 501); - this.picDesktop.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; - this.picDesktop.TabIndex = 0; - this.picDesktop.TabStop = false; - ((System.ComponentModel.ISupportInitialize)(this.picDesktop)).EndInit(); - this.ResumeLayout(false); - - } - - #endregion - - private System.Windows.Forms.PictureBox picDesktop; - } -} diff --git a/Server/Controls/PictureBoxEx.cs b/Server/Controls/PictureBoxEx.cs deleted file mode 100644 index 08abb4eb..00000000 --- a/Server/Controls/PictureBoxEx.cs +++ /dev/null @@ -1,181 +0,0 @@ -using System; -using System.Diagnostics; -using System.Drawing; -using System.Windows.Forms; -using xServer.Core.Utilities; - -namespace xServer.Controls -{ - public delegate void PictureSizeChangedEventHandler(int width, int height); - - public class PictureSizeChangedEventArgs : EventArgs - { - public int NewWidth; - public int NewHeight; - - public PictureSizeChangedEventArgs(int width, int height) - { - NewWidth = width; - NewHeight = height; - } - } - - public interface IRapidPictureBox - { - bool Running { get; set; } - - void Start(); - void Stop(); - void UpdateImage(Bitmap bmp, bool cloneBitmap = false); - - Image _Image { get; set; } - } - - /// - /// Custom PictureBox Control designed for rapidly-changing images. - /// - public partial class PictureBoxEx : PictureBox, IRapidPictureBox - { - #region IRapidPictureBox Implementation - - /// - /// True if the PictureBox should be currently streaming images; False if the - /// PictureBox should not be currently streaming images. - /// - public bool Running { get; set; } - - public void Start() - { - _frameCounter = new FrameCounter(); - - _sWatch = Stopwatch.StartNew(); - - Running = true; - } - - public void Stop() - { - if (_sWatch != null) - _sWatch.Stop(); - - Running = false; - } - - public void UpdateImage(Bitmap bmp, bool cloneBitmap = false) - { - try - { - CountFps(); - - if ((bmpWidth != bmp.Width) && (bmpHeight != bmp.Height)) - OnPictureSizeChanged(new PictureSizeChangedEventArgs(bmp.Width, bmp.Height)); - - lock (ImgLocker) - { - if (this._Image != null) - { - this._Image.Dispose(); - this._Image = null; - } - - this._Image = cloneBitmap ? new Bitmap(bmp, picDesktop.Width, picDesktop.Height) /*resize bitmap*/ : bmp; - } - } - catch (InvalidOperationException) - { - } - catch (Exception ex) - { - MessageBox.Show( - string.Format( - "An unexpected error occurred: {0}\n\nPlease report this as fast as possible here:\\https://github.com/MaxXor/xRAT/issues", - ex.Message), "", MessageBoxButtons.OK, MessageBoxIcon.Error); - } - } - - #endregion - - // Fields used to keep track of the remote desktop's size. - public int bmpWidth { get; private set; } - public int bmpHeight { get; private set; } - - // Fields for the FrameCounter. - public FrameCounter _frameCounter; - private Stopwatch _sWatch; - - public PictureBoxEx() - { - InitializeComponent(); - - //this.DoubleBuffered = true; - this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); - - _frameCounter = new FrameCounter(); - } - - #region Events - - public event PictureSizeChangedEventHandler PictureSizeChanged; - - protected virtual void OnPictureSizeChanged(PictureSizeChangedEventArgs e) - { - PictureSizeChangedEventHandler handler = PictureSizeChanged; - if (handler != null) - handler(e.NewWidth, e.NewHeight); - } - - #endregion - - #region Overrides - - protected override CreateParams CreateParams - { - get - { - CreateParams cp = base.CreateParams; - cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED - return cp; - } - } - - protected override void OnPaint(PaintEventArgs pe) - { - lock (ImgLocker) - { - if (this._Image != null) - { - pe.Graphics.DrawImage(this._Image, this.Location); - } - } - } - - #endregion - - private void CountFps() - { - var deltaTime = (float)_sWatch.Elapsed.TotalSeconds; - _sWatch = Stopwatch.StartNew(); - - _frameCounter.Update(deltaTime); - } - - private readonly object ImgLocker = new object(); - /// - /// Provides thread-safe access to the PictureBox's image. - /// - public Image _Image - { - get - { - return picDesktop.Image; - } - set - { - lock (ImgLocker) - { - picDesktop.Image = value; - } - } - } - } -} \ No newline at end of file diff --git a/Server/Controls/PictureBoxEx.resx b/Server/Controls/PictureBoxEx.resx deleted file mode 100644 index 7b18c444..00000000 --- a/Server/Controls/PictureBoxEx.resx +++ /dev/null @@ -1,126 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - - False - - \ No newline at end of file diff --git a/Server/Controls/RapidPictureBox.cs b/Server/Controls/RapidPictureBox.cs new file mode 100644 index 00000000..30354daf --- /dev/null +++ b/Server/Controls/RapidPictureBox.cs @@ -0,0 +1,195 @@ +using System; +using System.Diagnostics; +using System.Drawing; +using System.Windows.Forms; +using xServer.Core.Utilities; + +namespace xServer.Controls +{ + public interface IRapidPictureBox + { + bool Running { get; set; } + Image GetImageSafe { get; set; } + + void Start(); + void Stop(); + void UpdateImage(Bitmap bmp, bool cloneBitmap = false); + } + + /// + /// Custom PictureBox Control designed for rapidly-changing images. + /// + public class RapidPictureBox : PictureBox, IRapidPictureBox + { + /// + /// True if the PictureBox is currently streaming images, else False. + /// + public bool Running { get; set; } + + /// + /// Returns the width of the original screen. + /// + public int ScreenWidth { get; private set; } + + /// + /// Returns the height of the original screen. + /// + public int ScreenHeight { get; private set; } + + /// + /// Provides thread-safe access to the Image of this Picturebox. + /// + public Image GetImageSafe + { + get + { + return Image; + } + set + { + lock (_imageLock) + { + Image = value; + } + } + } + + /// + /// The lock object for the Picturebox's image. + /// + private readonly object _imageLock = new object(); + + /// + /// The Stopwatch for internal FPS measuring. + /// + private Stopwatch _sWatch; + + /// + /// The internal class for FPS measuring. + /// + private FrameCounter _frameCounter; + + /// + /// Subscribes an Eventhandler to the FrameUpdated event. + /// + /// The Eventhandler to set. + public void SetFrameUpdatedEvent(FrameUpdatedEventHandler e) + { + _frameCounter.FrameUpdated += e; + } + + /// + /// Unsubscribes an Eventhandler from the FrameUpdated event. + /// + /// The Eventhandler to remove. + public void UnsetFrameUpdatedEvent(FrameUpdatedEventHandler e) + { + _frameCounter.FrameUpdated -= e; + } + + /// + /// Starts the internal FPS measuring. + /// + public void Start() + { + _frameCounter = new FrameCounter(); + + _sWatch = Stopwatch.StartNew(); + + Running = true; + } + + /// + /// Stops the internal FPS measuring. + /// + public void Stop() + { + if (_sWatch != null) + _sWatch.Stop(); + + Running = false; + } + + /// + /// Updates the Image of this Picturebox. + /// + /// The new bitmap to use. + /// If True the bitmap will be cloned, else it uses the original bitmap. + public void UpdateImage(Bitmap bmp, bool cloneBitmap = false) + { + try + { + CountFps(); + + if ((ScreenWidth != bmp.Width) && (ScreenHeight != bmp.Height)) + UpdateScreenSize(bmp.Width, bmp.Height); + + lock (_imageLock) + { + if (GetImageSafe != null) + { + GetImageSafe.Dispose(); + GetImageSafe = null; + } + + GetImageSafe = cloneBitmap ? new Bitmap(bmp, Width, Height) /*resize bitmap*/ : bmp; + } + } + catch (InvalidOperationException) + { + } + catch (Exception ex) + { + MessageBox.Show( + string.Format( + "An unexpected error occurred: {0}\n\nPlease report this as fast as possible here:\\https://github.com/MaxXor/xRAT/issues", + ex.Message), "", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + /// + /// Constructor, sets Picturebox double-buffered and initializes the Framecounter. + /// + public RapidPictureBox() + { + this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true); + + _frameCounter = new FrameCounter(); + } + + protected override CreateParams CreateParams + { + get + { + CreateParams cp = base.CreateParams; + cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED + return cp; + } + } + + protected override void OnPaint(PaintEventArgs pe) + { + lock (_imageLock) + { + if (GetImageSafe != null) + { + pe.Graphics.DrawImage(GetImageSafe, Location); + } + } + } + + private void UpdateScreenSize(int newWidth, int newHeight) + { + ScreenWidth = newWidth; + ScreenHeight = newHeight; + } + + private void CountFps() + { + var deltaTime = (float)_sWatch.Elapsed.TotalSeconds; + _sWatch = Stopwatch.StartNew(); + + _frameCounter.Update(deltaTime); + } + } +} \ No newline at end of file diff --git a/Server/Core/Utilities/FrameCounter.cs b/Server/Core/Utilities/FrameCounter.cs index 0b13b294..57295f09 100644 --- a/Server/Core/Utilities/FrameCounter.cs +++ b/Server/Core/Utilities/FrameCounter.cs @@ -30,9 +30,9 @@ public class FrameCounter public void Update(float deltaTime) { - float CurrentFramesPerSecond = 1.0f / deltaTime; + float currentFramesPerSecond = 1.0f / deltaTime; - _sampleBuffer.Enqueue(CurrentFramesPerSecond); + _sampleBuffer.Enqueue(currentFramesPerSecond); if (_sampleBuffer.Count > MAXIMUM_SAMPLES) { @@ -41,7 +41,7 @@ public void Update(float deltaTime) } else { - AverageFramesPerSecond = CurrentFramesPerSecond; + AverageFramesPerSecond = currentFramesPerSecond; } OnFrameUpdated(new FrameUpdatedEventArgs(AverageFramesPerSecond)); diff --git a/Server/Forms/FrmRemoteDesktop.Designer.cs b/Server/Forms/FrmRemoteDesktop.Designer.cs index 365c94c9..0f28e5d1 100644 --- a/Server/Forms/FrmRemoteDesktop.Designer.cs +++ b/Server/Forms/FrmRemoteDesktop.Designer.cs @@ -35,7 +35,7 @@ private void InitializeComponent() this.lblQuality = new System.Windows.Forms.Label(); this.lblQualityShow = new System.Windows.Forms.Label(); this.btnMouse = new System.Windows.Forms.Button(); - this.picDesktop = new xServer.Controls.PictureBoxEx(); + this.picDesktop = new xServer.Controls.RapidPictureBox(); this.panelTop = new System.Windows.Forms.Panel(); this.cbMonitors = new System.Windows.Forms.ComboBox(); this.btnHide = new System.Windows.Forms.Button(); @@ -111,9 +111,11 @@ private void InitializeComponent() this.picDesktop.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.picDesktop.Cursor = System.Windows.Forms.Cursors.Default; this.picDesktop.Dock = System.Windows.Forms.DockStyle.Fill; + this.picDesktop.GetImageSafe = null; this.picDesktop.Location = new System.Drawing.Point(0, 0); this.picDesktop.Name = "picDesktop"; - this.picDesktop.Size = new System.Drawing.Size(797, 501); + this.picDesktop.Running = false; + this.picDesktop.Size = new System.Drawing.Size(784, 562); this.picDesktop.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; this.picDesktop.TabIndex = 0; this.picDesktop.TabStop = false; @@ -171,15 +173,14 @@ private void InitializeComponent() // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(797, 501); + this.ClientSize = new System.Drawing.Size(784, 562); this.Controls.Add(this.btnShow); this.Controls.Add(this.panelTop); this.Controls.Add(this.picDesktop); this.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.KeyPreview = true; - this.MinimizeBox = false; - this.MinimumSize = new System.Drawing.Size(570, 421); + this.MinimumSize = new System.Drawing.Size(640, 480); this.Name = "FrmRemoteDesktop"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "xRAT 2.0 - Remote Desktop []"; @@ -205,7 +206,7 @@ private void InitializeComponent() private System.Windows.Forms.Panel panelTop; private System.Windows.Forms.Button btnHide; private System.Windows.Forms.Button btnShow; - private xServer.Controls.PictureBoxEx picDesktop; + private xServer.Controls.RapidPictureBox picDesktop; private System.Windows.Forms.ComboBox cbMonitors; } } \ No newline at end of file diff --git a/Server/Forms/FrmRemoteDesktop.cs b/Server/Forms/FrmRemoteDesktop.cs index 6e92ee7e..94157d03 100644 --- a/Server/Forms/FrmRemoteDesktop.cs +++ b/Server/Forms/FrmRemoteDesktop.cs @@ -13,24 +13,19 @@ namespace xServer.Forms { public partial class FrmRemoteDesktop : Form { - private readonly Client _connectClient; - private bool _enableMouseInput; - private bool _started; - - private int _screenWidth; - private int _screenHeight; - public readonly Queue ProcessingScreensQueue = new Queue(); public readonly object ProcessingScreensLock = new object(); public bool ProcessingScreens; + private readonly Client _connectClient; + private bool _enableMouseInput; + private bool _started; + public FrmRemoteDesktop(Client c) { _connectClient = c; _connectClient.Value.FrmRdp = this; InitializeComponent(); - - picDesktop.PictureSizeChanged += picDesktop_PictureSizeChanged; } private void FrmRemoteDesktop_Load(object sender, EventArgs e) @@ -85,7 +80,7 @@ public void ProcessScreens(object state) { try { - // Update the new image from the packet data. + // update the new image from the packet data picDesktop.UpdateImage(_connectClient.Value.StreamCodec.DecodeData(ms), true); this.Invoke((MethodInvoker)delegate @@ -97,7 +92,8 @@ public void ProcessScreens(object state) }); } catch - { } + { + } } packet.Image = null; @@ -136,12 +132,6 @@ private void _frameCounter_FrameUpdated(FrameUpdatedEventArgs e) }); } - private void picDesktop_PictureSizeChanged(int width, int height) - { - _screenWidth = width; - _screenHeight = height; - } - private void ToggleControls(bool t) { _started = !t; @@ -171,8 +161,8 @@ private void FrmRemoteDesktop_FormClosing(object sender, FormClosingEventArgs e) private void FrmRemoteDesktop_Resize(object sender, EventArgs e) { - panelTop.Left = (this.Width / 2) - (panelTop.Width / 2); - btnShow.Left = (this.Width / 2) - (btnShow.Width / 2); + panelTop.Left = (this.Width/2) - (panelTop.Width/2); + btnShow.Left = (this.Width/2) - (btnShow.Width/2); } private void btnStart_Click(object sender, EventArgs e) @@ -189,7 +179,7 @@ private void btnStart_Click(object sender, EventArgs e) picDesktop.Start(); // Subscribe to the new frame counter. - picDesktop._frameCounter.FrameUpdated += _frameCounter_FrameUpdated; + picDesktop.SetFrameUpdatedEvent(_frameCounter_FrameUpdated); new Core.Packets.ServerPackets.GetDesktop(barQuality.Value, cbMonitors.SelectedIndex, RemoteDesktopAction.Start).Execute(_connectClient); } @@ -202,7 +192,7 @@ private void btnStop_Click(object sender, EventArgs e) picDesktop.Stop(); // Unsubscribe from the frame counter. It will be re-created when starting again. - picDesktop._frameCounter.FrameUpdated -= _frameCounter_FrameUpdated; + picDesktop.UnsetFrameUpdatedEvent(_frameCounter_FrameUpdated); } private void barQuality_Scroll(object sender, EventArgs e) @@ -238,12 +228,12 @@ private void btnMouse_Click(object sender, EventArgs e) private int GetRemoteWidth(int localX) { - return localX * _screenWidth / picDesktop.Width; + return localX * picDesktop.ScreenWidth / picDesktop.Width; } private int GetRemoteHeight(int localY) { - return localY * _screenHeight / picDesktop.Height; + return localY * picDesktop.ScreenHeight / picDesktop.Height; } private void picDesktop_MouseDown(object sender, MouseEventArgs e) diff --git a/Server/Server.csproj b/Server/Server.csproj index babc5a8c..5f8875d9 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -71,12 +71,9 @@ Component - + Component - - PictureBoxEx.cs - @@ -378,9 +375,6 @@ - - PictureBoxEx.cs - FrmAbout.cs