LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > Mouse > (ソフト内、ソフト外の)マウス座標をTMemoに表示する

Last updated at Posted at 2018-11-11
動作環境
C++ Builder XE4
Windows 7 Pro (32bit)

処理概要

  • マウス座標をTMemoに表示する
  • ソフト内およびソフト外、関係なく表示する

参考

実装

ツールパレットの「Additional」にある「TApplicationEvents」をフォームに追加して実装した。

デザイン
2018-11-11_14h16_38.png

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>
#include <Vcl.AppEvnts.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TMemo *Memo1;
    TApplicationEvents *ApplicationEvents1;
    void __fastcall ApplicationEvents1Idle(TObject *Sender, bool &Done);



private:    // ユーザー宣言
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)
{
    Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationEvents1Idle(TObject *Sender, bool &Done)
{
    static int preX = 0;
    static int preY = 0;
    int curX, curY;

    curX = Mouse->CursorPos.X;
    curY = Mouse->CursorPos.Y;

    if (curX == preX && curY == preY) {
        return;
    }

    preX = curX;
    preY = curY;

    String msg = String().sprintf(L"X:%d, Y:%d", curX, curY);
    Memo1->Lines->Add(msg);
}
//---------------------------------------------------------------------------

動作状況

2018-11-11_14h17_50.png

  • ソフトがアクティブの間はマウス座標が表示される
    • デスクトップをクリックした時点で座標更新はされなくなる? (詳しくは調べてない)
  • ソフトのウィンドウ内は座標の表示更新周期が短い
  • ソフトのウィンドウ外は座標の表示更新周期が長い

自分の目的としては十分なので、上記の差異は今のところ気にしない。

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