LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > 他ソフトのボタンをクリックする (座標固定版)

Last updated at Posted at 2016-11-25
動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2017/12/28)

他のソフトのボタンをクリックしたい。
座標に基づく方法を実装してみた。

参考 http://delphi.xcjc.net/viewthread.php?tid=48990

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::App_clickButton(String fullCaption)
{
    HWND appHwnd;

    appHwnd = FindWindowEx(NULL, NULL, NULL, fullCaption.c_str());
    if (appHwnd == NULL) {
        return;
    }

    SetForegroundWindow(appHwnd);

        // ソフトのウィンドウを最大化することで、ボタン位置を同じ場所に来るようにする
    keybd_event(VK_LWIN, 0, 0, 0);
    keybd_event(VK_UP, 0,0,0);
    keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0); // これがないと押したままになる

        // 左クリック
    SetCursorPos(200, 100);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0,0,0,0);
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // これがないと押したままになる
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    App_clickButton(L"TargetSoft");
}

"TargetSoft"というキャプションのソフトに対して、座標(200,100)にあるボタンをクリックできた。

TargetSoftにおいては、最大化をした時のボタンの位置が常に(200,100)に来るような実装になっているという前提がある。

TargetSoftのボタン配置が変わったり、OSの変更でメニューバーの表示状況が変わったりした場合にボタンの位置がずれると、動作しなくなる可能性がある。

ショートカットキーが用意されているソフトの場合はショートカットキーを使う方が良いだろう。

関連

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