Specify monitor when opening file. (.bat)

后端 未结 2 1225
温柔的废话
温柔的废话 2020-12-06 20:56

The following .bat file below simply opens two text files overlaying them, but I\'m wondering if it\'s possible to define a specific display source, or if anyone can assist

2条回答
  •  北海茫月
    2020-12-06 21:47

    Unless the application you're launching has a command-line switch for it, there's no easy way to specify on which monitor to display a window. As far as I'm aware, neither start nor notepad supports such a switch. The closest solution I've found is to move a window after it's already open.

    Edit: user32.dll SetWindowPos() invoked from PowerShell

    Here's a hybrid batch + PowerShell script to launch a program and move it to a specific monitor. Save it with a .bat extension.

    <# : batch portion
    @echo off & setlocal disabledelayedexpansion
    
    set args=%*
    call set args=%%args:%1 %2=%%
    set "exe=%~2"
    set "monitor=%~1"
    set "scriptname=%~nx0"
    powershell -noprofile "iex (${%~f0} | out-string)"
    exit /b %ERRORLEVEL%
    
    : end batch / begin powershell #>
    
    function usage() {
        write-host -nonewline "Usage: "
        write-host -f white "$env:scriptname monitor# filename [arguments]`n"
        write-host -nonewline "* "
        write-host -f white -nonewline "monitor# "
        write-host "is a 1-indexed integer.  Monitor 1 = 1, monitor 2 = 2, etc."
        write-host -nonewline "* "
        write-host -f white -nonewline "filename "
        write-host "is an executable or a document or media file.`n"
        write-host -nonewline "$env:scriptname mimics "
        write-host -f white -nonewline "start"
        write-host ", searching for filename both in %PATH% and"
        write-host "in Windows' app paths (web browsers, media players, etc).`n"
        write-host "Examples:"
        write-host "To display YouTube in Firefox on your second monitor, do"
        write-host -f white "     $env:scriptname 2 firefox `"www.youtube.com`"`n"
        write-host "To play an mp3 file using the default player on monitor 1:"
        write-host -f white "     $env:scriptname 1 mp3file.mp3"
        exit 1
    }
    
    add-type user32_dll @'
        [DllImport("user32.dll")]
        public static extern void SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
            int x, int y, int cx, int cy, uint uFlags);
    '@ -namespace System
    
    add-type -as System.Windows.Forms
    if ($env:monitor -gt [windows.forms.systeminformation]::MonitorCount) {
        [int]$monitor = [windows.forms.systeminformation]::MonitorCount
    } else {
        [int]$monitor = $env:monitor
    }
    try {
        if ($env:args) {
            $p = start $env:exe $env:args -passthru
        } else {
            $p = start $env:exe -passthru
        }
    }
    catch { usage }
    
    $shell = new-object -COM Wscript.Shell
    while (-not $shell.AppActivate($p.Id) -and ++$i -lt 100) { sleep -m 50 }
    
    try {
        $x = [Windows.Forms.Screen]::AllScreens[--$monitor].Bounds.X
        $hwnd = (Get-Process -id $p.Id)[0].MainWindowHandle
        [user32_dll]::SetWindowPos($hwnd, [intptr]::Zero, $x, 0, 0, 0, 0x41);
    }
    finally { exit 0 }
    

    Original answer: compile and link c# executable

    And moving a window is no easy task, either. See this post for some other options. But here's a batch script that will compose and link a C# app on the fly to handle window moves.

    @echo off
    setlocal
    
    :: // generate c.cs
    call :heredoc movewind >"%temp%\c.cs" && goto compile_and_link
    // syntax: movewind.exe [pid | "window title"] x y
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    class movewind {
        [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) {
            int pid;
            string title;
            bool res = Int32.TryParse(args[0], out pid);
            if (res) {title = Process.GetProcessById(pid).MainWindowTitle;} else {title = args[0];}
            IntPtr handle = FindWindow(null, title);
            try {
                SetWindowPos(handle, IntPtr.Zero, Convert.ToInt32(args[1]), Convert.ToInt32(args[2]), 0, 0, 0x41);
            }
            catch (Exception e) {
                Console.WriteLine("Exception caught while attempting to move window with handle " + handle);
                Console.WriteLine(e);
            }
        }
    }
    :compile_and_link
    
    set "movewind=%temp%\movewind.exe"
    
    for /f "delims=" %%I in ('dir /b /s "%windir%\microsoft.net\*csc.exe"') do (
        if not exist "%movewind%" "%%I" /nologo /out:"%movewind%" "%temp%\c.cs" 2>NUL
    )
    del "%temp%\c.cs"
    if not exist "%movewind%" (
        echo Error: Please install .NET 2.0 or newer.
        goto :EOF
    )
    
    :: // get left monitor width
    for /f "tokens=2 delims==" %%I in ('wmic desktopmonitor get screenwidth /format:list') do set "x=%%I"
    
    :: // make sure test environment is in place
    if not exist "c:\test" mkdir "c:\test"
    if not exist "c:\test\screen1.txt" >"c:\test\screen1.txt" echo This should be on the left.
    if not exist "c:\test\screen2.txt" >"c:\test\screen2.txt" echo This should be on the right.
    
    :: // syntax: movewind.exe [pid | "window title"] x y
    start /max notepad.exe "c:\test\screen1.txt"
    call :movewind "screen1.txt - Notepad" 0 0
    start /max notepad.exe "c:\test\screen2.txt"
    call :movewind "screen2.txt - Notepad" %x% 0
    
    del "%movewind%"
    
    :: // end main runtime
    goto :EOF
    
    :: // SCRIPT FUNCTIONS
    
    :movewind  <x> <y>
    tasklist /v | find /i "%~1" && (
        "%movewind%" "%~1" %~2 %~3
        goto :EOF
    ) || (
        ping -n 1 -w 500 169.254.1.1 >NUL
        goto movewind
    )
    
    :heredoc <uniqueIDX>
    :: // https://stackoverflow.com/a/15032476/1683264
    setlocal enabledelayedexpansion
    set go=
    for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
        set "line=%%A" && set "line=!line:*:=!"
        if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
        if "!line:~0,13!"=="call :heredoc" (
            for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
                if #%%i==#%1 (
                    for /f "tokens=2 delims=&" %%I in ("!line!") do (
                        for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                    )
                )
            )
        )
    )
    goto :EOF
    </code></pre>
        </p>
                 <div class="appendcontent">
                                                            </div>
                </div>
                <div class="jieda-reply">
                  <span class="jieda-zan button_agree" type="zan" data-id='898660'>
                    <i class="iconfont icon-zan"></i>
                    <em>0</em>
                  </span>
                       <span type="reply" class="showpinglun" data-id="898660">
                    <i class="iconfont icon-svgmoban53"></i>
                   讨论(0)
                  </span>
                                                      
                  
                  <div class="jieda-admin">
                              
                 
           
              
                  </div>
                                           <div class="noreplaytext bb">
    <center><div>   <a href="https://www.e-learn.cn/qa/q-270865.html">  查看其它2个回答
    </a>
    </div></center>
    </div>            </div>
                             <div class="comments-mod "  style="display: none; float:none;padding-top:10px;" id="comment_898660">
                        <div class="areabox clearfix">
    
    <form class="layui-form" action="">
                   
                <div class="layui-form-item">
        <label class="layui-form-label" style="padding-left:0px;width:60px;">发布评论:</label>
        <div class="layui-input-block" style="margin-left:90px;">
             <input type="text" placeholder="不少于5个字" AUTOCOMPLETE="off" class="comment-input layui-input" name="content" />
                            <input type='hidden' value='0' name='replyauthor' />
        </div>
        <div class="mar-t10"><span class="fr layui-btn layui-btn-sm addhuidapinglun" data-id="898660">提交评论 </span></div>
      </div>
      
    </form>
                        </div>
                        <hr>
                        <ul class="my-comments-list nav">
                            <li class="loading">
                            <img src='https://www.e-learn.cn/qa/static/css/default/loading.gif' align='absmiddle' />
                             加载中...
                            </li>
                        </ul>
                    </div>
              </li>
                                  			
            </ul>
            
            <div class="layui-form layui-form-pane">
              <form id="huidaform"  name="answerForm"  method="post">
                
                <div class="layui-form-item layui-form-text">
                  <a name="comment"></a>
                  <div class="layui-input-block">
                
        
    <script type="text/javascript" src="https://www.e-learn.cn/qa/static/js/neweditor/ueditor.config.js"></script>
    <script type="text/javascript" src="https://www.e-learn.cn/qa/static/js/neweditor/ueditor.all.js"></script>
    <script type="text/plain" id="editor"  name="content"  style="width:100%;height:200px;"></script>                                 
    <script type="text/javascript">
                                     var isueditor=1;
                var editor = UE.getEditor('editor',{
                    //这里可以选择自己需要的工具按钮名称,此处仅选择如下五个
                    toolbars:[['source','fullscreen',  '|', 'undo', 'redo', '|', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight', '|', 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'link', 'unlink', 'anchor', '|', 'simpleupload', 'insertimage', 'scrawl', 'insertvideo', 'attachment', 'map', 'insertcode', '|', 'horizontal', '|', 'preview', 'searchreplace', 'drafts']],
                
                    initialContent:'',
                    //关闭字数统计
                    wordCount:false,
                    zIndex:2,
                    //关闭elementPath
                    elementPathEnabled:false,
                    //默认的编辑区域高度
                    initialFrameHeight:250
                    //更多其他参数,请参考ueditor.config.js中的配置项
                    //更多其他参数,请参考ueditor.config.js中的配置项
                });
                            editor.ready(function() {
                	editor.setDisabled();
                	});
                                $("#editor").find("*").css("max-width","362px");
            </script>              </div>
                </div>
                              
        
    
            
             <div class="layui-form-item">
                    <label for="L_vercode" class="layui-form-label">验证码</label>
                    <div class="layui-input-inline">
                      <input type="text"  id="code" name="code"   value="" required lay-verify="required" placeholder="图片验证码" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">
                      <span style="color: #c00;"><img class="hand" src="https://www.e-learn.cn/qa/user/code.html" onclick="javascript:updatecode();" id="verifycode"><a class="changecode"  href="javascript:updatecode();"> 看不清?</a></span>
                    </div>
                  </div>
                                      <div class="layui-form-item">
                        <input type="hidden" value="270865" id="ans_qid" name="qid">
       <input type="hidden" id="tokenkey" name="tokenkey" value=''/>
                    <input type="hidden" value="Specify monitor when opening file. (.bat)" id="ans_title" name="title"> 
                 
                  <div class="layui-btn    layui-btn-disabled"  id="ajaxsubmitasnwer" >提交回复</div>
                </div>
              </form>
            </div>
          </div>
          <input type="hidden" value="270865" id="adopt_qid"	name="qid" /> 
          <input type="hidden" id="adopt_answer" value="0"	name="aid" />
        </div>
        <div class="layui-col-md4">
              
     <!-- 热门讨论问题 -->
         
     <dl class="fly-panel fly-list-one">
            <dt class="fly-panel-title">热议问题</dt>
                <!-- 本周热门讨论问题显示10条-->