Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Prevent resize/move and keep mouse within window

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Mon Aug 24, 2009 7:08 am    Post subject: Prevent resize/move and keep mouse within window Reply with quote

I'm using dxwnd to put a game in a window, but it's a pain in the ass because when I'm playing the game my mouse moves off the game and starts clicking on the control bar of the window, and on the desktop. As it's an RTS game, I often find that an attempt to select units ends up in me resizing my game window.

I'd like to do two things:
1) Prevent the window from being moved or resized.
2) Keep the mouse within the game window's display region (and not on its control bar or outside the window). This functionality needs to be something I can toggle with a hotkey.

The application would be written in C++. Normally I'd go for C#, but I'm trying to get more experience in C++ and I heard a rumour that RegisterGlobalHotkey doesn't work properly (at least without loads of messing about) in .NET languages.

I can find the window with FindWindow, no problem. I'm assuming I can prevent resize using SetWindowLong, but I'm not sure exactly how I'd go about it.

As regards the mouse issue, I have no idea how to approach this. I figured that I could use a timer with GetCursorPos and mouse_event to jump the mouse back to the nearest coordinate within the window when it escapes, but this seems somewhat hacky and very unelegant. Any ideas how to do it better?

Also, I have seen global hotkeys done before, but I've never actually created one myself. Well, I have, but it was about 6 years ago in VB6. Could someone post me a code sample of registering a hot key in C++ and having it call back to a method?

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Mon Aug 24, 2009 8:20 am    Post subject: Reply with quote

capture mouse movement by something like this
Code:
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
MSLLHOOKSTRUCT *mhs = (MSLLHOOKSTRUCT *) lParam;
if (nCode < 0 || wParam != WM_MOUSEMOVE) {
return (::CallNextHookEx(thishook, nCode, wParam, lParam));
}
POINT pt;
::GetCursorPos(&pt); //  position before mouse move
int dx = mhs->pt.x - pt.x;
int dy = mhs->pt.y - pt.y;
int nx = pt.x + dy;
int ny = pt.y + dx;
::SetCursorPos(nx, ny);
return 1; //  any nonzero value discards mouse message
//  ?should maybe be
//  return (::CallNextHookEx(thishook, -1, wParam, lParam));
}


and here for window coördinates: http://support.microsoft.com/kb/11570

and simply modify the code above so if it is outside of the window, it doesn't move.
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Mon Aug 24, 2009 8:52 am    Post subject: Reply with quote

Why don't you try ClipCursor.

Anything you try with Get/SetCursorPos will be..yes, unelegant.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Aug 24, 2009 8:57 am    Post subject: Reply with quote

@sshac, a timer and/or constantly checking cursor position is unnecessary. the equivalent can be done by handling WM_MOUSELEAVE which is i am guessing how ClipCursor works

@Burningmace, i can code you an example of registering a hotkey in C, interested ? it's really really basic though. all you need to do is call RegisterHotKey() then modify your wndproc so you are handling WM_HOTKEY messages for the id you have specified for a given hotkey
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Mon Aug 24, 2009 9:11 am    Post subject: Reply with quote

Code:
#include <Windows.h>

LPCTSTR ClassName = "LockMouse";
LPCTSTR WndName = "Mouse";

LRESULT CALLBACK WndProc(HWND hWnd , UINT Msg, WPARAM wParam , LPARAM lParam);

INT WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
   HWND hWnd;
   WNDCLASSEX WndClassEx;
   MSG Msg;

   ZeroMemory(&WndClassEx, sizeof(WNDCLASSEX));

   WndClassEx.cbSize = sizeof(WNDCLASSEX);
   WndClassEx.cbClsExtra = 0;
   WndClassEx.hInstance = hInstance;
   WndClassEx.cbWndExtra = 0;
   WndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   WndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW);
   WndClassEx.hbrBackground = (HBRUSH)GetStockObject(0);
   WndClassEx.lpszMenuName = NULL;
   WndClassEx.lpszClassName = ClassName;
   WndClassEx.hInstance = hInstance;
   WndClassEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
   WndClassEx.lpfnWndProc = WndProc;

   RegisterClassEx(&WndClassEx);

   hWnd = CreateWindowEx(WS_EX_APPWINDOW,ClassName,WndName,WS_OVERLAPPEDWINDOW,0,0,200,200,NULL,NULL,hInstance,NULL);

   ShowWindow(hWnd,SW_SHOWNORMAL);
   UpdateWindow(hWnd);

   while( GetMessage(&Msg, NULL, 0, 0) )
   {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
   }

   return Msg.wParam;
}

RECT rct;
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
   switch(Msg){

      case WM_LBUTTONDOWN:         
         GetWindowRect(hWnd, &rct);
         ClipCursor(&rct);
      break;

      case WM_DESTROY:
         PostQuitMessage(WM_QUIT);
      break;
      
      default:
         return DefWindowProc(hWnd,Msg,wParam,lParam);
      break;
   }
   return 0;
}


Modify it to however you like.




Slugsnack wrote:
@Burningmace, i can code you an example of registering a hotkey in C, interested ? it's really really basic though. all you need to do is call RegisterHotKey() then modify your wndproc so you are handling WM_HOTKEY messages for the id you have specified for a given hotkey


He said he knows how, but he heard it doesn't work with C# lol
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Mon Aug 24, 2009 9:19 am    Post subject: Reply with quote

smartz993 wrote:
Slugsnack wrote:
@Burningmace, i can code you an example of registering a hotkey in C, interested ? it's really really basic though. all you need to do is call RegisterHotKey() then modify your wndproc so you are handling WM_HOTKEY messages for the id you have specified for a given hotkey


He said he knows how, but he heard it doesn't work with C# lol


Quote:
Also, I have seen global hotkeys done before, but I've never actually created one myself. Well, I have, but it was about 6 years ago in VB6. Could someone post me a code sample of registering a hot key in C++ and having it call back to a method?


did i misinterpret ?

//edit : oh my bad he wants a GLOBAL hotkey..

as far as i know windows api does not have a function for registering global hotkeys. i guess you could do a keyboard interrupt hook lol but that's way overkill.. or maybe getasynckeystate.. hmmm lemme play around for a bit
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Mon Aug 24, 2009 3:27 pm    Post subject: Reply with quote

GetAsyncKeyState is probably the simplest way
Back to top
View user's profile Send private message
shhac
Expert Cheater
Reputation: 0

Joined: 30 Oct 2007
Posts: 108

PostPosted: Mon Aug 24, 2009 8:06 pm    Post subject: Reply with quote

Slugsnack wrote:
@sshac, a timer and/or constantly checking cursor position is unnecessary. the equivalent can be done by handling WM_MOUSELEAVE which is i am guessing how ClipCursor works
My way was a hook, where do you see any timers?
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Tue Aug 25, 2009 3:02 am    Post subject: Reply with quote

yeah that was my bad, read it wrong
Back to top
View user's profile Send private message
Polynomial
Grandmaster Cheater
Reputation: 5

Joined: 17 Feb 2008
Posts: 524
Location: Inside the Intel CET shadow stack

PostPosted: Wed Aug 26, 2009 7:55 am    Post subject: Reply with quote

Thanks for all the help so far.

Any ideas on how to prevent move/resize too? I think I've seen SetWindowLong used to do this, but I can't remember where or exactly how it was done.

_________________
It's not fun unless every exploit mitigation is enabled.
Please do not reply to my posts with LLM-generated slop; I consider it to be an insult to my time.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Aug 26, 2009 1:33 pm    Post subject: Reply with quote

handle wm_move and wm_size
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites