c#在listview中选择进程并向其发送sendMessage

时间:2016-02-29 09:37:12

标签: c# listview process

所以我制作了一个进程的列表视图,现在我想让它必须在其中选择一个进程,然后向它发送一条消息(在我的情况下是记事本)。

如何将记事本流程声明分配给选定的列表视图项?

以下是我现在所拥有的:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Management;


namespace sendMessage
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [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);



        public void DisplayProcesses() {
            try
            {
                Process[] processList = Process.GetProcesses();

                foreach (Process proc in processList)
                {
                    ListViewItem item = new ListViewItem(proc.ProcessName);
                    item.SubItems.Add(proc.ProcessName);
                    item.Tag = proc;
                    listView1.Items.Add(item);
                }
            }
            catch(Exception ex) {
                Console.WriteLine(ex);
            }
        }

        private static void DoSendMessage(string message)
        {
            Process notepad = new Process();
            notepad.ProcessName=

            if (notepad != null)
            {
                IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
                SendMessage(child, 0x000C, 0, message);
            }
            else {
                MessageBox.Show("No process...");
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(textBox1.Text))
            {
                MessageBox.Show("TextBox is empty");
            }
            else {
                DoSendMessage(textBox1.Text);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            DisplayProcesses();
        }
    }
}

0 个答案:

没有答案