Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to open executables and snap to top left hand corner of desktop

I was just wonderin' in a .bat file, if there was a way to call an external .bat file, or even an *.exe and make it open so it 'snaps' to the top left hand corner of the screen ?

Cheers

like image 411
user226973 Avatar asked Sep 11 '25 06:09

user226973


1 Answers

There is no direct way to position a Window from the Windows command prompt. You basically have the following options:

  • Use a GUI automation tool, e.g. AutoHotkey which lets you script window actions. AutoHotkey e.g. offers the WinMove command:

    Run, calc.exe
    WinWait, Calculator
    WinMove, 0, 0 ; Move the window found by WinWait to the upper-left corner of the screen.
    
  • Use PowerShell, e.g. with the WASP snapin (http://wasp.codeplex.com/).

  • Write a short program in C/C++/.NET that will position the active Window at position 0,0 of your main screen.

A very basic program in C#, that takes a window caption as parameter could look like that:

using System;
using System.Runtime.InteropServices;

class Program
{
    public const int SWP_NOSIZE = 0x0001;
    public const int SWP_NOZORDER = 0x0004;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    static void Main(string[] args)
    {
        IntPtr handle = FindWindow(null, args[0]);
        SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
}
like image 92
Dirk Vollmar Avatar answered Sep 13 '25 08:09

Dirk Vollmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!