我用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);
        }
    }
}

解决方案 »

  1.   

    你的代码有点问题
    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;
      }
      

  2.   

    用SPY++查出千千播放的句柄,然后发消息 就可以了,可以发鼠标,也可以发BUTTON的CLICK消息
      

  3.   

    http://msdn.microsoft.com/zh-cn/library/cc240584
      

  4.   

    是不是MainWindowHandle只是主窗口句柄,而千千静听等接受消息的不是主窗口控件吗?SPY++可以知道程序中接受消息的控件的句柄吗?