LoginSignup
1
2

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > モニタ解像度 > Bug > Screen->Monitors[0]のWidthとHeightがおかしくなる

Last updated at Posted at 2017-12-26
動作環境
C++ Builder XE4
Rad Studio 10.2 Tokyo Update 2

c++ builder > form > モニタ解像度の取得 > Screen->Monitors[0]->Width; / ->Height; // Screen->PrimaryMonitor->
を10.2 Tokyoで試していて気付いた不具合。

code

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)
{
// 1. Monitors[]使用
    int width = Screen->Monitors[0]->Width;
    int height = Screen->Monitors[0]->Height;

    String msg = String().sprintf(L"Monitors[0]: W%d x H%d", width, height);
    Memo1->Lines->Add(msg);

// 2. PrimaryMonitor使用
    width = Screen->PrimaryMonitor->Width;
    height = Screen->PrimaryMonitor->Height;
    msg = String().sprintf(L"Primary: W%d x H%d", width, height);
    Memo1->Lines->Add(msg);
}
//---------------------------------------------------------------------------

結果

qiita.png

Monitors[0]の値がおかしくなる条件がある。
上記では二回Button1を押下しているが、二回目の値がおかしい。

不具合発生条件

1. VMware上での再現

VMware上のWindows 7(や10など)で「モニタを循環」によりモニタ数を1から2に変更した時に発生する。

2. マルチモニタでの再現

1つのモニタと接続している状況から2つ目のモニタを接続後に発生する。

想定される状況

モニタが故障直後にMonitors[0]のHeight, Widthにおかしな値が入り、それをもとに位置計算する処理がすべて失敗する。

ソフトを起動した状態でモニタを1台から2台に増やす場合も失敗するかもしれない。

コードでは、値がおかしくならないPrimaryMonitor使用を検討すると良いだろうか。

1
2
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
2