| くま さま
KOZ さま
アドバイスありがとうございます。
最低限のコードのサンプルをつくってみては? とのことなので、作ってみたところ…
期待通り
(トリガーとなるボタンを押したら、
メモ帳にマウスカーソルが移動してクリック、
その後マウスカーソル消す)
の動作しちゃいました。
しかし、今度はメモ帳から戻ってきたはずのマウスポインタが消えたままになってしまいました。
(マウスカーソルがボタンに進入すると色が変わるのでそこにいる事は間違いないのですが)
何が間違っているのかよくわかりません。
更なるアドバイスを頂けませんでしょうか?
下記がそのサンプルです。
開発環境はVisual Studio 2017
言語はc#
プロジェクトのタイプは「Windowsフォームアプリケーション(.NET Framework)」で、
フォームにボタンを1つ作っただけです。
using System;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
//DLL使用するためのおまじない
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Process[] notepads = Process.GetProcessesByName("notepad");
if (notepads.Length == 0) return;
if (notepads[0] != null)
{
//マウスカーソル現在位置を保存
int Old_xPos = System.Windows.Forms.Cursor.Position.X;
int Old_yPos = System.Windows.Forms.Cursor.Position.Y;
//メモ帳の位置から新しいマウスカーソル位置を決定
RECT WindowRect;
GetWindowRect(notepads[0].MainWindowHandle, out WindowRect);
int New_xPos = WindowRect.Left+10;
int New_yPos = WindowRect.Top+10;
//まずは現在のマウスカーソルを非表示
Cursor.Hide();
//すぐに移動しちゃうとよくわからないから3秒weitします
Thread.Sleep(3000);
//マウスカーソルを新しい位置に移動
SetCursorPos(New_xPos, New_yPos);
//マウスクリック(なんかこの時点で勝手にマウスカーソルが表示されちゃう)
mouse_event(0x2, 0, 0, 0, 0);
mouse_event(0x4, 0, 0, 0, 0);
//すぐに移動しちゃうとよくわからないから3秒weitします
Thread.Sleep(3000);
//元の場所に戻るからメモ帳上のマウスカーソルを非表示
Cursor.Hide();
//マウスカーソルを元の位置に移動
SetCursorPos(Old_xPos, Old_yPos);
//メモ帳上でクリックしたからそっちがアクティブなので自フォームをアクティブにする
this.Activate();
//元の場所のマウスカーソルを表示
Cursor.Show();//←←←これが動作していない
}
}
}
}
|