如何实现在一个程序中把主窗体和子窗体分别显示在不同的显示器中? ( 积分: 100 )

  • 主题发起人 swangbit
  • 开始时间
S

swangbit

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做一个应用程序,使用BCB控制子窗体实现双屏显示。我按照论坛的提示,做了一个例子,但只能使主窗体和子窗体都显示在同一个显示器上,代码如下,请高人帮我指点指点:
//---------------------------------------------------------------------------
/*使用该方法可以是两个form都显示在副显示器上,如果去除蓝色部分,则两个窗体都显示在主显示器上,但我想实现的是form1显示在主显示器上,form2显示在副显示器上*/
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"

//---------------------------------------------------------------------------
USEFORM("Unit1.cpp", Form1);
USEFORM("Unit2.cpp", Form2);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->CreateForm(__classid(TForm2), &Form2);
[blue]int mleft,mtop;
if(Form2->GetWorkArea(&mleft,&mtop))
{
Form2->Left=mleft;
Form2->Top=mtop;
Form2->Show();
Sleep(3000);
}[/blue]
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//获取第二显示器的工作区域
bool TForm1::GetWorkArea(int *mleft,int *mtop)
{
TRect R;
for (int i=0;i<Screen->MonitorCount;i++)
{
if (!Screen->Monitors->Primary)
{
R= Screen->Monitors->WorkareaRect;
*mleft=R.Left;
*mtop=R.Top;
return true;
}
}
return false;
}
//-----------------------------------------------------------------
//显示form2,form2是一个简单窗体
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int mleft,mtop;
if(GetWorkArea(&mleft,&mtop))
{
Form2->Left=mleft;
 
S

swangbit

Unregistered / Unconfirmed
GUEST, unregistred user!
我想做一个应用程序,使用BCB控制子窗体实现双屏显示。我按照论坛的提示,做了一个例子,但只能使主窗体和子窗体都显示在同一个显示器上,代码如下,请高人帮我指点指点:
//---------------------------------------------------------------------------
/*使用该方法可以是两个form都显示在副显示器上,如果去除蓝色部分,则两个窗体都显示在主显示器上,但我想实现的是form1显示在主显示器上,form2显示在副显示器上*/
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"

//---------------------------------------------------------------------------
USEFORM("Unit1.cpp", Form1);
USEFORM("Unit2.cpp", Form2);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->CreateForm(__classid(TForm2), &Form2);
[blue]int mleft,mtop;
if(Form2->GetWorkArea(&mleft,&mtop))
{
Form2->Left=mleft;
Form2->Top=mtop;
Form2->Show();
Sleep(3000);
}[/blue]
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
//获取第二显示器的工作区域
bool TForm1::GetWorkArea(int *mleft,int *mtop)
{
TRect R;
for (int i=0;i<Screen->MonitorCount;i++)
{
if (!Screen->Monitors->Primary)
{
R= Screen->Monitors->WorkareaRect;
*mleft=R.Left;
*mtop=R.Top;
return true;
}
}
return false;
}
//-----------------------------------------------------------------
//显示form2,form2是一个简单窗体
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int mleft,mtop;
if(GetWorkArea(&mleft,&mtop))
{
Form2->Left=mleft;
 
P

power2010

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI中,窗体的DefaultMonitor这个属性可以控制由那个显示器来显示
 

寻路

Unregistered / Unconfirmed
GUEST, unregistred user!
Provides access to the monitor on which the form appears.
property Monitor: TMonitor;
Description
Use Monitor to access information about the monitor on which
the form appears if the application runs on a multi-monitor system.
The monitor is determined by the DefaultMonitor property.

Specifies the monitor on which the form appears.
type TDefaultMonitor = (dmDesktop, dmPrimary, dmMainForm, dmActiveForm);
property DefaultMonitor: TDefaultMonitor;
Description
Use DefaultMonitor to associate a form with a particular monitor in
a multi-monitor application.

开发时注意使用ScreenCenter,和DesktopCenter
的不同,你只要试一下,立刻就会明白的
 
S

swangbit

Unregistered / Unconfirmed
GUEST, unregistred user!

Similar threads

顶部