Skip to content

Commit

Permalink
Fixed a bug with the fullscreen code in r6; fixed windows next to the…
Browse files Browse the repository at this point in the history
… screen border being considered as fullscreen windows.

Improved performance by only installing the mouse hook when the alt key is pressed.
Check if the window still exists while dragging.
  • Loading branch information
stefansundin committed Aug 3, 2008
1 parent d3fbccc commit dab5abb
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 42 deletions.
33 changes: 5 additions & 28 deletions altdrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@
//Stuff
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);

//Hook data
static HINSTANCE hinstDLL;
static HHOOK keyhook, mousehook;

//Global info
static HICON icon[2];
static NOTIFYICONDATA traydata;
static UINT WM_TASKBARCREATED;
static int tray_added=0;
static int hook_installed=0;
static int hide=0;

static HINSTANCE hinstDLL;
static HHOOK keyhook;
static int hook_installed=0;

static char msg[100];

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
Expand Down Expand Up @@ -263,20 +261,6 @@ int InstallHook() {
return 1;
}

//Get address to mouse hook (beware name mangling)
if ((procaddr=(HOOKPROC)GetProcAddress(hinstDLL,"MouseProc@12")) == NULL) {
sprintf(msg,"GetProcAddress() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
return 1;
}

//Set up the mouse hook
if ((mousehook=SetWindowsHookEx(WH_MOUSE_LL,procaddr,hinstDLL,0)) == NULL) {
sprintf(msg,"SetWindowsHookEx() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
return 1;
}

//Success
hook_installed=1;
if (!hide) {
Expand All @@ -298,13 +282,6 @@ int RemoveHook() {
return 1;
}

//Remove mouse hook
if (UnhookWindowsHookEx(mousehook) == 0) {
sprintf(msg,"UnhookWindowsHookEx() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
return 1;
}

//Unload dll
if (FreeLibrary(hinstDLL) == 0) {
sprintf(msg,"FreeLibrary() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
Expand Down Expand Up @@ -393,7 +370,7 @@ LRESULT CALLBACK MyWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
SetAutostart(1,0);
}
else if (wmId == SWM_ABOUT) {
MessageBox(NULL, "AltDrag - 0.2\nhttp://altdrag.googlecode.com/\nrecover89@gmail.com\n\nDrag windows with the mouse when pressing the alt key.\n\nYou can use -hide as a parameter to hide the tray icon.\n\nSend feedback to recover89@gmail.com", "About AltDrag", MB_ICONINFORMATION|MB_OK);
MessageBox(NULL, "AltDrag - 0.3\nhttp://altdrag.googlecode.com/\nrecover89@gmail.com\n\nDrag windows with the mouse when pressing the alt key.\nFullscreen windows, such as games, will not be dragged.\n\nYou can use -hide as a parameter to hide the tray icon.\n\nSend feedback to recover89@gmail.com", "About AltDrag", MB_ICONINFORMATION|MB_OK);
}
else if (wmId == SWM_EXIT) {
DestroyWindow(hWnd);
Expand Down
78 changes: 67 additions & 11 deletions hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ static int move=0;
static HWND hwnd=NULL;
static POINT offset;

static HINSTANCE hinstDLL;
static HHOOK mousehook;
static int hook_installed=0;

static char msg[100];

_declspec(dllexport) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
Expand All @@ -37,9 +41,13 @@ _declspec(dllexport) LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPA
if (vkey == VK_MENU || vkey == VK_LMENU || vkey == VK_RMENU) {
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
alt=1;
InstallHook();
}
else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
alt=0;
if (!move) {
RemoveHook();
}
}
}
}
Expand All @@ -54,6 +62,7 @@ _declspec(dllexport) LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM
//Double check that Alt is pressed
if (!(GetAsyncKeyState(VK_MENU)&0x8000)) {
alt=0;
RemoveHook();
}

//Alt is still being pressed
Expand All @@ -78,7 +87,7 @@ _declspec(dllexport) LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM
}

//Don't move the window if it's fullscreen
if (window.left != desktop.left && window.top != desktop.top && window.right != desktop.right && window.bottom != desktop.bottom) {
if (window.left != desktop.left || window.top != desktop.top || window.right != desktop.right || window.bottom != desktop.bottom) {
//Restore the window if it's maximized
if (IsZoomed(hwnd)) {
//Restore window
Expand Down Expand Up @@ -120,30 +129,77 @@ _declspec(dllexport) LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM
else if (wParam == WM_LBUTTONUP && move) {
move=0;
hwnd=NULL;
RemoveHook();
//Prevent mouseup from propagating
return 1;
}

//Move window
if (wParam == WM_MOUSEMOVE && move) {
//Get window size
RECT window;
if (GetWindowRect(hwnd,&window) == 0) {
sprintf(msg,"GetClientRect() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
if (IsWindow(hwnd)) {
//Get window size
RECT window;
if (GetWindowRect(hwnd,&window) == 0) {
sprintf(msg,"GetClientRect() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
}

//Move
if (MoveWindow(hwnd,pt.x-offset.x,pt.y-offset.y,window.right-window.left,window.bottom-window.top,TRUE) == 0) {
sprintf(msg,"MoveWindow() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
}
}

//Move
if (MoveWindow(hwnd,pt.x-offset.x,pt.y-offset.y,window.right-window.left,window.bottom-window.top,TRUE) == 0) {
sprintf(msg,"MoveWindow() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
else {
move=0;
hwnd=NULL;
RemoveHook();
}
}
}

return CallNextHookEx(NULL, nCode, wParam, lParam);
}

int InstallHook() {
if (hook_installed) {
//Hook already installed
return 1;
}

//Set up the mouse hook
if ((mousehook=SetWindowsHookEx(WH_MOUSE_LL,MouseProc,hinstDLL,0)) == NULL) {
sprintf(msg,"SetWindowsHookEx() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
return 1;
}

//Success
hook_installed=1;
return 0;
}

int RemoveHook() {
if (!hook_installed) {
//Hook not installed
return 1;
}

//Remove mouse hook
if (UnhookWindowsHookEx(mousehook) == 0) {
sprintf(msg,"UnhookWindowsHookEx() failed (error code: %d) in file %s, line %d.",GetLastError(),__FILE__,__LINE__);
MessageBox(NULL, msg, "AltDrag Warning", MB_ICONWARNING|MB_OK);
return 1;
}

//Success
hook_installed=0;
return 0;
}

BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD reason, LPVOID reserved) {
if (reason == DLL_PROCESS_ATTACH) {
hinstDLL=hInstance;
}
return TRUE;
}
5 changes: 5 additions & 0 deletions info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ http://altdrag.googlecode.com/
recover89@gmail.com

Drag windows with the mouse when pressing the alt key.
Fullscreen windows, such as games, will not be dragged.

You can use -hide as a parameter to hide the tray icon.

Changelog
---------
0.3:
Fixed a bug that made AltDrag ignore all windows which were at the screen borders since it thought they were fullscreen windows.
Improved performance by only installing the mouse hook when the alt key is pressed.

0.2:
You can no longer drag fullscreen windows.
The window is better placed when it's restored from its maximized state.
Expand Down
6 changes: 3 additions & 3 deletions resources.rc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "AltDrag.exe.manifest"

#define VS_VERSION_INFO 1
VS_VERSION_INFO VERSIONINFO
FILEVERSION 0,2,0,0
PRODUCTVERSION 0,2,0,0
FILEVERSION 0,3,0,0
PRODUCTVERSION 0,3,0,0
FILEFLAGSMASK 0x3fL
FILEFLAGS 0x0L
FILEOS 0x40004L
Expand All @@ -21,7 +21,7 @@ BEGIN
BLOCK "040904b0"
BEGIN
VALUE "FileDescription", "AltDrag"
VALUE "FileVersion", "0.2"
VALUE "FileVersion", "0.3"
VALUE "InternalName", "altdrag"
VALUE "LegalCopyright", "� Stefan Sundin 2008"
VALUE "OriginalFilename", "AltDrag.exe"
Expand Down

0 comments on commit dab5abb

Please sign in to comment.