ClickOnesでパラメータを渡すとエラーになった。
セキュリティーでガードがかかっているので、上記CheckBOXをチェック
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.Runtime.InteropServices; // setforegraoundwindow namespace SendKeyType { public partial class Form1 : Form { public Form1() { InitializeComponent(); } // http://www.atmarkit.co.jp/fdotnet/dotnettips/024w32api/w32api.html [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private void button2_Click(object sender, EventArgs e) { // 選択しているプロセスをアクティブ int pid = int.Parse(comboBox1.SelectedValue.ToString()); Process p = Process.GetProcessById(pid); SetForegroundWindow(p.MainWindowHandle); // キーストロークを送信 SendKeys.Send(textBox1.Text); } private void button3_Click(object sender, EventArgs e) { // プロセス一覧を更新 comboBox1.DataSource = ProcessTable(); comboBox1.ValueMember = "PID"; comboBox1.DisplayMember = "NAME"; } private DataTable ProcessTable() { // プロセスのリストを取得 // http://d.hatena.ne.jp/tomoemon/20080430/p2 Process[] ps = Process.GetProcesses(); Array.Sort(ps, new ProcComparator()); DataTable table = new DataTable(); table.Columns.Add("PID"); table.Columns.Add("NAME"); foreach (Process p in ps) { DataRow row = table.NewRow(); row.SetField<int>("PID", p.Id); row.SetField<string>("NAME", p.ProcessName + " - " + p.MainWindowTitle); table.Rows.Add(row); } table.AcceptChanges(); return table; } // ..... } // プロセス名でソート ... for Array.Sort public class ProcComparator : IComparer<Process> { public int Compare(Process p, Process q) { return p.ProcessName.CompareTo(q.ProcessName); } }
元ネタ
http://d.hatena.ne.jp/yatt/20110402/1301731512