Skip to content

Commit

Permalink
#1 add cmd options to snapToProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
michiproep committed Jul 4, 2021
1 parent 076a028 commit 774bbb4
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 2 deletions.
Binary file removed bin/ScreenshareHelper.exe
Binary file not shown.
Binary file removed bin/ScreenshareHelper_x86.exe
Binary file not shown.
Binary file not shown.
Binary file added bin/x86/ScreenshareHelper.exe
Binary file not shown.
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ I started building this tool on .net 5 windows forms, so you should install the
## Download

[ScreenshareHelper.exe](https://github.com/michiproep/ScreenshareHelper/tree/master/bin)
Either use x86 or x64 precompiled verion.

## How to use the tool?
1. Start ScreenshareHelper.exe => Transparent window appears
Expand All @@ -27,6 +28,18 @@ I started building this tool on .net 5 windows forms, so you should install the
You can actually move the tool to another screen (if you wish and got one) - once you set the capture area with the set-button. This way you can actually see what's happening

## Command Line Options / SnapToProcess
You can create a .bat file or use a desktop shortcut to add command line options to let the tool snap to a certain process on startup.
Use process name or better process ID (MainWindow process ID) to accomplish this.

-n => Process name (e.g. firefox)
-i => PID

Examples
```
ScreenshareHelper.exe -n notepad2
ScreenshareHelper.exe -i 12345
```
## Donate
If you like the tool, just [Paypal.me](https://paypal.me/mlproe?locale.x=de_DE)

Expand Down
13 changes: 13 additions & 0 deletions src/ScreenshareHelper/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using CommandLine;

namespace ScreenshareHelper
{
public class Options
{
[Option('n', "processname", Required = false, HelpText = "Process name to snap to at startup.", SetName = "process")]
public string Process { get; set; }

[Option('i', "pid", Required = false, HelpText = "Process ID to snap to at startup.", SetName = "process")]
public int? ProcessID { get; set; }
}
}
82 changes: 81 additions & 1 deletion src/ScreenshareHelper/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using CommandLine;
using ScreenshareHelper.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -12,12 +16,88 @@ static class Program
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args).
WithParsed(o =>
{
if (!string.IsNullOrEmpty(o.Process))
SnapToProcess(o.Process);
else if (o.ProcessID.HasValue)
SnapToProcess(o.ProcessID.Value);
}
);

Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}


#region SnapToProcess
private static void SnapToProcess(int processID)
{
var p = System.Diagnostics.Process.GetProcessById(processID);
if(p != null && p.MainWindowHandle != IntPtr.Zero)
{
if(GetWindowRect(p.MainWindowHandle, out RECT r))
{
r = GetWindowBounds(p.MainWindowHandle);
Settings.Default.CaptureLocation = new System.Drawing.Point(r.left, r.top);
Settings.Default.CaptureSize = new System.Drawing.Size(r.right -r.left, r.bottom -r.top);
Settings.Default.Save();
}
}
}

private static void SnapToProcess(string process)
{
//find process
var tmp = System.Diagnostics.Process.GetProcesses();
var p = System.Diagnostics.Process.GetProcessesByName(process).FirstOrDefault();
if (p != null)
SnapToProcess(p.Id);
}
#endregion

#region Win32

[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}

[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT Rect);

[DllImport("dwmapi.dll")]
static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, IntPtr pvAttribute, int cbAttribute);

public static RECT GetWindowBounds(IntPtr handle)
{
RECT rect;
if (Environment.OSVersion.Version.Major < 6)
{
//Is Below Vista (exclusive)
if (!GetWindowRect(handle, out rect))
throw new Win32Exception(Marshal.GetLastWin32Error());
return rect;
}
//Vista (inclusive) and above will include shadows in GetWindowRect.
IntPtr ptrFrame = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(RECT)));
int ret = DwmGetWindowAttribute(handle, /*(int)DWMWA.EXTENDED_FRAME_BOUNDS*/ 9, ptrFrame, Marshal.SizeOf(typeof(RECT)));
if (ret != 0)
throw new Win32Exception(ret);
rect = (RECT)Marshal.PtrToStructure(ptrFrame, typeof(RECT));
Marshal.FreeHGlobal(ptrFrame);
return rect;
}

#endregion
}
}
8 changes: 8 additions & 0 deletions src/ScreenshareHelper/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"ScreenshareHelper": {
"commandName": "Project",
"commandLineArgs": "-i 35188"
}
}
}
5 changes: 4 additions & 1 deletion src/ScreenshareHelper/ScreenshareHelper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ApplicationIcon>rocket_startup_monitor_screen_computer_icon_124621.ico</ApplicationIcon>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
Expand Down

0 comments on commit 774bbb4

Please sign in to comment.