This will explain how to close the notepad using C#.net application
Firstly Add Reference of System.Runtime.InteropServices. using System.Runtime.InteropServices; import these DLLs [DllImport("user32.dll")] public static extern int FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
FormDesigner code::
namespace cLoseotherWindow { partial class Form1 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null;
/// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
/// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(166, 134); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Close"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(141, 35); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 1; this.textBox1.Text = "Notepad"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(141, 85); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 20); this.textBox2.TabIndex = 2; this.textBox2.Text = "Untitled - Notepad"; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(68, 42); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(63, 13); this.label1.TabIndex = 3; this.label1.Text = "Class Name"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(68, 92); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(70, 13); this.label2.TabIndex = 4; this.label2.Text = "Window Text"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(280, 176); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; } }
cs file code
using System; using System.Collections.Generic; using System.Windows.Forms;
namespace cLoseotherWindow { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
|
| Author: Vasudevan Deepak Kumar 23 May 2008 | Member Level: Diamond Points : 2 |
A few things:
1) A few of the event handlers seem to be missing. 2) If the essence of the code is on the 'SendMessage' (P/Invoke API), wouldn't it have been better if we had a quick console application rather than a heavy windows forms?
|
| Author: komaladevi 23 May 2008 | Member Level: Gold Points : 2 |
it worked for me if anything is missing please provide me with pleasure
|