using System;
using System.Drawing;
using System.Windows.Forms;
// The 'windowHandle' parameter will contain the window handle for the:
// - Active window when run by hotkey
// - Window Location target when run by a Window Location rule
// - TitleBar Button owner when run by a TitleBar Button
// - Jump List owner when run from a Taskbar Jump List
// - Currently focused window if none of these match
public static class DisplayFusionFunction
{
public static void Run(IntPtr windowHandle)
{
//get the width from the user
using(WindowPercentInput dialog = new WindowPercentInput(windowHandle))
{
if(dialog.ShowDialog() != DialogResult.OK)
return;
Rectangle workArea = BFS.Monitor.GetMonitorWorkAreaByWindow(windowHandle);
try
{
int width, height;
//if the user entered nothing for the width, keep it the same
if(string.IsNullOrEmpty(dialog.WidthPercent))
{
width = BFS.Window.GetBounds(windowHandle).Width;
}
else
{
//if the number entered is greater than 100, treat is as a pixel value
int dialogWidth = Convert.ToInt32(dialog.WidthPercent);
if(dialogWidth > 100)
width = dialogWidth;
else
width = (int)Math.Round(workArea.Width * dialogWidth / 100m);
}
//if the user entered nothing for the height, keep it the same
if(string.IsNullOrEmpty(dialog.HeightPercent))
{
height = BFS.Window.GetBounds(windowHandle).Height;
}
else
{
//if the number entered is greater than 100, treat is as a pixel value
int dialogHeight = Convert.ToInt32(dialog.HeightPercent);
if(dialogHeight > 100)
height = dialogHeight;
else
height = (int)Math.Round(workArea.Height * dialogHeight / 100m);
}
//set the window size, compensate for invisible windows borders if this is windows 10
BFS.Window.SetSize(windowHandle, width, height);
}
catch
{
}
}
}
private class WindowPercentInput : Form
{
private TextBox txtWidth = new TextBox();
private TextBox txtHeight = new TextBox();
private IntPtr ParentWindow = IntPtr.Zero;
internal string WidthPercent
{
get
{
return txtWidth.Text;
}
}
internal string HeightPercent
{
get
{
return txtHeight.Text;
}
}
internal WindowPercentInput(IntPtr parent)
{
Label label2 = new Label();
Label label1 = new Label();
Label label3 = new Label();
Button btnOK = new Button();
Button btnCancel = new Button();
this.ParentWindow = parent;
this.SuspendLayout();
Rectangle workArea = BFS.Monitor.GetMonitorWorkAreaByWindow(parent);
Rectangle windowBounds = BFS.Window.GetBounds(parent);
label1.Location = new Point(8, 8);
label1.Size = new Size(296, 23);
label1.TabIndex = 0;
if(BFS.Window.IsMaximized(parent))
label1.Text = "Current Window: Maximized";
else
label1.Text = string.Format("Current Window Size: {0}px ({1}%) x {2}px ({3}%)", windowBounds.Width, (int)Math.Round(windowBounds.Width / (decimal)workArea.Width * 100m), windowBounds.Height, (int)Math.Round(windowBounds.Height / (decimal)workArea.Height * 100m));
label1.TextAlign = ContentAlignment.MiddleLeft;
label3.Location = new Point(8, 32);
label3.Size = new Size(296, 23);
label3.TabIndex = 1;
label3.Text = "Enter the width and height in percent:";
label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.txtWidth.Location = new Point(8, 64);
this.txtWidth.Size = new Size(144, 20);
this.txtWidth.TabIndex = 2;
label2.Location = new Point(152, 64);
label2.Size = new Size(16, 23);
label2.TabIndex = 3;
label2.Text = "X";
label2.TextAlign = ContentAlignment.MiddleLeft;
this.txtHeight.Location = new Point(168, 64);
this.txtHeight.Size = new Size(144, 20);
this.txtHeight.TabIndex = 4;
btnOK.Location = new Point(144, 96);
btnOK.Size = new Size(80, 24);
btnOK.TabIndex = 5;
btnOK.Text = "OK";
btnCancel.DialogResult = DialogResult.Cancel;
btnCancel.Location = new Point(232, 96);
btnCancel.Size = new Size(80, 24);
btnCancel.TabIndex = 6;
btnCancel.Text = "Cancel";
btnCancel.UseVisualStyleBackColor = true;
this.ClientSize = new System.Drawing.Size(323, 131);
this.Controls.Add(this.txtHeight);
this.Controls.Add(this.txtWidth);
this.Controls.Add(label1);
this.Controls.Add(label3);
this.Controls.Add(btnCancel);
this.Controls.Add(btnOK);
this.Controls.Add(label2);
this.MinimizeBox = false;
this.MaximizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.ResumeLayout(false);
this.Text = "Set Window Size";
btnOK.Click += btnOK_Click;
btnCancel.Click += btnCancel_Click;
this.Load += OnLoad;
this.AcceptButton = btnOK;
this.CancelButton = btnCancel;
}
private void OnLoad(object sender, EventArgs e)
{
if(this.ParentWindow != IntPtr.Zero)
{
Rectangle bounds = BFS.Window.GetBounds(this.ParentWindow);
this.Location = new Point(bounds.X - this.Width / 2 + bounds.Width / 2, bounds.Y - this.Height / 2 + bounds.Height / 2);
}
BFS.Window.Focus(this.Handle);
BFS.Window.SetAlwaysOnTop(this.Handle, true);
this.txtWidth.Focus();
}
private void btnOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}
}