Hello Everyone ,My motive is to control visual studio by voice commands by using microsoft speech services.
I want to control visual studio from my another window form app in c#. I have a little bit success but there is some problem in SendKeys to the application.
As i have open two instances of visual studio. when i run my program from visual studio , it works fine. i am giving Ctrl+N command (^n)by using send keys to add new File Dialog box. On run my program, the control goes to another instance and open add new file dialog box . it works well .
But when i run exe directly from bin folder, it does not send keys to Visual studio instance. Exe Finds well of window of visual studio but not open file Dialog box. There is some problem for setForgroundWindow(handel) method.
What I have tried:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ControlVs
{
public partial class Form1 : Form
{
IntPtr handle = new IntPtr(0);
[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
handle = FindWindow(@"HwndWrapper[DefaultDomain;;f515415e-f7ae-41b2-b5fd-11635f9b28a9]", null);
ShowWindow(handle, 5);
label1.Text = Convert.ToString(handle);
if (SetForegroundWindow(handle))
{
System.Threading.Thread.Sleep(1000);
SendKeys.SendWait(@"^{n}");
}
}
}
}
namespace ControlVs
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(108, 97);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(382, 106);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(18, 13);
this.label1.TabIndex = 1;
this.label1.Text = "HI";
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
}
}