C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Runtime.InteropServices; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11 12namespace Transparent 13{ 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 this.TransparencyKey = Color.Red; 20 } 21 override protected void OnMouseDown(MouseEventArgs e) 22 { 23 foreach (System.Diagnostics.Process p in 24 System.Diagnostics.Process.GetProcesses()) 25 { 26 if (p.MainWindowTitle.Equals("無題 - ペイント")) 27 { 28 SendMessage(p.Handle, 0x0201, (IntPtr)0x0001, (IntPtr)MakeLParam(e.X, e.Y)); 29 } 30 } 31 } 32 override protected void OnMouseUp(MouseEventArgs e) 33 { 34 foreach (System.Diagnostics.Process p in 35 System.Diagnostics.Process.GetProcesses()) 36 { 37 if (p.MainWindowTitle.Equals("無題 - ペイント")) 38 { 39 SendMessage(p.Handle, 0x0202, (IntPtr)0, (IntPtr)0); 40 } 41 } 42 } 43 44 protected override void OnMouseMove(MouseEventArgs e) 45 { 46 foreach (System.Diagnostics.Process p in 47 System.Diagnostics.Process.GetProcesses()) 48 { 49 if (p.MainWindowTitle.Equals("無題 - ペイント")) 50 { 51 IntPtr lParam = (IntPtr)MakeLParam(e.X, e.Y); 52 SendMessage(p.Handle, 0x0200, (IntPtr)0, lParam); 53 } 54 } 55 } 56 public static int MakeLParam(int wLow, int wHigh) 57 { 58 return (((short)wHigh << 16) | (wLow & 0xffff)); 59 } 60 [DllImport("user32.dll")] 61 extern static System.IntPtr SendMessage( 62 System.IntPtr hWnd, // 送信先ウィンドウのハンドル 63 System.UInt32 Msg, // メッセージ 64 System.IntPtr wParam, // メッセージの最初のパラメータ 65 System.IntPtr lParam // メッセージの 2 番目のパラメータ 66 ); 67 } 68}
透明ウインドウで受け取ったマウスイベントを透明ウインドウの下に隠れている"無題 - ペイント"にそのままスルーしたいです。
マウスイベントを受け取って、ペイントのウインドウを探すところまでは動作確認済です。
SendMessageがうまくいってないようです。
なぜでしょうか?
補足
いやいや、今回はこれが課題です。
なぜSendMessageがペイントに行かないのかわかりません。
追記
C#
1SendMessage(p.Handle, 0x0201, (IntPtr)0x0001, (IntPtr)MakeLParam(e.X, e.Y)); 2 ↓ 3SendMessage(p.MainWindowHandle, 0x0201, (IntPtr)0x0001, (IntPtr)MakeLParam(e.X, e.Y));
ハンドルが間違っているのかと思い、変えてみましたがやっぱりダメでした。
なぜでしょうか?
追記
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Runtime.InteropServices; 8using System.Text; 9using System.Threading.Tasks; 10using System.Windows.Forms; 11 12namespace Transparent 13{ 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 this.TransparencyKey = Color.Red; 20 } 21 override protected void OnMouseDown(MouseEventArgs e) 22 { 23 foreach (System.Diagnostics.Process p in 24 System.Diagnostics.Process.GetProcesses()) 25 { 26 if (p.MainWindowTitle.Equals("無題 - ペイント") || 27 p.MainWindowTitle.Equals("Form2")) 28 { 29 SendMessage(p.MainWindowHandle, 0x0201, (IntPtr)0x0001, (IntPtr)MakeLParam(e.X, e.Y)); 30 } 31 } 32 } 33 override protected void OnMouseUp(MouseEventArgs e) 34 { 35 foreach (System.Diagnostics.Process p in 36 System.Diagnostics.Process.GetProcesses()) 37 { 38 if (p.MainWindowTitle.Equals("無題 - ペイント") || 39 p.MainWindowTitle.Equals("Form2")) 40 { 41 SendMessage(p.MainWindowHandle, 0x0202, (IntPtr)0, (IntPtr)0); 42 } 43 } 44 } 45 46 protected override void OnMouseMove(MouseEventArgs e) 47 { 48 foreach (System.Diagnostics.Process p in 49 System.Diagnostics.Process.GetProcesses()) 50 { 51 if (p.MainWindowTitle.Equals("無題 - ペイント") || 52 p.MainWindowTitle.Equals("Form2")) 53 { 54 IntPtr lParam = (IntPtr)MakeLParam(e.X, e.Y); 55 SendMessage(p.MainWindowHandle, 0x0200, (IntPtr)0, lParam); 56 } 57 } 58 } 59 public static int MakeLParam(int wLow, int wHigh) 60 { 61 return (((short)wHigh << 16) | (wLow & 0xffff)); 62 } 63 [DllImport("user32.dll")] 64 extern static System.IntPtr SendMessage( 65 System.IntPtr hWnd, // 送信先ウィンドウのハンドル 66 System.UInt32 Msg, // メッセージ 67 System.IntPtr wParam, // メッセージの最初のパラメータ 68 System.IntPtr lParam // メッセージの 2 番目のパラメータ 69 ); 70 } 71}
えっと、透明ウインドウをもう一つ作って、もう一つの透明ウインドウ上でマウスをクリックしたら、ちゃんとOnMouseDownの中に入って来ました。なぜペイントにメッセージが渡らないのでしょうか?
p.MainWindowHandleも間違い?
ペイントのクライアント領域のウインドウハンドルを指定しないとダメ?
追記
子ウインドウを拾ってもダメだ
C#
1using System; 2using System.Drawing; 3using System.Runtime.InteropServices; 4using System.Windows.Forms; 5 6namespace Transparent 7{ 8 public partial class Form1 : Form 9 { 10 private const int SWP_NOSIZE = 0x0001; 11 private const int SWP_NOMOVE = 0x0002; 12 private const int SWP_SHOWWINDOW = 0x0040; 13 14 private const int HWND_TOPMOST = -1; 15 private const int HWND_NOTOPMOST = -2; 16 17 static bool isTop = false; 18 public Form1() 19 { 20 InitializeComponent(); 21 this.TransparencyKey = Color.Red; 22 StartTimer(); 23 } 24 private Timer _timer = null; 25 26 private void StartTimer() 27 { 28 Timer timer = new Timer(); 29 timer.Tick += new EventHandler(TickHandler); 30 timer.Interval = 2000; 31 timer.Start(); 32 _timer = timer; 33 } 34 35 private void StopTimer() 36 { 37 if (_timer == null) 38 { 39 return; 40 } 41 _timer.Stop(); 42 _timer = null; 43 } 44 45 private void TickHandler(object sender, EventArgs e) 46 { 47 foreach (System.Diagnostics.Process p in 48 System.Diagnostics.Process.GetProcesses()) 49 { 50 if (p.MainWindowTitle.Equals("無題 - ペイント")) 51 { 52 if (isTop) 53 { 54 isTop = false; 55 this.TopMost = false; 56 SetWindowPos(p.MainWindowHandle, HWND_TOPMOST, 0, 0, 0, 0, 57 SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE); 58 SetWindowPos(p.MainWindowHandle, HWND_NOTOPMOST, 0, 0, 0, 0, 59 SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE); 60 } 61 else 62 { 63 isTop = true; 64 this.TopMost = true; 65 } 66 } 67 } 68 } 69 [DllImport("user32.dll", SetLastError = true)] 70 private static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab); 71 [DllImport("user32.dll", SetLastError = true)] 72 [return: MarshalAs(UnmanagedType.Bool)] 73 private static extern bool SetWindowPos(IntPtr hWnd, 74 int hWndInsertAfter, int x, int y, int cx, int cy, int uFlags); 75 } 76}
今のところ、これが限界。
もっといい方法はありませんか?
こちらの質問が複数のユーザーから「過去の低評価」という指摘を受けました。
回答1件
あなたの回答
tips
プレビュー