動作環境
C++ Builder XE4
関連
- c++ builder > form > モニタ解像度の取得 > Screen->Monitors[0]->Width; / ->Height; // Screen->PrimaryMonitor->
- C++ Builder XE4, 10.2 Tokyo > モニタ解像度 > Bug > Screen->Monitors[0]のWidthとHeightがおかしくなる
- UI > C++ Builder XE4 > プロパティposition > マルチモニタでの実行時 > poDesktopCenter: モニタにわたって表示 | poScreenCenter: 一つのモニタの中央に表示
概要
- 二台のモニタで実行
- 左右に並べた状態
- サイズが異なる
- それぞれのサイズに対して画面分割をしたい
- そのため、どちらのモニタにいるかを知りたい
LeftとWidthから検知することにした
実装 v0.1
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE で管理されるコンポーネント
TMemo *Memo1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // ユーザー宣言
public: // ユーザー宣言
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String msg;
int left = Screen->Monitors[0]->Left;
int width = Screen->Monitors[0]->Width;
if (this->Left >= left && this->Left < (left + width) ) {
msg = L"Monitors[0]";
} else {
msg = L"Monitors[1]";
}
Memo1->Lines->Add(msg);
}
//---------------------------------------------------------------------------
動作例
モニタを行き来しながらButton1を押下した。
備考
「どちらのモニタにいるか」を知るプロパティがあってもいいような気もするが、見つけ出せていない。
TScreen
TMonitor.Primary
これは「どちらのモニタをプライマリにしているか」を知るためのプロパティなのだろう。
失敗する場合
モニタ二台を「縦に」配置している場合は上記の実装では失敗する。
その場合、水平方向と垂直方向の両方で確認する必要がある。
実装 v0.2
水平と垂直の両方で確認するように変更した。
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String msg;
int left = Screen->Monitors[0]->Left;
int width = Screen->Monitors[0]->Width;
int top = Screen->Monitors[0]->Top;
int height = Screen->Monitors[0]->Height;
if (this->Left >= left && this->Left < (left + width) &&
this->Top >= top && this->Top < (top + height)) {
msg = L"Monitors[0]";
} else {
msg = L"Monitors[1]";
}
Memo1->Lines->Add(msg);
}
//---------------------------------------------------------------------------