LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > Bug? > TStreamMemory > WriteCopmponent() > TShape->Brush->Color = clWhiteの時だけ、色情報をコピーできない > clWhiteがデフォルトColorであるため

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

前回

不可解な動作

上記の実装を使っていたが、不可解な動作となる

  • コピー元のTShapeがBrush->Color = clWhiteの時、色情報がコピーできない

実装

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)
{
    // Shape1は白に、他は任意の色
    Shape1->Brush->Color = clWhite;
    Shape2->Brush->Color = clAqua;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    copyProperties((TControl *)Shape1, (TControl *)Shape2);

    // 重なっているため
    Shape2->Left = Shape1->Left + 150;
}

void __fastcall TForm1::copyProperties(TControl *srcCtrl, TControl *dstCtrl)
{
    // 元の名前を保持
    String orgName_src = srcCtrl->Name;
    String orgName_dst = dstCtrl->Name;

    // プロパティコピー
    TMemoryStream *strm = new TMemoryStream;
    Shape1->Name = L"";  // to avoid source collision
    try {
        strm->WriteComponent(srcCtrl);
        strm->Position = 0;
        strm->ReadComponent(dstCtrl);
    }
    __finally
    {
        delete strm;
    }

    srcCtrl->Name = orgName_src;
    dstCtrl->Name = orgName_dst;
}

動作例

ソフト起動直後
2018-12-26_19h35_33.png

Button1押下後
2018-12-26_19h35_48.png

サイズに関するプロパティはコピーできたが、Brush->Color(=clWhite)はコピーできていない。

IDEのバグのようだ。

Shape1のBrush->ColorがclWhite以外の時はColorもコピーされる。

対処

以下のように、Colorをコピーしなおす処理が必要だろう。

    copyProperties((TControl *)Shape1, (TControl *)Shape2);
    Shape2->Brush->Color = Shape1->Brush->Color;

10.2 Tokyoでも同じ実装を確認したところ、再現した。

StackOverflow

StackOverflowにて質問をしている。

回答が付いたらここにも記載する予定。

2018-12-28
回答をいただいた。

clWhiteがdefaultカラーであるための挙動であるとのこと。
RTTIによるコピー方法のコード例も合わせて教えていただいた。

I appreciate it, Remy Lebeau.

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