我要用程序往后台窗口的写字板发送6的按键信息,但不成功,代码如下,请高手赐教.
一定要用发送键盘信息的方式,这只是个例子,我想做个类似能往后台发键盘和MOUSE信息的按键精灵.谢谢了using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Win32;
using System.Diagnostics;
namespace EasyZT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }       // [DllImport("User32.dll", EntryPoint = "FindWindow")]        private void button1_Click(object sender, EventArgs e)
        {
            int retval = 0; //增加一个返回值用来判断操作是否成功 
            IntPtr ParenthWnd = new IntPtr(0);
            IntPtr EdithWnd = new IntPtr(0);
            const int WM_KEYDOWN = 0x100;
            const int WM_KEYUP = 0x101;            foreach (Process p in Process.GetProcesses(System.Environment.MachineName))
            {
                if (p.MainWindowHandle != IntPtr.Zero)
                {
                    //显示用户程序名 
                    textBox3.Text = textBox3.Text + "\n "+(p); // string s = p.ToString();  
                }
            }            //ParenthWnd = Win32.FindWindow(null, this.textBox1.Text);
            //MessageBox.Show(this.textBox1.Text);
            ParenthWnd = Win32.FindWindow( null,"无标题 - 记事本");
            //ParenthWnd = Win32.FindWindow(null,"notepad");
            this.textBox2.Text = ParenthWnd.ToString();
            
            Win32.SendMessage(ParenthWnd, WM_KEYDOWN, (IntPtr)31, "0");
            Win32.SendMessage(ParenthWnd, WM_KEYUP, (IntPtr)31, "0");
        }
    }    public class Win32
    {
        public delegate bool CallBack(int hwnd, int lParam);         [DllImport("User32.Dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);        [DllImport("User32.Dll")]
        public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);        [DllImport("User32.Dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);        [DllImport("user32")]
        public static extern int EnumWindows(CallBack x, int y);
    }
}

解决方案 »

  1.   

    KAO,这里的论坛都没人回贴子啊?是太简单了???
      

  2.   

    IntPtr b = Win32.FindWindowEx(ParenthWnd, IntPtr.Zero, "Edit", null);
    Win32.SendMessage(b, WM_CHAR, (IntPtr)49/*   数字键1   */, "0");   
    测试过是可以成功,写入1
    如果你试不成功的话,用sky++检查一下句柄,会不会是句柄没找到
      

  3.   

    hooo:我用你这种方式测试好像也不行,我需要使用 WM_KEYDOWN,WM_KEYUP的方式发送信息.
    能否把你测试成功的例子发一份给我,最好是可以直接在vs2005上编译执行的项目文件?
    我的邮件是:[email protected]谢谢!
      

  4.   

    获取句柄:
     ParenthWnd = Win32.FindWindow("notepad","无标题 - 记事本");
    你的标题好像错了
    可用
    ParenthWnd = Win32.FindWindow("notepad",null);
    StringBuilder title = new StringBuilder();
    int r =  Win32.GetWindowText(ParenthWnd,    title, 100);
    读取下 
    写入文本:用 WM_SETTEXT消息
    或者
    Win32.SetForegroundWindow(ParenthWnd);
    SendKeys.Send("my text");
    SendKeys.Send("{ENTER}");WM_KeyDown好像不行。