如何指定外部控制台程序的窗口位置? [英] How can I specify the window position of an external console program?

查看:123
本文介绍了如何指定外部控制台程序的窗口位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Win32 VCL应用程序中,我正在使用ShellExecute启动许多较小的Delphi控制台应用程序。有没有办法控制这些控制台窗口的位置?我想以屏幕为中心启动它们。

In my Win32 VCL application I am using ShellExecute to launch a number of smaller Delphi console applications. Is there a way to control the position of those console windows? I would like to launch them centered on screen.

推荐答案

如果您可以控制控制台应用程序,则可以从控制台应用程序内部设置控制台窗口的位置:

If you have control over the console application, You could set the console window position from inside the console application itself:

program Project1;
{$APPTYPE CONSOLE}
uses
  Windows,
  MultiMon;

function GetConsoleWindow: HWND; stdcall; external kernel32 name 'GetConsoleWindow';

procedure SetConsoleWindowPosition;
var
  ConsoleHwnd: HWND;
  R: TRect;
begin
  ConsoleHwnd := GetConsoleWindow;
  // Center the console window
  GetWindowRect(ConsoleHwnd, R);
  SetWindowPos(ConsoleHwnd, 0,
    (GetSystemMetrics(SM_CXVIRTUALSCREEN) - (R.Right - R.Left)) div 2,
    (GetSystemMetrics(SM_CYVIRTUALSCREEN) - (R.Bottom - R.Top)) div 2,
    0, 0, SWP_NOSIZE);
end;

begin
  SetConsoleWindowPosition;  
  // Other code...
  Readln;
end.

如果无法重新编译控制台应用程序,则可以使用 FindWindow( 'ConsoleWindowClass','<可执行文件的路径>')以获取控制台窗口句柄(如果通过 SetConsoleTitle )。
这种方法的缺点是,控制台窗口会从默认位置跳到新位置(在Windows XP中经过测试)。

If you can't recompile the console application, you could use FindWindow('ConsoleWindowClass', '<path to the executable>') to obtain the console window handle (the Title parameter could vary if it was set via SetConsoleTitle). The downside with this approach, is that the console window is seen "jumping" from it's default position to it's new position (tested with Windows XP).

这篇关于如何指定外部控制台程序的窗口位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆