views:

4864

answers:

7

I just saw a really useful UI feature of the forthcoming Windows 7 ( visit http://www.gizmodo.com.au/2008/10/windows_7_walkthrough_boot_video_and_impressions-2.html and scroll down to the video entitled Super Scientific Video of New Window Resizing Feature)

In a nutshell you can drag a window by the title bar to the top of the screen to maximise it, drag it back down to restore and drag it to the left edge or the right edge of the screen to have it snap into position butted up against the edge you dragged it to, and resized to occupy half the screen.

Now I know there are a bunch of keyboard-based apps that do "similar" things but I've not seen a mouse-based program that does JUST the above, simple, useful and nothing more. If you know of one (or are smart enough to write one) please post the URL :)

Anyway, if I wanted to try and write a small app to do this I have a question:

If I wanted to try and write a small app to do this, could I write it as a .NET app? I guess hooking into Windows' window drag/drop handling is pretty low-level and maybe not something that could be done in C#.NET?

any smartypants here who can knock a small app like this out in their lunchbreak? ;) I'm sure anything mimicing Windows 7 features would prove very popular, at least pre Windows 7's launch.

Edit: The AeroSnap guys (http://www.aerosnap.de/index_eng.htm) have done a nice job of implementing this feature and judging by the number of downloads, it was a popular feature!

A: 

Yes, you can do this from C#, via PInvoke. Tbh, the best way to learn such APIs is by looking at the source of something like Bblean. This'll show you what you need to know and exposing it to PInvoke is pretty trivial.

Cody Brocious
NO, don't add hooks written in managed code. It will cause the runtime to get loaded into all applications in the system. Even worse, if the other app is also managed, you might end up with the wrong version of the runtime, either for your hook or for the other app.
erikkallen
A: 

pinvoke.net

This is a good reference website for using the Windows API via PInvoke from within .NET applications.

Chris Pietschmann
Thanks for the link to pinvoke.net. Lots of API details to dig around in there!
Sprogz
A: 

Thanks for taking the time to answer. I'll checkout using PInvoke. I'm assuming I need to hit the Windows API for both the hooking into windows as well as actually performing the move/resizing of the dragged window?

Sprogz
Yes -- the best way to learn this stuff is by looking at bblean, in my experience. A friend was working on a port of Bblean to Nemerle (another .NET language) -- if you google around, you might be able to find the source.
Cody Brocious
+1  A: 

If your goal is to create a hook that works across all applications and thus all windows on the desktop, then you cannot use a managed code assembly. You will have to write in a language that produces traditional DLLs such as C++.

In order to hook at the system level which then hooks into all running applications you must provide a dll that can be placed into the address spaces of all running aps. I would venture to say that a large percentage of most people's daily aps are not managed .net framework apps.

Bill
This is what I was afraid of after I read at the bottom of an [MSDN article on the subject of Windows hooks in C#.NET](http://support.microsoft.com/kb/318804/) that stated "Global hooks are not supported in the .NET Framework." What's the dif between a global hook and the hooks in that MSDN article?
Sprogz
The MSDN article is setting up application local hooks. Whe the code sample calls SetWindowsHookEx with the AppDomain.GetCurrentThreadId() for the dwThreadId parameter it is making its hook local. Change this parameter to zero and you have a global hook--just don't run it in .net it will blow up.
Bill
Also I recommend the article http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx as indicated by Nick. We did something similar to allow a VB application to be the recipient of global input activities. We created a C++ DLL that returned custom win msgs to a window in the VB app.
Bill
A: 

Try looking at these articles:

Nick
A: 

Try checking this one also, you have the explanation, source code and the references to the official MSDN documentation.

http://www.josefcobonnin.com/post/2007/06/19/Hook-Keyboard-and-Mouse.aspx

+4  A: 

There's an offical way: Windows API Code Pack

The Windows® API Code Pack for Microsoft® .NET Framework provides a source code library that can be used to access some new Windows 7 features (and some existing features of older versions of Windows operating system) from managed code. These Windows features are not available to developers today in the .NET Framework.

The individual features supported in this version (v1.0) of the library are:

Windows 7 Taskbar Jump Lists, Icon Overlay, Progress Bar, Tabbed Thumbnails, and Thumbnail Toolbars. Windows 7 Libraries, Known Folders, non-file system containers. Windows Shell Search API support, a hierarchy of Shell Namespace entities, and Drag and Drop functionality for Shell Objects. Explorer Browser Control. Shell property system. Windows Vista and Windows 7 Common File Dialogs, including custom controls. Windows Vista and Windows 7 Task Dialogs.

Simon
This is the answer.
Will