Steve Dunn
Steve Dunn

Reputation: 21791

Custom properties on a win32 window

I've heard (well read, at http://www.ddj.com/windows/184416861), that it's possible to set custom properties on a Win32 window.

The trouble is, the download for the article above is on an ftp server that won't let me in.

As a bit of background info, I have a .NET app. The main window is registered to handle custom window messages. From a separate app, I need to post messages to this window. I can't find the window by caption as the caption changes. I can't find it JUST by window class, as the window class is the same for all forms in that app domain.

Ideally, I'd like to set a custom property on the Win32 window of the main form (Form1) that say, yes, this is form1. Then when I'm enumerating the windows of this app, I can tell that this is the required form by seeing if this custom property exists.

Cheers,

Steve

Upvotes: 2

Views: 2937

Answers (2)

Steve Dunn
Steve Dunn

Reputation: 21791

As Martin says, the answer is the Win32 APIs GetProp and SetProp.

Here's what I now do when I create the main form:

[DllImport("user32.dll", SetLastError=true)]
static extern bool SetProp(IntPtr hWnd, string lpString, IntPtr hData);

SetProp( this.Handle, @"foo", new IntPtr( 1 ) ) ;

Now, I can check this property when enumerating the windows:

[DllImport("user32.dll")]
private static extern IntPtr GetProp(IntPtr hWnd, string lpString);

IntPtr result = GetProp( (IntPtr) hWnd, @"foo" ) ;

Upvotes: 2

Martin B
Martin B

Reputation: 24180

See here for an overview of window properties. Basically, you call the Win32 API function SetProp to set a window property and GetProp to retrieve it. There are a few more functions for enumerating properties and the like, but it sounds as if SetProp and GetProp is all you need.

Upvotes: 2

Related Questions