The Sandbox Thread

1 post

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Overriding a Form's WndProc in .NET CF 2.0

Back to Forum: The Sandbox
  • User profile image
    fluffybunny

    In developing MobileBlogger 2005 for the PocketPC, I wanted to resize the tabs containing the controls when the SIP (Soft Input Panel) was shown and hidden.

    I soon discovered I could not override the WndProc like I would in a WinForms application. One of the first hurdles was to get a handle to the form, which for some reason is not accessible in the CF, so I had to find an SDK way to do it. I used an apparently common technique to do this via the API call to GetCapture, which returns the window handle of the window that currently captured the mouse. This worked, so I moved on to implementing code to replace the WndProc via the GetWindowLong API.

    MobileBlogger was originally developed in .NET CF 1.0, but after I implemented code to override the WndProc, I got a nice "NotSupportedException" from Microsoft. I don't really understand their thinking on this, but I knew there had to be a way. Of course you can do this if you are developing your application with the SDK, but I wanted to use managed code and the framework.

    After writing a test application for the .NET CF v2.0 to see if this unwelcome behaviour persisted to the next version of the framework, I was pleasantly surprised that someone at Microsoft had come to their senses. It worked! The code in the form's load event handler saves the current WndProc (so we can call it after we are done handling anything that we want to handle) and then replaces it with the new WndProc. The framework actually came to my rescue when I found that Microsoft had added (seemingly for this very purpose) the method, Marshal.GetFunctionPointerForDelegate(). The code is in the attached zip file. Enjoy!

    private void MainForm_Load (object sender, EventArgs e)
    {
       newWndProc = new WndProcDelegate (WndProc);

       oldWndProc = GetWindowLong (this.Handle, GWL_WNDPROC);

       SetWindowLong (this.Handle, GWL_WNDPROC, Marshal.GetFunctionPointerForDelegate (newWndProc));
    }

    Check out http://mikeunderhill.blogspot.com/">http://mikeunderhill.blogspot.com for other rants...

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.