PostMessage传递消息给窗口无反应

realmagician 2012-03-19 12:41:58
我用PostMessage给一个播放器发送了一个“空格”消息,返回值是1,说明却是发送了,但是播放器依然播放没有暂停。
然后我自己写了一个响应空格消息的小程序,并用PostMessage给它发送消息,是有效的。请问该如何让播放器也响应PostMessage发出的消息啊?
以下是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//后来添加的
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace Test_control
{
class Program
{
static void Main(string[] args)
{
SendMsg sm = new SendMsg();
int cmond=0;
String input;
for (; ; )
{
input = Console.ReadLine();
cmond = Convert.ToInt16(input);
if (cmond == 0)
break;
else
{
sm.StartOrStop();
}
}
}
}

public class SendMsg
{
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(
IntPtr hwnd,
int wMsg,
uint wParam,
uint lParam
);

IntPtr mainWindowHander;

public SendMsg()
{
Process[] allProcesses = Process.GetProcesses();

foreach(Process p in allProcesses)
{
if (p.ProcessName == "PotPlayerMini")
{
mainWindowHander = p.MainWindowHandle;
Console.WriteLine(p.ProcessName);

}
}
//如果没找到,即程序没有启动

if(mainWindowHander == IntPtr.Zero)
{
Console.WriteLine("not open");
Process p = new Process();
p.StartInfo.FileName = @"D:\SoftWare\PotPlayer 1.5.28069_wwwo\Potplayer\PotPlayerMini.exe";
p.Start();
Thread.Sleep(1000);
mainWindowHander = p.MainWindowHandle;
}
}
public void StartOrStop()
{
PostMessage(mainWindowHander, 0x0100, 0x20, 0x20390001);
PostMessage(mainWindowHander, 0x0101, 0x20, 0x20390001);
}
}
}
...全文
946 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
realmagician 2012-03-23
  • 打赏
  • 举报
回复
问题解决了,果然是程序多窗口的问题,没有获得接受消息的控件的句柄
莫忘初心svip 2012-03-19
  • 打赏
  • 举报
回复
你的代码有点问题
if(mainWindowHander == IntPtr.Zero)
{
Console.WriteLine("not open");
Process p = new Process();
p.StartInfo.FileName = @"D:\SoftWare\PotPlayer 1.5.28069_wwwo\Potplayer\PotPlayerMini.exe";
p.Start();
Thread.Sleep(1000);
加一个while 不然p.MainWindowHandle会等于零
mainWindowHander = p.MainWindowHandle;
}
realmagician 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tebiefu 的回复:]

用SPY++查出千千播放的句柄,然后发消息 就可以了,可以发鼠标,也可以发BUTTON的CLICK消息
[/Quote]
是不是MainWindowHandle只是主窗口句柄,而千千静听等接受消息的不是主窗口控件吗?SPY++可以知道程序中接受消息的控件的句柄吗?
莫忘初心svip 2012-03-19
  • 打赏
  • 举报
回复
用SPY++查出千千播放的句柄,然后发消息 就可以了,可以发鼠标,也可以发BUTTON的CLICK消息
兔子-顾问 2012-03-19
  • 打赏
  • 举报
回复
用SendInput或是keyboard_event
realmagician 2012-03-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tebiefu 的回复:]

你的代码有点问题
if(mainWindowHander == IntPtr.Zero)
{
Console.WriteLine("not open");
Process p = new Process();
p.StartInfo.FileName = @"D:\SoftWare\PotPlayer 1.5.28069_wwwo\Potplayer\PotPlayer……
[/Quote]

应该不是这个问题,我试了自己写的程序是可以实现的。。。但是一般的窗体程序就不行了。视频播放器不行,千千静听也不行

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧