C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

No.98623 の関連記事表示

<< 0 >>
■98623  Re[3]: 他のアプリ上にあるマウスポインタを消去する方法
□投稿者/ Tom -(2021/12/08(Wed) 01:28:14)
    くま さま
    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();//←←←これが動作していない
                }
            }
        }
    }
    
    
記事No.98588 のレス /過去ログ171より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -