Fire DoubleClick event on ListView in C# - c#

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.

Related

C# . How to take effect for regitstry edited Caret Width immediately?

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
}
}
}

Invoke error why trying to run autoclicker

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"

C# how to toggle simulated mouse clicks with left mouse button

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);
}
}

Webbrowser disable all audio output - from online radio to youtube

My webbrowser:
XAML:
//...
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
//...
<my:WindowsFormsHost Name="windowsFormsHost"/>
Code behind C#:
System.Windows.Forms.WebBrowser Browser = new System.Windows.Forms.WebBrowser();
windowsFormsHost.Child = Browser;
My question is how to disable all audio output.
I found this:
C#:
private const int Feature = 21; //FEATURE_DISABLE_NAVIGATION_SOUNDS
private const int SetFeatureOnProcess = 0x00000002;
[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(int featureEntry,
[MarshalAs(UnmanagedType.U4)] int dwFlags,
bool fEnable);
Its fine, but this code disable only "click" sound, so its kind of useless in this case.
I just want from my application 100% mute, no sounds at all.
I've read that in this webbrowser it need to be done through Windows Sounds, but I cant really bielieve that I cant do this in code.
Here is how you can do it with ease. Not specific to WebBrowser though, but does what you requested: I just want from my application 100% mute, no sounds at all.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WinformsWB
{
public partial class Form1 : Form
{
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr h, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr h, uint dwVolume);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// save the current volume
uint _savedVolume;
waveOutGetVolume(IntPtr.Zero, out _savedVolume);
this.FormClosing += delegate
{
// restore the volume upon exit
waveOutSetVolume(IntPtr.Zero, _savedVolume);
};
// mute
waveOutSetVolume(IntPtr.Zero, 0);
this.webBrowser1.Navigate("http://youtube.com");
}
}
}
You can try as well to use DISPID_AMBIENT_DLCONTROL
DLCTL_DLIMAGES, DLCTL_VIDEOS, and DLCTL_BGSOUNDS: Images, videos, and background sounds will be downloaded from the server and displayed or played if these flags are set. They will not be downloaded and displayed if the flags are not set.

How to manually raise an event by supplying X, Y and Windowhandle parameters?

We are working on a module in which I gave the xpos, ypos, windowhandle.
Now what I want to do is I want to raise the event for the corresponding input on (xpos, ypos, windowhandle).
For example:
The input given is to select a file means it will select a file automatically through my module.
00010086 PWM_LBUTTONDBCLK fwKeys:MK_LBUTTON xPos:273yPos:354
The event will look like the above.
Pseudo code.. You can modify to suit your need...
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class Form1 : Form
{
[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 Form1()
{
}
public void DoMouseClick()
{
//Call the imported function with the cursor's current position
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}
//...other code needed for the application
}

Categories

Resources