using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace RegisterClassName
{
public class Form1 : System.Windows.Forms.Form
{
[ DllImport("kernel32") ]
private static extern IntPtr GetModuleHandle(string modName);
[ DllImport( "user32.dll" ) ]
private static unsafe extern int RegisterClass( WNDCLASS wnd );
[ DllImport( "gdi32.dll" ) ]
private static extern IntPtr GetStockObject( int fnObject );
[DllImport( "User32" )]
private static extern IntPtr FindWindow(string lpszClassName, string lpszWindowName);
[ StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class WNDCLASS
{
public int style;
[ MarshalAs( UnmanagedType.FunctionPtr ) ]
public WndProcDelegate lpfnWndProc;
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
}
public delegate IntPtr WndProcDelegate( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
private WndProcDelegate m_Delegate;
private WNDCLASS wndclass;
private void Register()
{
wndclass = new WNDCLASS();
wndclass.style = CreateParams.ClassStyle;
// Window procedure
m_Delegate = new WndProcDelegate( MyWndProc );
wndclass.lpfnWndProc = m_Delegate;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
// This gets the hInstance for the module being executed
wndclass.hInstance = GetModuleHandle( null );
wndclass.hIcon = this.Icon.Handle;
wndclass.hCursor = this.Cursor.Handle;
// This gets a gray brush
wndclass.hbrBackground = GetStockObject( 2 );
wndclass.lpszMenuName = null;
wndclass.lpszClassName = "My Test Window";
// Atom is 0 if registration fails
int atom = RegisterClass( wndclass );
MessageBox.Show( atom.ToString() );
}
private IntPtr MyWndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam )
{
Message m = Message.Create( hwnd, msg, wParam, lParam );
WndProc( ref m );
return m.Result;
}
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
Register();
this.CreateParams.ClassName = "My Test Window";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Find Window";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(192, 54);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr ptr = FindWindow( "My Test Window", null );
MessageBox.Show( ptr.ToInt32().ToString() );
}
}
}