I have created a small Window without border by WinApi functions in C#. I want to move this window when right mouse button is pressed. I am trying to catch a mouse offset by anylizing WM_MOUSEMOVE event. It seems to work and i can move my window holding Right Mouse Button.
But I am loosing control of the window when i move my mouse too fast. That's because my Window is too small and if mouse leaves window very quick it doesn'n recieve WM_MOUSEMOVE messages anymore and i can't calculate a MouseOffset to move my Window.
So, How I can fix that?
You need to call SetCapture to tell Windows that your hwnd wants all the events even when the mouse isn't physically over the window. http://msdn.microsoft.com/en-us/library/windows/desktop/ms646262(v=vs.85).aspx
You can tell Windows that the user actually moused down on the title bar and it will handle the rest automatically for you.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ReleaseCapture();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
internal const uint WM_NCLBUTTONDOWN = 0xA1;
internal const int HTCAPTION = 2; // Window captions
internal const int HTBOTTOMRIGHT = 17; // Bottom right corner
/// <summary>
/// Simulates a Windows drag on the window border or title.
/// </summary>
/// <param name="handle">The window handle to drag.</param>
/// <param name="dragType">A HT* constant to determine which part to drag.</param>
internal static void DragWindow(IntPtr handle, int dragType) {
User32.ReleaseCapture();
User32.PostMessage(handle, User32.WM_NCLBUTTONDOWN, new IntPtr(dragType), IntPtr.Zero);
}
Related
Essentially cannot find and answer to this question, or if it is even possible.
I have a game I am creating for a class, and it simply looks better when forced full screen and when the zoom is set to a particular size. I was wonder if I could recreate this without the player being necessary to change it themselves.
ALT + ENTER Full screen
And
CTRL + Scroll wheel zoom
For a literal answer to your question on how to:
Send keys to go Fullscreen, and
Send a Ctrl+MouseWheel
You want some help from the Win32 interop to send keyboard & mouse messages to your console window.
using System.Runtime.InteropServices;
public class Win32
{
public const int VK_F11 = 0x7A;
public const int SW_MAXIMIZE = 3;
public const uint WM_KEYDOWN = 0x100;
public const uint WM_MOUSEWHEEL = 0x20A;
public const uint WHEEL_DELTA = 120;
public const uint MK_CONTROL = 0x00008 << 16;
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
As reference the magic numbers are from:
Virtual Keys (VK_*)
Window input messages (WM_*)
Mousewheel params (WHEEL_DELTA & MK_*)
and the ShowWindow params (SW_*)
You could then simply send your keypress and mousewheel like so:
using static Win32;
// Get window handle of the console
var hwnd = GetConsoleWindow();
// Go fullscreen by sending the F11 keydown message.
PostMessage(hwnd, WM_KEYDOWN, (IntPtr)VK_F11, IntPtr.Zero);
// Or maximize the window instead. Your users may not know how to get out of fullscreen...
/// ShowWindow(hwnd, SW_MAXIMIZE);
// Send mouse wheel message.
// MK_CONTROL: Holds the Ctrl key. WHEEL_DELTA: Positive=Up, Negative=Down.
PostMessage(hwnd, WM_MOUSEWHEEL, (IntPtr)(MK_CONTROL | WHEEL_DELTA), IntPtr.Zero);
Alternatively, as #JeremyLakerman mentioned in a comment to your question, you could set the console font to a larger size; which is a lot better, but also a bit more involved than sending Ctrl+MouseWheel.
I am trying to create a form in C# that is fully transparent, but will not allow clicks to go through it to the other windows below.
I have found two methods that were promising, but did not achieve the results I wanted.
The first is by setting the background color and transparency key to the same value. This gives me the transparent form, but clicking goes through.
this.BackColor = Color.Red;
this.TransparencyKey = Color.Red;
The other thing I tried is to set the opacity of the form to 1%. This creates the effects I wanted - almost. I get a 99% transparent form, but there is a slight color alteration to whatever is underneath the form. Since the app I am making is meant to be used in color-sensitive context (graphic design and such), even a tiny alteration of the colors is unacceptable. So I turn to you, dear SO. Can this be done?
I have found the solution, and I am sharing it with you guys as well.
The answer was quite simple: I set: this.TransparencyKey = Color.Lime;
And then I used a 1x1px Lime PNG as the background image. This also has the added benefit of not obscuring the form border and title bar. I will remove them later, but at the moment, it's useful to know where the form is located.
I found a solution to this completely by accident.
I wanted a click through transparent window and got it by using the answer in this SO question:
C# cursor highlighting/follower
That answer uses a transparency filter of LightGreen, but I thought that I might need to use that Color so I changed it to AliceBlue and click through stopped working. I switched back to LightGreen and it started working again.
Instead of trying to capture mouse actions on the transparent form, you can try just capturing mouse actions system-wide (clicks, moves) and handle them as you need.
This can be done in the following way (assuming the drawn-on form keeps maximized. If not, see the next paragraphs below):
Take screenshot of the current screen.
Create a form and use the screenshot as the background image.
Remove form title from the form, simply make it as a panel.
While the solution above solves what you want, you need to answer the question:
How will the user close the form he's drawing on?
If the form needs to be resized-moved - complicated version
However, if you want to resize this form (just noticed your edit with the new screenshots), then you need to cut the part of the taken screenshot and show it as the background of the form. But this goes farther then: you need to do it every time the form is resized or moved.
I personally would take the first (simpler) approach.
You can set hook to catch the mouse events.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public class MouseStruct
{
public Point pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
private int hHook;
public const int WH_MOUSE_LL = 14;
public static HookProc hProc;
public int SetHook()
{
hProc = new HookProc(MouseHookProc);
hHook = SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
return hHook;
}
public void UnHook()
{
UnhookWindowsHookEx(hHook);
}
//callback function, invoked when there is an mouse event
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
var MyMouseStruct = (MouseStruct)Marshal.PtrToStructure(lParam, typeof(MouseStruct));
if (nCode < 0)
{
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
else
{
switch wParam{
case (IntPtr)513:
//click
//do whatever you want
case (IntPtr)512:
//move
case (IntPtr)514:
//release
default:
}
Cursor.Position = MyMouseStruct.pt;
//stop the event from passed to other windows.
return 1;
}
}
More details at MSDN.
Winforms App. .Net 3.5.
I need to set the focus from my C# application to the user's desktop (almost like simulating a mouse click on the desktop).
Can someone please show me how to do this with C#? I just want to set focus on the desktop so the focus is no longer on my application but I want to do this from within my application.
Edit: An answer below works by setting the focus to the desktop, but it minimizes all the open windows on the user's desktop.
Is there a way I can maybe set the focus to the next open window on the desktop instead? I just want to get the focus off of my application (without minimizing my application or hiding it). I just want to move focus to somewhere else. Maybe the desktop was not the best choice if it will minimize all the user's open windows/applications.
This should do it for you.
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1 {
class Program {
[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;
static void Main(string[] args) {
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero);
System.Threading.Thread.Sleep(2000);
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
}
}
}
Get Next Window
I don't have a code example ready for these two but I'm going to give you the links to both. The first think you need to do is call GetWindow. After doing that you'll want to call SwitchToThisWindow passing in the pointer you received from GetWindow.
You can add this COM object in your project:
Microsoft Shell Controls And Automation
And then just call:
Shell32.ShellClass shell = new Shell32.ShellClass();
shell.MinimizeAll();
This will minimize all the windows and then focus the desktop. Otherwise, if you have your window non-full screen then you can simulate the mouse click using:
//This is a replacement for Cursor.Position in WinForms
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool SetCursorPos(int x, int y);
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
//This simulates a left mouse click
public static void LeftMouseClick(int xpos, int ypos)
{
SetCursorPos(xpos, ypos);
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
You can calculate coordinates by looking at your window startup location plus height/width and select a available space (that will be the desktop indeed).
The MSV-Studio description for Locked is "The Locked property determines if we can move or resize the control" so I set the winforms Locked property to true but the form is still movable.
What is the correct way to prevent the form from moving?
Maximize it. Thanks, JackN. ;-)
I use the following code to display a form dialog window for a corporate security application written in-house - one of the requirements was that the form could not be moved, resized or live under any other form. Anyway, see below for a start...
/// <seealso href="http://msdn.microsoft.com/en-us/library/ms633548(v=vs.85).aspx"/>
/// <seealso href="http://msdn.microsoft.com/en-us/library/ms633545(v=vs.85).aspx"/>
public class ShowMessage
{
const int SW_SHOWMAXIMIZED = 3; //for maximising (if desired)
const int SW_SHOW = 5; //for simply activating the form (not needed)
const int SW_SHOWNORMAL = 1; //displays form at original size and position (what we use here)
const UInt32 SWP_NOSIZE = 0x0001; //cannot be resized
const UInt32 SWP_NOMOVE = 0x0002; //cannot be moved
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); //always lives at the top
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE; //sets the flags for no resize / no move
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
/// <summary>
/// Displays the passed form using the parameters set in the base ShowMessage class
/// </summary>
/// <param name="frm">A Windows Form object</param>
/// <example><code>ShowMessage.ShowTopmost(new myForm());</code></example>
public static void ShowTopmost(Form frm)
{
ShowWindow(frm.Handle, SW_SHOWNORMAL); //shows the form
SetWindowPos(frm.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); //sets the form position as topmost, centered
}
}
Then I simply call
ShowMessage.ShowTopmost(new frmMessage());
I'm not saying it's the only way or the right way, but it a way to do it.
It's generally bad form to prevent the user from moving the window. The user should be able to have the window wherever he wants. Preventing resizing is one thing, preventing moving is another. I'm not aware of any C# native way of doing this, but you can probably hook down into Win32 to prevent the window from moving.
You might be able to use the Move event of the form and set the form back to the starting position. You would have to capture and store (in memory) the starting position.
I have a scenario, i need to send click events to an independent application. I started that application with the following code.
private Process app;
app = new Process();
app.StartInfo.FileName = app_path;
app.StartInfo.WorkingDirectory = dir_path;
app.Start();
Now i want to send Mouse click message to that applicaiton, I have specific coordinates in relative to application window. How can i do it using Windows Messaging or any other technique.
I used
[DllImport("user32.dll")]
private static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
It works well but cause the pointer to move as well. So not fit for my need.
Then i use.
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
It works well for minimize maximize, but do not work for mouse events.
The codes for mousevents i am using are,
WM_LBUTTONDOWN = 0x201, //Left mousebutton down
WM_LBUTTONUP = 0x202, //Left mousebutton up
WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
WM_RBUTTONDOWN = 0x204, //Right mousebutton down
WM_RBUTTONUP = 0x205, //Right mousebutton up
WM_RBUTTONDBLCLK = 0x206, //Right mousebutton do
Thanks for the help in advance, and waiting for feedback.
For a single click you should send two mouse event at the same time for an exact mouse click event.
SendMessage(nChildHandle, 0x201, 0, 0); //Mouse left down
SendMessage(nChildHandle, 0x202, 0, 0); //Mouse left up
It is working in my project.
Save the cursor position, use mouse_event and move the cursor back. mouse_event is really the best way to do this.