LoginSignup
0
0

More than 5 years have passed since last update.

C++ Builder XE4 > UI > 実装: [マウスクリック]と[マウスでの範囲選択]を判別する

Last updated at Posted at 2018-12-10
動作環境
C++ Builder XE4

概要

  • マウスクリック
    • 左クリック
    • クリックを離す
  • マウスでの範囲選択
    • 左クリック
    • 移動
    • クリックを離す

上記を判別する実装を考えた。

実装

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 で管理されるコンポーネント
    TPanel *Panel1;
    TMemo *Memo1;
    void __fastcall Panel1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y);
    void __fastcall Panel1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y);
    void __fastcall Panel1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y);

private:    // ユーザー宣言
    bool m_bfMouseMove; // マウス移動があったかどうか
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

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::Panel1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
    if (m_bfMouseMove == false) {
        Memo1->Lines->Add(L"Click");
    } else {
        Memo1->Lines->Add(L"Range Select");
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
    m_bfMouseMove = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Panel1MouseMove(TObject *Sender, TShiftState Shift, int X,
          int Y)
{
    m_bfMouseMove = true;
}
//---------------------------------------------------------------------------

動作例

2018-12-10_15h15_36.png

備考1

「X,Y位置を保持してMouseUp時にずれがないか確認」という案も考えたが、上の実装の方がシンプルだろう。

Panel1MouseUp()のみを使う実装があれば知りたい。

備考2

左クリック時に少しでも座標が動くとクリックと認識されない。
「クリックしたの処理されない」とユーザに思われる場面がありそう。

座標の移動量を見て、多少の移動であればクリックとして処理する、などの工夫は必要かもしれない。

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