C++ Builder 10.2 Tokyo > color > UI > 색상 선택 + RGB 값 입력 [복수 세트 버전] > Tag: 조작 대상 컴포넌트 저장 및 사용
19307 단어 cppBuilder우이colorrgb태그
RAD Studio 10.2 Tokyo Update 3
개요
이전 버전
상기는 「하나의 세트」의 실장.
이것을 "복수 세트"로 확장했습니다.
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);
}
}
//---------------------------------------------------------------------------
이벤트 설정
실행 예
각각의 버튼과 TEdit로 색을 변경할 수 있게 되었다.
기타 코드
Unit2
다음과 같은 .h, .cpp 파일.
Reference
이 문제에 관하여(C++ Builder 10.2 Tokyo > color > UI > 색상 선택 + RGB 값 입력 [복수 세트 버전] > Tag: 조작 대상 컴포넌트 저장 및 사용), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/7of9/items/23557b3d02bf9c751eb2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)