I would like to so when I hold my left mouse button down it simulates clicks but this is not working as it's not simulating clicks but if I remove the if statement in the timer1, it simulates clicks but I want it to only simulate when I'm holding left mouse.
Here is my code so far what I've done. if you could help that would be great
public partial class Form1 : Form
{
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
//Mouse actions
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.Left){
DoMouseClick();
}
}
public void DoMouseClick()
{
//Call the imported function with the cursor's current position
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
}
Related
I found a path for Caret Width in my registry: HKEY_USERS\S-1-5-21-1217365396-2387141574-3682890637-1001\Control Panel\Desktop. Value "CaretWidth" = 1. I want to change it for 5, for example.
But how take effect for this change immediately? I think need to use some method from pinvoke.net, but I don't know how do it. Can you help me?
You can use SystemParametersInfo and pass SPI_SETCARETWIDTH
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices; // Add
namespace Caret_Changer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDCHANGE = 0x02;
public const uint SPI_SETCARETWIDTH = 0x2007;
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, uint pvParam, uint fWinIni);
private void ChangeCaret(uint caret)
{
SystemParametersInfo(SPI_SETCARETWIDTH, 0, caret, SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
}
// Start button
private void Button1_Click(object sender, EventArgs e)
{
ChangeCaret(0x0000005); // New caret width
}
// Stop Button
private void Button2_Click(object sender, EventArgs e)
{
ChangeCaret(0x0000001); // Return default width
}
}
}
I want to make an autoclicker that will click when i press "F" button; But each time I try to press "F" I get invoke error.
I tried to run Click thread in another void but it didn't work.
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
UserActivityHook actHook;
static bool autoclickerToggle = false;
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public Form1()
{
InitializeComponent();
actHook = new UserActivityHook(); // crate an instance with global hooks
// hang on events
actHook.OnMouseActivity += new MouseEventHandler(MouseMoved);
actHook.KeyDown += new KeyEventHandler(MyKeyDown);
actHook.KeyPress += new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp += new KeyEventHandler(MyKeyUp);
}
public void MouseMoved(object sender, MouseEventArgs e) { }
public void MyKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F)
{
AutoclickerToggle();
}
}
public void MyKeyPress(object sender, KeyPressEventArgs e) { }
void AutoclickerToggle()
{
if (autoclickerToggle)
{
autoclickerToggle = false;
}
else
{
autoclickerToggle = true;
Thread Click = new Thread(() => Clicker());
Click.Start();
}
}
public void MyKeyUp(object sender, KeyEventArgs e) { }
public void Clicker()
{
while (autoclickerToggle)
{
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
Thread.Sleep(100);
}
}
As I said I've got this error when pressing "F" (used translator there, because I had this error in polish language) "Calling PInvoke 'Test! Test.Form1 :: mouse_event 'has upset the balance of the stack. The likely cause is a mismatch between the managed PInvoke signature and the unmanaged target signature. Verify that the called convention and signature parameters of the PInvoke function match the unmanaged destination signature"
Could someone explain to me how this code work
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void mainForm_MouseDown(object sender, MouseEventArgs e)
{ //When a mouseButton is pressed down on the form
if (e.Button == MouseButtons.Left)
{ // if it is mouseButton1
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
I know it gives my form the possibility of being dragged around, but i don't understand how.
Thanks!
As title says everything clear. I want to open this menu via my Windows Application. Thanks.
Use the following code:
private void button1_Click(object sender, EventArgs e)
{
KeyDown(ConsoleKey.LeftWindows);
KeyDown(ConsoleKey.P);
KeyUp(ConsoleKey.LeftWindows);
KeyUp(ConsoleKey.P);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;
public static void KeyDown(ConsoleKey vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
public static void KeyUp(ConsoleKey vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
Use code like in this post:
SendKeys.Send and Windows Key
to send your Windows+P keystrokes.
Is it possible to fire a DoubleClick event on a ListView programmatically? Without needing to know the location/signature of the event handler?
if I understood what you want instead doing so:
private void MouseDoubleClick(object sender, EventArgs e)
{
//some code on mouse double click
}
make:
private void MethodToExecuteOnDoubleClick()
{
//some code on mouse double click
}
private void MouseDoubleClick(object sender, EventArgs e)
{
MethodToExecuteOnDoubleClick();
}
and then you can call MethodToExecuteOnDoubleClick() whenever you want without need to rise doubleclick event
I blogged about that a while ago: Simulate a Click; it is not a real click, but it triggers the event handlers. The blog say "OnClick", replace it with "OnDoubleClick" and you should be fine.
For simulate mouse click you can do something like this:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
//....
[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x10;
public void DoMouseClick()
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
//...
}
It's better to create an encapsulating control and to handle any logic that you might need in there.