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

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

Related

How to make invisible form when screen capture is running

is there any one can help me?
i am creating a windows application in c#. and i want to make it invisible when i do screen recording. i mean, it will not record but i can use it.
i try displayaffinity
but this makes form black when record. but i want full invisible.
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
public static extern uint SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity);
private void Form1_Load(object sender, EventArgs e)
{
const uint WDA_NONE = 0;
const uint WDA_MONITOR = 1;
SetWindowDisplayAffinity(this.Handle, WDA_MONITOR);
}
}
}

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

How to drag Custom Usercontrol on windows form?

I have a sln file with two projects in it, the first project contains a Custom Usercontrol inherited from Button, the second project has a form and a button on it, when i click the button on the form the Custom Usercontrol which is a button will be added to the form, now i should be able to drag the button on the form wherever i want to on the form after i run it, how to do it.
public partial class buttCustom : Button
{
public buttCustom()
{
InitializeComponent();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.Controls.Add(buttControl);
}
}
i should be able to move that green custom button in the image to move anywhere i want using mouse.
I think you can add the code below in your custom User Control class.
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr one, int two, int three, int four);
private void CustomCtrl_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, 0x112, 0xf012, 0);
}
So the result would be something like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyViewer.CustomControls
{
public partial class CustomCtrl : UserControl
{
public string formTitleText
{
get { return label1.Text; }
set { label1.Text = value; }
}
public CustomCtrl()
{
InitializeComponent();
}
[DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
private extern static void ReleaseCapture();
[DllImport("user32.DLL", EntryPoint = "SendMessage")]
private extern static void SendMessage(System.IntPtr one, int two, int three, int four);
// Add MouseDown Event
private void CustomCtrl_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(Handle, 0x112, 0xf012, 0);
}
}
}
Then you can add the custom User Control which can be draggable to your form from the toolbox.
Agula Otgonbaatar has the correct answer, however I use these values
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
I also check for MouseButton.Left in my MouseDown handler, so it would be:
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
This works for me.

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.

Fire DoubleClick event on ListView in 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.

Categories

Resources