Author Topic: Get Screen Data  (Read 6229 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Get Screen Data
« on: April 05, 2019, 11:31:51 AM »
I was inspired by a discussion between Pfeil and Bailey to create a small profile with commands and an inline function for retrieving screen data (size in pixels as well as a few other bits). The main use here would be for adjusting scaling/maths of pixel/size-dependent actions based on the user's screen size. There may be ways to trim the code down further and Gary may have plans to effectively obsolete this profile in the future, however for now it does the job well. You can also read more about the Screen Class to see what else is possible.

Here are the inline function details:
Referenced Assemblies: Microsoft.CSharp.dll;System.dll;System.Core.dll;System.Drawing.dll;System.Windows.Forms.dll
Code: [Select]
using System.Windows.Forms;

public class VAInline
{
public void main()
{
string ScreenAction = VA.GetText("~~ScreenInfoAction"); // Get requested screen data retrieval action from VoiceAttack text variable

Form f = null; // Declare Form f
try // Attempt the following...
{
f = new MyForm(VA); // Create new MyForm instance
}
catch { VA.WriteToLog("error!", "red"); } // Output info to event log
finally // Perform after completing try
{
f.Close(); // Close the form
}

if (ScreenAction == "all") // Check if requested screen data retrieval action is for ALL screens
{
// For each screen, output relevant information
foreach (var screen in System.Windows.Forms.Screen.AllScreens)
{
VA.WriteToLog("Screen Name: " + screen.DeviceName);
VA.WriteToLog("Screen Size: " + screen.Bounds.Width + " x " + screen.Bounds.Height);
VA.WriteToLog("Primary Screen: " + screen.Primary.ToString());
VA.WriteToLog("Active Screen: " + (screen.DeviceName == MyForm.ActiveScreen.DeviceName ? "True" : "False"));
VA.WriteToLog("----");
}
}
else if (ScreenAction == "active") // Check if requested screen data retrieval action is for the ACTIVE screen
{
// Output info about the active screen to the event log
VA.WriteToLog("Active Screen Name: " + MyForm.ActiveScreen.DeviceName);
VA.WriteToLog("Screen Size: " + MyForm.ActiveScreen.Bounds.Width + " x " + MyForm.ActiveScreen.Bounds.Height);
}
else
VA.WriteToLog("Unrecognized screen data retrieval action", "red"); // Output info to event log
}
}

class MyForm : Form
{
public static Screen ActiveScreen {set; get;} // Declare public variable for storing ActiveScreen

public MyForm(dynamic VA)
{
this.StartPosition = FormStartPosition.CenterScreen; // Set the start position of the form to the center of the screen
ActiveScreen = Screen.FromControl(this); // Capture the screen that contains the (not shown) form
}
}

// References:
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.screen?view=netframework-4.7.2
// https://stackoverflow.com/questions/1121600/how-do-i-determine-which-monitor-my-net-windows-forms-program-is-running-on

And here is a sample of the output for both included commands on my 1920x1080 3-monitor setup:


Support This and Future Efforts
If you find this profile useful, please consider buying me a cup of coffee. Thank you for your support!
 


Enjoy!  :)
« Last Edit: June 02, 2020, 09:44:40 PM by Exergist »

ikkuranus

  • Newbie
  • *
  • Posts: 6
Re: Get Screen Data
« Reply #1 on: August 12, 2024, 02:23:13 AM »
Any chance you could make this dump the active screen resolution into a text variable?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Get Screen Data
« Reply #2 on: August 12, 2024, 03:05:18 AM »
That's very simple, as Exergist has done all the hard work already.

If you just need the active screen resolution in a set of variables, the inline function can be simplified to something like
Code: [Select]
using System.Windows.Forms;

public class VAInline
{
public void main()
{
Form f = null; // Declare Form f
try // Attempt the following...
{
f = new MyForm(VA); // Create new MyForm instance
}
catch { VA.WriteToLog("error!", "red"); } // Output info to event log
finally // Perform after completing try
{
f.Close(); // Close the form
}

// Output active screen width and height to VoiceAttack variables
VA.SetInt("~activeScreenWidth", MyForm.ActiveScreen.Bounds.Width);
VA.SetInt("~activeScreenHeight", MyForm.ActiveScreen.Bounds.Height);
}
}

class MyForm : Form
{
public static Screen ActiveScreen {set; get;} // Declare public variable for storing ActiveScreen

public MyForm(dynamic VA)
{
this.StartPosition = FormStartPosition.CenterScreen; // Set the start position of the form to the center of the screen
ActiveScreen = Screen.FromControl(this); // Capture the screen that contains the (not shown) form
}
}

// References:
// https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.screen?view=netframework-4.7.2
// https://stackoverflow.com/questions/1121600/how-do-i-determine-which-monitor-my-net-windows-forms-program-is-running-on

Which would output to integer variables named "~activeScreenWidth" and "~activeScreenHeight" (note that these are command-scoped variables, I.E. they'll only be available to that instance of the command; remove the "~" prefix if you want to make them global, and are certain that won't cause conflicts)

ikkuranus

  • Newbie
  • *
  • Posts: 6
Re: Get Screen Data
« Reply #3 on: August 12, 2024, 04:04:13 AM »
Thanks! I did eventually figure it out but that way is definitely more efficient.