動作環境
C++ Builder XE4
Windows 7 Pro (32bit)
処理概要
- マウス座標をTMemoに表示する
- ソフト内およびソフト外、関係なく表示する
参考
実装
ツールパレットの「Additional」にある「TApplicationEvents」をフォームに追加して実装した。
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);
}
//---------------------------------------------------------------------------
動作状況
- ソフトがアクティブの間はマウス座標が表示される
- デスクトップをクリックした時点で座標更新はされなくなる? (詳しくは調べてない)
- ソフトのウィンドウ内は座標の表示更新周期が短い
- ソフトのウィンドウ外は座標の表示更新周期が長い
自分の目的としては十分なので、上記の差異は今のところ気にしない。