Processing Ajax...

Title
Close Dialog

Message

Confirm
Close Dialog

Confirm
Close Dialog

Confirm
Close Dialog

Specify Window Left, Top, Width, and Height Percentage

Description
This script allows you to specify the X, Y location of the window, and the width/height in percent (less than 100) or pixels (greater than 100).
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
May 31, 2018
Date Last Modified
May 31, 2018

Scripted Function (Macro) Code

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)
	{
    	//check to see if this is windows 10
		bool isWindows10 = (Environment.OSVersion.Version.Major == 9) || (Environment.OSVersion.Version.Major == 10);
    
		//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 x, y, 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);
				}
				
				if(string.IsNullOrEmpty(dialog.LeftPercent))
                    x = BFS.Window.GetBounds(windowHandle).X;			
				else
                    x = Convert.ToInt32(dialog.LeftPercent);
				
				if(string.IsNullOrEmpty(dialog.TopPercent))
                    y = BFS.Window.GetBounds(windowHandle).Y;			
				else
                    y = Convert.ToInt32(dialog.TopPercent);
				
				
                BFS.Window.SetSizeAndLocation(windowHandle, x, y, width, height);
			}
			catch
			{
			}
		}
	}

	private class WindowPercentInput : Form
	{		
		private IntPtr ParentWindow = IntPtr.Zero;
		
		private Button btnCancel;
		private Button btnOK;
		private Label lblWindow;
		private Label lblLeft;
		private Label lblTop;
		private Label lblWidth;
		private Label lblHeight;
		private TextBox txtLeft;
		private TextBox txtTop;
		private TextBox txtWidth;
		private TextBox txtHeight;
		private Label label1;
		private Label label2;
		private Label label3;
		private Label label4;
		
		public string WidthPercent
		{
			get
			{
				return this.txtWidth.Text;
			}
		}
		
		public string HeightPercent
		{
			get
			{
				return this.txtHeight.Text;
			}
		}
				
		public string TopPercent
		{
            get
            {
                return this.txtTop.Text;
            }
		}
			
        public string LeftPercent
        {
            get
            {
                return this.txtLeft.Text;
            }
        }
				
		public WindowPercentInput(IntPtr parent)
		{
			this.ParentWindow = parent;
			this.btnCancel = new Button();
			this.btnOK = new Button();
			this.lblWindow = new Label();
			this.lblLeft = new Label();
			this.lblTop = new Label();
			this.lblWidth = new Label();
			this.lblHeight = new Label();
			this.txtLeft = new TextBox();
			this.txtTop = new TextBox();
			this.txtWidth = new TextBox();
			this.txtHeight = new TextBox();
			this.label1 = new Label();
			this.label2 = new Label();
			this.label3 = new Label();
			this.label4 = new Label();
			
			this.SuspendLayout();

			this.btnCancel.Location = new System.Drawing.Point(232, 144);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(80, 24);
			this.btnCancel.TabIndex = 14;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.UseVisualStyleBackColor = true;

			this.btnOK.Location = new System.Drawing.Point(144, 144);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(80, 24);
			this.btnOK.TabIndex = 13;
			this.btnOK.Text = "OK";
			this.btnOK.UseVisualStyleBackColor = true;

			this.lblWindow.Location = new System.Drawing.Point(16, 16);
			this.lblWindow.Name = "lblWindow";
			this.lblWindow.Size = new System.Drawing.Size(296, 24);
			this.lblWindow.TabIndex = 0;
			this.lblWindow.Text = "Current Window: ";
			this.lblWindow.TextAlign = System.Drawing.ContentAlignment.TopLeft;

			this.lblLeft.Location = new System.Drawing.Point(16, 40);
			this.lblLeft.Name = "lblLeft";
			this.lblLeft.Size = new System.Drawing.Size(72, 24);
			this.lblLeft.TabIndex = 1;
			this.lblLeft.Text = "Left:";
			this.lblLeft.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

			this.lblTop.Location = new System.Drawing.Point(16, 64);
			this.lblTop.Name = "lblTop";
			this.lblTop.Size = new System.Drawing.Size(72, 24);
			this.lblTop.TabIndex = 4;
			this.lblTop.Text = "Top:";
			this.lblTop.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

			this.lblWidth.Location = new System.Drawing.Point(16, 88);
			this.lblWidth.Name = "lblWidth";
			this.lblWidth.Size = new System.Drawing.Size(72, 24);
			this.lblWidth.TabIndex = 7;
			this.lblWidth.Text = "Width:";
			this.lblWidth.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

			this.lblHeight.Location = new System.Drawing.Point(16, 112);
			this.lblHeight.Name = "lblHeight";
			this.lblHeight.Size = new System.Drawing.Size(72, 24);
			this.lblHeight.TabIndex = 10;
			this.lblHeight.Text = "Height:";
			this.lblHeight.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

			this.txtLeft.Location = new System.Drawing.Point(88, 40);
			this.txtLeft.Name = "txtLeft";
			this.txtLeft.Size = new System.Drawing.Size(200, 20);
			this.txtLeft.TabIndex = 2;

			this.txtTop.Location = new System.Drawing.Point(88, 64);
			this.txtTop.Name = "txtTop";
			this.txtTop.Size = new System.Drawing.Size(200, 20);
			this.txtTop.TabIndex = 5;

			this.txtWidth.Location = new System.Drawing.Point(88, 88);
			this.txtWidth.Name = "txtWidth";
			this.txtWidth.Size = new System.Drawing.Size(200, 20);
			this.txtWidth.TabIndex = 8;

			this.txtHeight.Location = new System.Drawing.Point(88, 112);
			this.txtHeight.Name = "txtHeight";
			this.txtHeight.Size = new System.Drawing.Size(200, 20);
			this.txtHeight.TabIndex = 11;

			this.label1.Location = new System.Drawing.Point(288, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(24, 24);
			this.label1.TabIndex = 3;
			this.label1.Text = "px";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

			this.label2.Location = new System.Drawing.Point(288, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(24, 24);
			this.label2.TabIndex = 6;
			this.label2.Text = "px";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

			this.label3.Location = new System.Drawing.Point(288, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(24, 24);
			this.label3.TabIndex = 9;
			this.label3.Text = "%";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

			this.label4.Location = new System.Drawing.Point(288, 112);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(24, 24);
			this.label4.TabIndex = 12;
			this.label4.Text = "%";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(329, 184);
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			this.Controls.Add(this.label4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.txtHeight);
			this.Controls.Add(this.txtWidth);
			this.Controls.Add(this.txtTop);
			this.Controls.Add(this.txtLeft);
			this.Controls.Add(this.lblHeight);
			this.Controls.Add(this.lblWidth);
			this.Controls.Add(this.lblTop);
			this.Controls.Add(this.lblLeft);
			this.Controls.Add(this.lblWindow);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnCancel);
			
			this.ResumeLayout(false);
			this.Text = "Set Window Size";
			
			Rectangle windowBounds = BFS.Window.GetBounds(parent);
			Rectangle workArea = BFS.Monitor.GetMonitorWorkAreaByWindow(parent);
            if(BFS.Window.IsMaximized(parent))
				this.lblWindow.Text = "Current Window: Maximized";
			else
				this.lblWindow.Text = string.Format("Bounds: [ X: {0}, Y: {1}, W: {2} ({3}%), H: {4} ({5}%) ]", windowBounds.X, windowBounds.Y, windowBounds.Width, (int)Math.Round(windowBounds.Width / (decimal)workArea.Width * 100m), windowBounds.Height, (int)Math.Round(windowBounds.Height / (decimal)workArea.Height * 100m));
			
			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();
		}
	}
}