LoginSignup
0
0

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > TImage > 画像を二倍に拡大 + MouseDownで拡大前画像の位置取得

Last updated at Posted at 2017-10-26
動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)

やろうとしていたこと

準備

適当な画像

  • logo.png
    • #include <pngimage.hpp>を使う

code

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 で管理されるコンポーネント
    TImage *Img_logo;
    TButton *B_enlarge;
    void __fastcall FormShow(TObject *Sender);
    void __fastcall B_enlargeClick(TObject *Sender);
    void __fastcall Img_logoMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <pngimage.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------

static int s_image_ratio = 1;
static const int kRadius_image = 50;
static const int kMargin_image = 10;

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    Img_logo->Stretch = true;
    Img_logo->Picture->LoadFromFile(L"logo.png");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_enlargeClick(TObject *Sender)
{
    if (s_image_ratio != 1) {
        return;
    }
    s_image_ratio = 2;
    Img_logo->Width  *= s_image_ratio;
    Img_logo->Height *= s_image_ratio;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Img_logoMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
    int ax = X / s_image_ratio;
    int ay = Y / s_image_ratio;
    int margin = kMargin_image / s_image_ratio;

    bool bfhit = true;

    if (ax < (kRadius_image - margin) ||
        ax > (kRadius_image + margin)) {
        bfhit = false;
    }
    if (ay < (kRadius_image - margin) ||
        ay > (kRadius_image + margin)) {
        bfhit = false;
    }

    String msg;
    if (bfhit) {
        msg = L"You hit the nail on the head: " + IntToStr(ax) + L"," + IntToStr(ay);
    } else {
        msg = L"You're closer than ever before.";
    }
    OutputDebugString(msg.c_str());
}
//---------------------------------------------------------------------------

 実行例

拡大前

qiita.png

デバッグ出力: You hit the nail on the head: 46,42 プロセス Project1.exe (4080)
デバッグ出力: You're closer than ever before. プロセス Project1.exe (4080)
デバッグ出力: You're closer than ever before. プロセス Project1.exe (4080)

拡大後

qiita.png

デバッグ出力: You hit the nail on the head: 51,53 プロセス Project1.exe (4080)
デバッグ出力: You're closer than ever before. プロセス Project1.exe (4080)
デバッグ出力: You're closer than ever before. プロセス Project1.exe (4080)
デバッグ出力: You're closer than ever before. プロセス Project1.exe (4080)

関連情報

フォームとコントロールを動的にサイズ変更するときの考慮事項

日本語むずかしい。。。

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