I'm writing a toy application that plays with the mouse cursor, and I'm trying to move it programmticly. Using either Cursor.Position = ... or Win32 interop calls work fine on a normal machine, but I'm having difficulties getting it to work under VMWare.
Does anyone have any suggestions?
EDIT
To clarify:
I've got a small windows forms application I've made running inside the VM, it has one button on it, when clicked it is supposed to move the mouse cursor inside the VM. I've used both the Cursor.Position method and the approach that Wolf5 has suggested.
Try this instead:
[DllImport("user32", SetLastError = true)]
private static extern int SetCursorPos(int x, int y);
public static void SetMousePos(Point p) {
SetMousePos(p.X, p.Y);
}
public static void SetMousePos(int x, int y) {
SetCursorPos(x, y);
}
Of course you will have to make sure VMWARE has focus in the first place since it cannot set the mouse position of your mouse outside VMWARE.
Don't focus the VM with your real mouse. Or uninstall the VMWare mouse driver so the VM doesn't get the focus unless you click inside it.
I have resolved the issue.
In a desperate attempt to try anything i finally gave up and un-installed the mouse driver from the VM. After a reboot, my toy application works.
The device was listed as a VMWare Pointing device, after the reboot it's coming up as an "unknown device", but the mouse still works. Albeit I a little on the nippy side.
Related
I am working on a WPF application and want to set the cursor position to the corner of the screen.
I tried using the WinForms approach:
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(0, 0);
and the user32.dll approach:
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
The WinForms approach worked sometimes, but I don't understand why it worked and didn't work sometimes. I made sure the code is actually being called. The user32.dll approach didn't work for me at all.
Simple solution is make your application enough big and move mouse inside of it.
http://www.aspdotnet-pools.com/2017/10/move-mouse-cursor-automatically-c.html
To move mouse outside your application you need global mouse hook.
Global mouse event handler
I'm looking for an api that can cause a windows select/highlight event to occur on a windows desktop, without actually causing the mouse cursor to move.. I can cause the mouse cursor to move with :
public static extern bool SetCursorPos(int X, int Y);
But that moves the actual cursor to that point... I'm looking for a way to highlight only as one might do by using the tab and arrow keys to move around the windows desktop. Any suggestions are appreciated..
regards, rob
I think you may be looking for SetFocus. You can get a control's handle with Control.Handle or FindWindow , and p/invoke SetFocus (use IntPtr as the argument type).
We ported win-form application legacy code developed in c# .net framework 1.1 to c# .net 3.5.
There is functionality for drawing dot plot and apply different shapes(polygon,square etc.) on dots in plot.
You can change size of applied shape by dragging using mouse what happening is when dragging shape some part get invisible till we drop to new point and also draws very slow speed.
you can call it as flickering .
this functionality works fine in windows xp, in windows 7 works fine
with basic and classic theme
it flickers only when aero theme is applied in windows 7
i have tried
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/aaed00ce-4bc9-424e-8c05-c30213171c2c
and
Flickering in a Windows Forms app
none of them helped.
after time stamping execution time i found that
for (int i=0; i < arraypnts.Length - 1; i++) {
g.DrawLine(Pens.Black, arraypnts[i], arraypnts[i+1]);
}
// draw the last line
g.DrawLine(Pens.Black, arraypnts[0], arraypnts[arraypnts.Length-1]);
takes hell lot of time when aero theme applied compared to basic theme applied
I had a similar problem long time ago and after a while finally found the solution by redrawing components only when setting the Object visibility to false first.
Object.Visible = false
//redraw your components
Object.Visible = true
I dont know if this is possible in your case but worth a try!
I think you need to use double buffer for you form object.
this.DoubleBuffered = true;
Well I found a workaround for the flickering problem. Just disable the desktop composition — you can do it either at application level or OS level.
For application level, go to exe file -> right click -> compatibility-> check disable desktop composition.
Foror OS level (and better visual appearance), start->right click
computer -> properties -> Advance system settings -> Advance tab ->
performance settings -> visual effects->custom-> uncheck Enable
desktop composition.
“works fine with Aero off” …sounds like a recourse issue to me…. Are you drawing in a paint event? If not where are you obtaining your graphics object from? You can programmatically disable desktop composition, I need it for some projects I do… but nothing related to drawing…
[DllImport("dwmapi.dll")]
private static extern int DwmIsCompositionEnabled(ref bool enabled);
const uint DWM_EC_DISABLECOMPOSITION = 0;
const uint DWM_EC_ENABLECOMPOSITION = 1;
[DllImport("dwmapi.dll", EntryPoint = "DwmEnableComposition")]
private static extern uint DwmEnableComposition(uint compositionAction);
usage:
DwmEnableComposition(DWM_EC_ENABLECOMPOSITION);
First of all I'm not sure if ".exe window" is the proper term. It's the window that pops up when you start the application.
I'm a game programmer, and when I'm debugging, I very rapidly start it up, look at the problem, then close it down again to make minor changes in the code, then start it again etc. I do this like once per minute, so it happens a lot. My problem is that the .exe window always appears at the middle of my main screen (where I'm coding), and I'm running double monitors, and I'd like the game window to appear on my second screen instead of my main screen (obscuring my code).
Can I change where the exe window appears in VS2010? I've looked around everywhere, it feels like. Or is it something that will have to be managed by a 3rd party program? If so, what program?
Edit:
OK, OK, I found the solution. I did a really dumb mistake where I didn't mention that I am using XNA, and not using winforms. Sorry for misleading you guys. Here's how I solved it:
First off I had to include:
using System.Runtime.InteropServices;
Then at the top of my main class I created a tiny class:
public static class User32
{
[DllImport("user32.dll")] public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
Then in my Initialize function I simply call:
#if DEBUG
User32.MoveWindow(Game.Window.Handle, 2000, 400, 600, 480, true);
#endif
It's a little ugly, but it's only for debugging and only called once, so psh.
Original solution found here: http://social.msdn.microsoft.com/forums/en-US/xnagamestudioexpress/thread/bc9588a9-542f-425b-9025-d69fe2b0b182/
You can set the Form.StartPosition property, or just manually write to the Left and Top properties of the form you want to move.
Option 1: You could set the appropriate properties on the window/form if a debugger is attached.
if (System.Diagnostics.Debugger.IsAttached)
{
// Set the window/form's top/left properties.
}
Option 2: Add a command line switch, use that as startup parameter (Properties->Debug->Commandline arguments), and then set the appropriate properties in the window/form:
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.Args.Any(arg => arg.Equals("/debugmode", StringComparison.OrdinalIgnoreCase))
// Set some value which you check in your main window.
}
Although you are not using winforms, you still change it in Xna by using winforms objects. I know you found a solution but here is how to change it without using interop.
Add a reference to System.Windows.Forms and System.Drawing to the References in the game project.
Resist the temptation to add using statements for these as it can cause ambiguity with some Xna objects (Point, for instance, which in Xna uses floats).
In the Game.Initialize method:
System.Drawing.Point p = new System.Drawing.Point(2000, 400);// or wherever you want
System.Windows.Forms.Control c = Control.FromHandle(this.Window.Handle);
c.Location = p;
the game window will now start at the screen 2000,400 location.
I would just call it the "main application window". Anyway, assuming you're using WinForms, this would put the window in the top left corner of the first screen that isn't your primary screen:
void Form1_Load(object sender, EventArgs e)
{
#if DEBUG
Location = Screen.AllScreens.First(s => !s.Primary).Bounds.Location;
#endif
}
If you've only got two monitors hooked up, it'll work fine. You could also get more creative and center the application window on the other monitor, maximize it, whatever. The #if could be substituted with if (System.Diagnostics.Debugger.IsAttached) as suggested by #Daniel if you wanted. I used the former just to present another alternative.
Im trying to move my cursor according to my hand point in kinect, I can get the real coordinates I mean I can move an image on screen but I want real cursor to be settled according to my hand coordinates. I tried Console.SetCursor(x,y) but it gives exception I also tried to download windows forms dll but I cant find the version 4.00 . Is there any simple way to set cursor in a desired position? (which is working by the way and as I said Console.SetcursorPosition is not wodking?)
You didn't provide very much information about you app but I suspect that you just need to assign to Cursor.Position from System.Windows.Forms. You may need to add a reference to System.Windows.Forms in order to gain access to this, depending on exactly what type of project you have.
If you want to keep it lightweight and avoid taking a reference to WinForms then you could just pinvoke to SetCursorPos.
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
Just use
Cursor.Position = new Point();
You can find more information's here
Thank you for question and answer.
I found strange and unobvious behavior.
Let you use multi-monitor (two or three monitor) configuration, and use two windows in your application.
One window is active, and you set cursor position for second windows.
And this two windows located on different monitors.
Then, you need call SetCursorPos TWICE!
TWICE, who would have thought?
Firs call move cursor to virtual borderline between monitors.
And only second call move cursor to required position second window!
Example of code (after 6 hours of experiments):
SetForegroundWindow(this.Handle); // set second window active
SendMessage(this.Handle, 0x20, this.Handle, (IntPtr)1); // Send WM_SETCURSOR
SetCursorPos(400, 600);
Thread.Sleep(50);
SetCursorPos(400, 600);