LoginSignup
1
0

More than 5 years have passed since last update.

C++ Builder XE4 > TScreen > マルチモニタ > 二台のモニタでどちらのモニタにいるかを知る

Last updated at Posted at 2018-12-12
動作環境
C++ Builder XE4

関連

概要

  • 二台のモニタで実行
    • 左右に並べた状態
  • サイズが異なる
  • それぞれのサイズに対して画面分割をしたい
  • そのため、どちらのモニタにいるかを知りたい

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を押下した。

2018-12-12_17h31_55.png

備考

「どちらのモニタにいるか」を知るプロパティがあってもいいような気もするが、見つけ出せていない。

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);

}
//---------------------------------------------------------------------------
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0