Click here to Skip to main content
11,429,777 members (71,514 online)
Click here to Skip to main content

X-Window Manager like dragging and resizing of windows

, 21 Sep 2005
Rate this:
Please Sign up or sign in to vote.
This article shows how to make use of a mouse hook to allow simple dragging and resizing of windows.

Introduction

Hooks are a powerful feature of Windows and can be used to trap nearly all events in the Windows environment. This example shows how to trap mouse events and implements a simple window manager. It allows you to move and resize windows, easily following the style of many window managers in the UNIX X-Window environment.

With this tool, any window on your Desktop can easily be dragged with the left-mouse button while holding down the ALT key, starting anywhere in the window's client area. In the same way, it is possible to resize any window while dragging with the right mouse-button, holding down ALT. That's in my opinion far more comfortable than aiming with the mouse for the border or a corner of the window Smile | :)

The tool consists of two parts. First the main program that loads the second part - a DLL that will subsequently be injected in other processes to trap mouse events. For details about this, you may refer to other great articles at CodeProject as for example Hooks and DLLs by Joseph M. Newcomer in the DLL-section. The main program further installs a tray icon (warning: programmer's art Smile | :) ), that allows the user to quit the tool.

History

  • 06.08.2003

    Initial version.

  • 17.04.2003

    Bug fix: proper handling of Alt-key release while dragging or sizing (thanks Whosit).

  • 10.09.2005

    Bug fix: don't move or resize windows that are maximized (thanks TWink).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Markus Rollmann
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionOK to fork? Pin
foobarz29-Dec-08 12:31
memberfoobarz29-Dec-08 12:31 
AnswerRe: OK to fork? Pin
Markus Rollmann29-Dec-08 21:40
memberMarkus Rollmann29-Dec-08 21:40 
GeneralWorks in Vista! Pin
JXON4-Sep-08 20:49
memberJXON4-Sep-08 20:49 
GeneralGreat! Pin
Mwob2-May-07 23:18
memberMwob2-May-07 23:18 
GeneralRe: Great! Pin
AlexHaan3-May-07 1:38
memberAlexHaan3-May-07 1:38 
GeneralAwesome Pin
aijfisuhhtrtl16-Apr-07 4:34
memberaijfisuhhtrtl16-Apr-07 4:34 
GeneralPrevent Users from Resizing Windows Pin
mrtm30509-Mar-07 12:14
membermrtm30509-Mar-07 12:14 
Generalis snapping possible Pin
mmanson13219-Jan-07 10:26
membermmanson13219-Jan-07 10:26 
GeneralTwo suggestions Pin
Anonymous5-Oct-05 2:02
sussAnonymous5-Oct-05 2:02 
GeneralRe: Two suggestions Pin
Markus Rollmann5-Oct-05 2:16
memberMarkus Rollmann5-Oct-05 2:16 
GeneralCursor shape Pin
Eric Lapouge22-Sep-05 22:32
memberEric Lapouge22-Sep-05 22:32 
GeneralBugs and Improvements Pin
Twink7-Sep-05 14:54
memberTwink7-Sep-05 14:54 
Well I love this program and would find it hard to live without it now, so I've started to get around to fixing someof the annoying things in it =)


To stop moving of maximised windows in wmmousedll.cpp just before the code

switch (opMode) {
case O_MOVE :
// we're dragging a window - calc the relative movement and
// reposition the window


put this

WINDOWPLACEMENT wndpl;
::GetWindowPlacement(hWnd, &wndpl);
if(wndpl.showCmd == SW_MAXIMIZE)
return CallNextHookEx(hhook, nCode, wParam, lParam);


Another addition is just after the code I added above is this piece of code

// get class
char dada[20];
GetClassName(hWnd,dada,20);
if(stricmp(dada, "TSSHELLWND") == 0) return CallNextHookEx(hhook, nCode, wParam, lParam);


You can replace TSSHELLWND with the class of any window you dont want to be able to drag around like this, in this case (TSSHELLWND) is remote desktop. This could be extended to have a UI letting you add more window classes (or even better exe names and paths, but I dunno how to do that)

This program stops Alt-Clicks getting through to the window at all, this is not desirable but would be hard to fix nicely.

One way is in the cases
case WM_RBUTTONDOWN :
case WM_LBUTTONDOWN :

You really count return 0; instead of 1.

Admittedly this has other problems, as sometimes I want to resize instead of alt-clicking a button and with my change it will perform both. I'm guessing a fix for this would be to send a mouse click to the window on mouse up if the mouse did not move instead of the return 0 fix.

-- modified at 20:00 Wednesday 7th September, 2005
GeneralRe: Bugs and Improvements Pin
Markus Rollmann10-Sep-05 1:30
memberMarkus Rollmann10-Sep-05 1:30 
GeneralRe: Bugs and Improvements Pin
Twink11-Sep-05 15:47
memberTwink11-Sep-05 15:47 
Questionbug? Pin
p1ayer7-Jul-03 20:27
memberp1ayer7-Jul-03 20:27 
AnswerRe: bug? Pin
Markus Rollmann7-Jul-03 21:23
memberMarkus Rollmann7-Jul-03 21:23 
GeneralGreat technique Pin
Vernero Cifagni17-Apr-03 3:26
memberVernero Cifagni17-Apr-03 3:26 
Generalvery creative, so 5 points! Pin
Zhefu Zhang17-Apr-03 3:13
memberZhefu Zhang17-Apr-03 3:13 
General(update) Pin
Markus Rollmann17-Apr-03 2:59
memberMarkus Rollmann17-Apr-03 2:59 
GeneralRe: (update) Pin
Whosit17-Apr-03 4:37
memberWhosit17-Apr-03 4:37 
GeneralDoesnt forget right away Pin
Whosit16-Apr-03 11:31
memberWhosit16-Apr-03 11:31 
GeneralRe: Doesnt forget right away Pin
Markus Rollmann16-Apr-03 11:40
memberMarkus Rollmann16-Apr-03 11:40 
GeneralRe: Doesnt forget right away Pin
Whosit16-Apr-03 11:47
memberWhosit16-Apr-03 11:47 
GeneralRe: Doesnt forget right away Pin
Markus Rollmann16-Apr-03 11:59
memberMarkus Rollmann16-Apr-03 11:59 
GeneralChange for override Pin
Whosit16-Apr-03 11:55
memberWhosit16-Apr-03 11:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 21 Sep 2005
Article Copyright 2002 by Markus Rollmann
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid