BCB多显示器开发的问题!
1、现在有一个别人的程序,打开时是没有窗口标题栏的。
2、我要写一个程序,把它的程序移动到第二个显示器上显示
这里我遇到两个问题
1、如何获取别人程序的工作窗口
2、如何移动没有标题栏的窗口
[解决办法]
获取一个窗口的方法很多:
有EnumWindows,EnumChildWindows,FindWindow,FindWindowEx,GetWindow等等。
移动没有标题栏的窗口:
获取到该窗口句柄以后,通过MoveWindow或SetWindowPos改变其位置就行了。
[解决办法]
> > > 查询出程序中所有的窗口
用EnumWindows就可以了。
> > > 把窗口定位到我想要的显示器上
GetSystemMetrics(SM_CMONITORS)可以获取当前显示器的总数
EnumDisplayMonitors枚举每个显示器
至于把窗口定位到指定的显示器上,可以参考一下VCL中Forms.pas的源码:
procedure TCustomForm.SetWindowToMonitor;
var
AppMon, WinMon: HMONITOR;
I, J: Integer;
ALeft, ATop: Integer;
begin
if (FDefaultMonitor <> dmDesktop) and (Application.MainForm <> nil) then
begin
AppMon := 0;
if FDefaultMonitor = dmMainForm then
AppMon := Application.MainForm.Monitor.Handle
else if (FDefaultMonitor = dmActiveForm) and (Screen.ActiveCustomForm <> nil) then
AppMon := Screen.ActiveCustomForm.Monitor.Handle
else if FDefaultMonitor = dmPrimary then
AppMon := Screen.Monitors[0].Handle;
WinMon := Monitor.Handle;
for I := 0 to Screen.MonitorCount - 1 do
if (Screen.Monitors[I].Handle = AppMon) then
if (AppMon <> WinMon) then
for J := 0 to Screen.MonitorCount - 1 do
if (Screen.Monitors[J].Handle = WinMon) then
begin
if FPosition = poScreenCenter then
SetBounds(Screen.Monitors[I].Left + ((Screen.Monitors[I].Width - Width) div 2),
Screen.Monitors[I].Top + ((Screen.Monitors[I].Height - Height) div 2),
Width, Height)
else
if FPosition = poMainFormCenter then
begin
SetBounds(Screen.Monitors[I].Left + ((Screen.Monitors[I].Width - Width) div 2),
Screen.Monitors[I].Top + ((Screen.Monitors[I].Height - Height) div 2),
Width, Height)
end
else
begin
ALeft := Screen.Monitors[I].Left + Left - Screen.Monitors[J].Left;
if ALeft + Width > Screen.Monitors[I].Left + Screen.Monitors[I].Width then
ALeft := Screen.Monitors[I].Left + Screen.Monitors[I].Width - Width;
ATop := Screen.Monitors[I].Top + Top - Screen.Monitors[J].Top;
if ATop + Height > Screen.Monitors[I].Top + Screen.Monitors[I].Height then
ATop := Screen.Monitors[I].Top + Screen.Monitors[I].Height - Height;
SetBounds(ALeft, ATop, Width, Height);
end;
end;
end;
end;