C++ Builder 10.2 Tokyo > color > UI > 색상 선택 + RGB 값 입력 [복수 세트 버전] > Tag: 조작 대상 컴포넌트 저장 및 사용

운영 환경
RAD Studio 10.2 Tokyo Update 3

개요


  • 아래의 UI를 준비했다
  • A. TColorBox에서 색상 선택
  • B. RGB 값의 직접 입력


  • 이전 버전


  • C++ Builder 10.2 Tokyo > color > UI > 색 선택 + RGB 값 입력 [한 세트 버전]

  • 상기는 「하나의 세트」의 실장.
    이것을 "복수 세트"로 확장했습니다.

    Tag : 조작 대상 컴포넌트 저장 및 사용



    코드를 공통화하기 위해 Tag 속성을 사용합니다.
  • Tag 프로퍼티에 조작 대처의 컴퍼넌트의 포인터를 격납
  • Tag 프로퍼티로부터 포인터를 취득해, 그것을 조작합니다.

    Unit1



    Unit1.h
    //---------------------------------------------------------------------------
    
    #ifndef Unit1H
    #define Unit1H
    //---------------------------------------------------------------------------
    #include <System.Classes.hpp>
    #include <Vcl.Controls.hpp>
    #include <Vcl.StdCtrls.hpp>
    #include <Vcl.Forms.hpp>
    #include <Vcl.ExtCtrls.hpp>
    //---------------------------------------------------------------------------
    class TForm1 : public TForm
    {
    __published:    // IDE で管理されるコンポーネント
        TButton *B_select1;
        TShape *Shape1;
        TEdit *E_RGBvalue1;
        TLabel *Label1;
        TButton *B_select2;
        TShape *Shape2;
        TEdit *E_RGBvalue2;
        TLabel *Label2;
        void __fastcall B_selectXClick(TObject *Sender);
        void __fastcall FormShow(TObject *Sender);
        void __fastcall E_RGBvalueXExit(TObject *Sender);
        void __fastcall E_RGBvalueXKeyDown(TObject *Sender, WORD &Key, TShiftState Shift);
    
    
    private:    // ユーザー宣言
    public:     // ユーザー宣言
        __fastcall TForm1(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
    extern PACKAGE TForm1 *Form1;
    //---------------------------------------------------------------------------
    #endif
    
    

    Unit1.cpp
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    #include "Unit2.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TForm1 *Form1;
    //---------------------------------------------------------------------------
    
    /*
    Note:
    Tagプロパティにそのコンポーネントが操作するコンポーネントのポインタを格納する
    共通実装において、そのポインタを使って操作を行う
    */
    
    __fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
    {
    }
    void __fastcall TForm1::FormShow(TObject *Sender)
    {
        E_RGBvalue1->MaxLength = 6; // R,G,B値それぞれ00..FF
        E_RGBvalue1->Text = L"FFFFFF";
        E_RGBvalue2->MaxLength = 6; // R,G,B値それぞれ00..FF
        E_RGBvalue2->Text = L"FFFFFF";
    
        // Tagを使ったコンポーネント操作用
        //   1用
        E_RGBvalue1->Tag = (NativeInt)Shape1;
        B_select1->Tag = (NativeInt)E_RGBvalue1;
        //   2用
        E_RGBvalue2->Tag = (NativeInt)Shape2;
        B_select2->Tag = (NativeInt)E_RGBvalue2;
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::B_selectXClick(TObject *Sender)  // 共通実装
    {
        Form2->ShowModal();
        if (Form2->ModalResult != mrOk) {
            return;
        }
    
        TColor acol = Form2->ColorBox1->Selected;
        int Rval = GetRValue(acol);
        int Gval = GetGValue(acol);
        int Bval = GetBValue(acol);
    
        TButton *btPtr = (TButton *)Sender;
        TEdit *edPtr = (TEdit *)btPtr->Tag;
        if (edPtr != NULL) {
            edPtr->Text = String().sprintf(L"%02X%02X%02X", Rval, Gval, Bval);
            edPtr->OnExit(edPtr);
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::E_RGBvalueXExit(TObject *Sender)  // 共通実装
    {
        TEdit *edPtr = (TEdit *)Sender;
    
        // char型を扱うため、ここでAnsiStringにする必要がある (Stringだとwchar_tになる)
        AnsiString astr = AnsiString(edPtr->Text);   // e.g. FFFF00
    
        int tmp;
        int Red, Green, Blue;
        char *endptr;
    
        AnsiString wrk = astr.SubString(1, 6);
        tmp = strtol(wrk.c_str(), &endptr, 16);
    
        if (*endptr == 0x00) {
            Red   = strtol(astr.SubString(1, 2).c_str(), /*endptr=*/NULL, 16);
            Green = strtol(astr.SubString(3, 2).c_str(), /*endptr=*/NULL, 16);
            Blue  = strtol(astr.SubString(5, 2).c_str(), /*endptr=*/NULL, 16);
        } else {
            Red = 255;
            Green = 255;
            Blue = 255;
            edPtr->Text = L"FFFFFF";
        }
    
        TShape *shPtr = (TShape *)edPtr->Tag;
        if (shPtr != NULL) {
            shPtr->Brush->Color = (TColor)RGB(Red, Green, Blue);
            shPtr->Pen->Color = (TColor)RGB(Red, Green, Blue);
        }
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::E_RGBvalueXKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)  // 共通実装
    {
        if (Key == VK_RETURN) {
            TEdit *edPtr = (TEdit *)Sender;
            edPtr->OnExit(Sender);
        }
    }
    //---------------------------------------------------------------------------
    
    

    이벤트 설정


  • B_selectXClick
  • B_select1의 OnClick
  • B_select2의 OnClick

  • E_RGBvalueXExit
  • E_RGBValue1의 OnExit
  • E_RGBValue2의 OnExit

  • E_RGBValueXKeyDown
  • E_RGBValue1의 OnKeyDown
  • E_RGBValue1의 OnKeyDown


  • 실행 예



    각각의 버튼과 TEdit로 색을 변경할 수 있게 되었다.



    기타 코드



    Unit2



    다음과 같은 .h, .cpp 파일.
  • C++ Builder 10.2 Tokyo > color > UI > 색 선택 + RGB 값 입력 [한 세트 버전]
  • 좋은 웹페이지 즐겨찾기