Since getting a satisfactory answer on SuperUser is very difficult, I want to rephrase this question and ask:
Is there any way to programmatically detect a mouse was plugged in the usb port, and change the cursor speed in windows (perhaps through an API)?
I'd like to use C#, but I'm open to any language that can run on a windows 7 machine.
I don't know about the detection but you can use P/Invoke to the SystemParametersInfo api using
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(SPI uiAction, uint uiParam, String pvParam, SPIF fWinIni);
with the uiAction as (SPI_SETMOUSESPEED) = 0x0071
Related
I'm building a universal Windows app and need to lock down the desktop so that the user can't escape out of the program. So I need to temporarily disable the Windows key and ctrl + alt + del.
I was able to add a event handler like so:
Window.Current.CoreWindow.KeyDown += (s, e) =>
{
if(e.VirtualKey == VirtualKey.LeftWindows || e.VirtualKey == VirtualKey.RightWindows)
{
e.Handled = true;
}
}
But the keypress event still fires.
I also found a method using what I believe are Windows hooks from the user32 lib.
[DllImport("user32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterHotKey(IntPtr hwnd, int id, uint fsModifiers, uint vk);
[DllImport("user32", SetLastError = true)]
public static extern int UnregisterHotKey(IntPtr hwnd, int id);
[DllImport("kernel32", SetLastError = true)]
I found some sample code here, but I think I'll have to tweak it for a simple Windows key press.
Is the Windows hook method (as used in the example link) the way to go?
I think Kiosk apps for assigned access was very close to your requirement. This document describes how to implement a kiosk app. You could use the Lock framework and assigned access to create a kiosk app that enables users to interact with a single app on a device.
Set up a kiosk on Windows 10 Pro, Enterprise, or Education for your reference.
I'd like to create an application where I can resize Windows forms.
I can iterate through windows and get certain properties like so for example:
How to iterate through window elements using JNA?
I've found similar ones, but can I resize and relocate these windows rather than just getting their attributes like title and rect? I'd like to sort all visible windows as a tile but I can't figure out how I can manipulate their position.
For example, I want to grab and resize Total Commander and Notepadd++ (the 2 visible windows) to be on the left and right half of the screen.
Is it possible? If anyone can show me a snippet I'd be grateful.
Easiest way would be to enumerate all window instances in system and call proper WinAPI function like described here
C#: You can obtain all the windows you can use this code:
internal static class WindowsEnumeratorNativeMethods {
[return: MarshalAs(UnmanagedType.Bool)]
internal delegate Boolean EnumerationCallback(IntPtr handle, IntPtr parameter);
[DllImport("User32.dll",
CallingConvention = CallingConvention.Winapi,
ExactSpelling = true,
EntryPoint = "EnumWindows",
CharSet = CharSet.Unicode,
SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean EnumWindows(EnumerationCallback lpEnumFunc,
IntPtr lParam);
}
...
List<IntPtr> handles = new List<IntPtr>();
WindowsEnumeratorNativeMethods.EnumWindows(
(h, p) => {
handles.Add(h);
return true;},
IntPtr.Zero);
Next you should find out the required windows (Notepad++ etc). You can do it
by looking for windows' captions (see GetWindowText API function); to move/size
the window use SetWindowPos API function.
While implementing API wrappers, you may find this useful:
http://www.pinvoke.net/
In my C# console application, I am using SendMessage() to minimize all windows, effectively showing the Windows 8 Legacy Desktop. This works great, but I have to use a Thread.Sleep(1000) in order to wait for the Legacy Desktop to actually show before before I try to do anything else.
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
Thread.Sleep(1000);
I really want to replace the Thread.Sleep() with a more efficient way to detect that the Legacy Desktop is showing before continuing on.
Any ideas?
Edit:
Here are the Interop wrappers and constants. just in case it helps..
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
I'm not sure if this will work any better for you, but perhaps it's worth a try...
(1) Add to your project a reference to "Shell32" (via Add Reference -> COM -> Microsoft Shell Controls and Automation).
(2) Set the reference's "Embed Interop Types" to false.
(3) Use the following code to minimise all the windows:
dynamic shell = new Shell32.ShellClass();
shell.MinimizeAll();
However, I suspect that this is just an alternative way of doing the SendMessage().
Does anyone out there have any experience programatically retreiving the lat/long from a GPS attached to a mobile PC? A team I'm on is currently looking at hardware options--as the programmer who will eventually have to live with whatever is selected I was wondering if anyone out there has had experience writing .Net programs that interface with a GPS? Any recomendations for hardware and/or programming would be greatly appreciated.
As I envision it, my application will need to ask the GPS for the current lat/long perhaps once every 10 to 20 seconds.
I've written such an application before.
As Henk said, you listen on a COM port. Build a component that reads the com stream in, in say a 1024 buffer. that'll be plenty to contain at least 1 complete NMEA sentence. From there, read the input until you find the start of a sentence and parse it. If for some reason you don't have the full sentence, read in another buffer, append and continue/try again.
If you're willing to be dependent on Windows 7, there's a Location API that handles the NMEA decoding for you.
If the Gps is integrated within your windows CE PC or windows mobile phone, you can simply use the GPS Intermediate Driver to pool for information.
Since you are in a .net environment, you could create a .Net wrapper to this native API.
public class GpsHardware
{
private const string gpsLibraryName = "gpsapi.dll";
private const string coreLibraryName = "coredll.dll";
[DllImport(GpsHardware.coreLibraryName, SetLastError = false)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EventModify(IntPtr hEvent, uint function);
[DllImport(GpsHardware.gpsLibraryName, SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr GPSOpenDevice(IntPtr hNewLocationData, IntPtr hDeviceStateChange, string szDeviceName, uint dwFlags);
[DllImport(GpsHardware.gpsLibraryName, SetLastError = true)]
private static extern uint GPSCloseDevice(IntPtr hGPSDevice);
[DllImport(GpsHardware.gpsLibraryName, SetLastError = true)]
private static extern uint GPSGetPosition(IntPtr hGPSDevice, IntPtr pGPSPosition, uint dwMaximumAge, uint dwFlags);
[DllImport(GpsHardware.gpsLibraryName, SetLastError = true)]
private static extern uint GPSGetDeviceState(IntPtr pGPSDevice);
...
}
Of course you will have to deal with marshaling and all the great interop things :)
I need to wake up a hibernated laptop at a given time every day.
Should I use pinvoke? If yes? which one? How?
You can wake the computer up from sleep, I'm not sure about hibernate. This example shows you how to do it. In short you use these two imports:
[DllImport("kernel32.dll")]
public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWaitableTimer(SafeWaitHandle hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume);
I've only tested it on Windows Vista and 7, these may not be available on XP.
I know it's VB and not C#, but take a look at this example, it does require that your motherboard meets certain requirements.