Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Subclass form window proc

109 views
Skip to first unread message

Mike Scott

unread,
Aug 1, 2003, 7:45:14 AM8/1/03
to
What's the easiest way to subclass the window proc of a form so that I can
add special processing for Windows messages such as WM_CLOSE?

Cheers,

MikeS.


Alex Yakhnin[eMVP]

unread,
Aug 1, 2003, 8:20:57 AM8/1/03
to
The combination of MessageWindow and native dll that'd
redirect the subclassed messages to this MessageWindow...

-Alex
--
Alex Yakhnin, Microsoft Embedded MVP
IntelliProg, Inc.
http://www.intelliprog.com
"Check out our Compact Framework controls..."

>.
>

Mark Erikson

unread,
Aug 2, 2003, 11:00:26 PM8/2/03
to
On Fri, 1 Aug 2003 12:45:14 +0100, "Mike Scott" <mi...@nospam.com>
wrote:

The other option is to use Alex Feinman's OpenNETCF.Callbacks library.
Since it's pretty much an all-managed code deal, this lets you put it
all inside your current program, rather than shipping an additional
DLL. See the article at
http://www.alexfeinman.com/Callbacks/Callbacks.htm for really detailed
information on how the technique works(the WndProc section is at the
bottom).

Here's some example code (mostly taken from Alex's sample). Hope it
helps!

Mark Erikson

Basic usage: call InitiateSubclass() in your form's constructor. Fill
in WndProc as usual.


using OpenNETCF.Callbacks;

[DllImport("coredll")]
public static extern IntPtr GetFocus();

[DllImport("coredll")]
public static extern int GetWindowLong(IntPtr hWnd, GetWindowLongParam
nItem);

[DllImport("coredll")]
public static extern void SetWindowLong(IntPtr hWnd, int
GetWindowLongParam, int nValue);

[DllImport("coredll")]
public static extern IntPtr CallWindowProc(IntPtr pfn, IntPtr hWnd,
int msg, IntPtr wParam, IntPtr lParam);


private Callback newWndProc;
private IntPtr oldWndProc;

int GWL_WNDPROC = -4;

private void InitiateSubclass()
{
this.Focus();
IntPtr hWnd = GetFocus();

if (hWnd == IntPtr.Zero)
throw new InvalidOperationException("Subclass error!");

newWndProc = CallbackFactory.AllocateCallback(this, "WndProc");
oldWndProc = new IntPtr(GetWindowLong(hWnd, GWL_WNDPROC));
Win32Window.SetWindowLong(hWnd, GWL_WNDPROC,
newWndProc.CB.ToInt32());
}

private void RemoveSubclass()
{
this.Focus();
IntPtr hWnd = GetFocus();

SetWindowLong(hWnd, GWL_WNDPROC, oldWndProc.ToInt32());
}


private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr
lParam )
{
// do all your usual message processing here
// to call the base WndProc, do the following:
// CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam);
}

Alex Feinman [MVP]

unread,
Aug 3, 2003, 2:52:19 AM8/3/03
to
Guys,
Keep in mind that I haven't had a chance to update Callbacks library with
the ARM code for PPC 2003. If there are any takers, I would gladly
incorporate the new assembly stub into the article code.

"Mark Erikson" <marku...@yahoo.com> wrote in message
news:aiuoivcpu8c2ivji9...@4ax.com...

0 new messages