Hey,
I'm fairly new to this PInvoke stuff. Could anyone tell me how to get whether a window is being resized with the WH_CALLWNDPROC method? A description would be great too.
Thanks a lot

Recommended Answers

All 16 Replies

You should override the WndProc method in your form. Then check for the message.

You really don't want to use hooks unless they are absolutely necessary.

The Windows.Forms class actually allows you to override the WndProc message pump. So all messages sent to that window can be intercepted.

The signature is protected override void WndProc(ref Message msg) You can get the message code from msg.Msg the WM_* codes can be found at pinvoke.net

After you have processed the message, please end the method with base.WndProc(ref msg)

You really don't want to use hooks unless they are absolutely necessary.

The Windows.Forms class actually allows you to override the WndProc message pump. So all messages sent to that window can be intercepted.

The signature is protected override void WndProc(ref Message msg) You can get the message code from msg.Msg the WM_* codes can be found at pinvoke.net

After you have processed the message, please end the method with base.WndProc(ref msg)

Hey,
I am trying to get a tooltip to show the size of any window when it is being resized. I already have a WndProc section for a global keyboard shortcut- could I use this for the resizing function. Also, what must I put with the DLLImport stuff, and how do I set the hook?
Thanks a lot

Ok firstly. Don't Hook, seriously...don't! :D

You don't need to use P/Invoke for this. You can do it using your current WndProc section.

Use a switch-case block and check for the WM_SIZE (0x0005) message and your keyboard short-cut.

Could you not possibly do this using the Form Resize and ResizeEnd events?

Ok firstly. Don't Hook, seriously...don't! :D

You don't need to use P/Invoke for this. You can do it using your current WndProc section.

Use a switch-case block and check for the WM_SIZE (0x0005) message and your keyboard short-cut.

Could you not possibly do this using the Form Resize and ResizeEnd events?

"I am trying to get a tooltip to show the size of any window when it is being resized." - I mean external windows.
Would this still work with your method?
Cheers

Ah, no it would not. You would have to use Hooks...

PInvoke for SetWindowsHookEx

Ignore the stuff about keyboards, the Hook you need to set is WH_CALLWNDPROCRET Also make note of the structure and signature required for the callback methods. They are on the WH_CALLWNDPROCRET link.

Ah, no it would not. You would have to use Hooks...

PInvoke for SetWindowsHookEx

Ignore the stuff about keyboards, the Hook you need to set is WH_CALLWNDPROCRET Also make note of the structure and signature required for the callback methods. They are on the WH_CALLWNDPROCRET link.

Hey,
It doesn't appear to work for other window. :/
Is this my error, or is it just not possible?
Another idea would be to detect a mouse click, use the GetWindowRect to get the information, get the width of the border by using this.Width - this.ClientArea.Width (/ 2), and detect whether it is in that area. How would that look programatically?

It should work for other windows, that's what they're designed for.

It should work for other windows, that's what they're designed for.

I could be missing something. I just made a demo app before I put it in my main project, but here's the source
http://pastebin.com/KGV7CBTZ
Thanks a lot for your help by the way- I really appreciate it.

Set the last parameter of your SetWindowsHookEx call to 0. If you use the current thread Id then you're asking it only to associate with that thread. Which isn't what you want.

Set the last parameter of your SetWindowsHookEx call to 0. If you use the current thread Id then you're asking it only to associate with that thread. Which isn't what you want.

Hmm. When I do that, I get nothing at all.

From the MSDN:

You must place a global hook procedure in a DLL separate from the application installing the hook procedure. The installing application must have the handle to the DLL module before it can install the hook procedure. To retrieve a handle to the DLL module, call the LoadLibrary function with the name of the DLL. After you have obtained the handle, you can call the GetProcAddress function to retrieve a pointer to the hook procedure. Finally, use SetWindowsHookEx to install the hook procedure address in the appropriate hook chain. SetWindowsHookEx passes the module handle, a pointer to the hook-procedure entry point, and 0 for the thread identifier, indicating that the hook procedure should be associated with all threads in the same desktop as the calling thread.

So it needs to be in a separated library by the looks of it ^^

From the MSDN:

So it needs to be in a separated library by the looks of it ^^

Ah. I was trying to do this without DLLs, but it looks like that will not be possible. I will just use PInvokes Managed Window API in that case.
Thank so much for your help, you helped a lot. Best wishes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.