Remove double-click style from control in C# - c#

I have a PictureBox control which internally generates the following Windows messages when I click rapidly:
* WM_LBUTTONDOWN
* WM_LBUTTONUP
* WM_LBUTTONDBLCLK
* WM_LBUTTONUP
Is there any way to remove the double-click style from the control, perhaps in the form's constructor? In Delphi I am able to do this using this code:
// Prevent TImage from generating OnDblClick event notifications
btnShift.ControlStyle := (btnShift.ControlStyle - [csDoubleClicks]);
Without handling both the Click and DoubleClick events, how would I achieve the same thing in C#?

Override the default class styles in a new control
using System;
using System.Windows.Forms;
namespace NoDblClickPic
{
public partial class NoDblClickPicControl : PictureBox
{
private const int CS_DBLCLKS = 0x8;
public NoDblClickPicControl()
{
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle &= ~CS_DBLCLKS;
return cp;
}
}
}
}

Related

Keeping docked right control visually stable when resizing the window from the left side

In the code below, when the Form is resized using the left bar, the Button appears to be pulled away from the right edge of the window, even though the RTL flag is set. Is there a way to keep the button from visually bouncing when the window is resized from the left?
For example, if the RTL flag line is commented out, and the Form is resized from the right bar, then the button is visually stable, always appearing exactly against the leftmost border.
Tested using:
Windows 8.1 Pro with Media Center (Version 6.3 build 9600) for .NET 4.5.2 and .NET 4.7.2
Windows Server 2016 (versions to be determined).
Animated gif:
public class MyForm : Form {
private const int WS_EX_LAYOUTRTL = 0x00400000;
LE le = new LE();
public Button btn = new Button { Text = "Button" };
public MyForm() : base() {
Controls.Add(btn);
}
protected override CreateParams CreateParams {
get {
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_LAYOUTRTL; // comment out this line and resize from the right, the button stays still
return cp;
}
}
public override LayoutEngine LayoutEngine {
get {
return le;
}
}
// empty layout engine
private class LE : LayoutEngine {
public override void InitLayout(object child, BoundsSpecified specified) {}
public override bool Layout(object container, LayoutEventArgs layoutEventArgs) {
return false;
}
}
}

Fix Shadow Around Form WinForm

I try to add shadow around Form So I put this in my form code:
protected override CreateParams CreateParams
{
get
{
const int CS_DROPSHADOW = 131072;
CreateParams cparams = base.CreateParams;
cparams.ClassStyle = CS_DROPSHADOW;
return cparams;
}
}
it work perfect but when i click on parent form by mistake the shadow remove and i can't return it back how can i fix it?

Cannot get ListView to stop flickering

C# .Net 4.5
I have a ListView that is constantly being updated by different threads through this method:
public void UpdateUI_List_SetRow(int rowNum, ListViewItem item)
{
lock (lock_List)
{
try
{
if (this.lstStatus.InvokeRequired)
lstStatus.Invoke(new MethodInvoker(() => lstStatus.Items[rowNum] = item));
else
lstStatus.Items[rowNum] = item;
}
catch (Exception ex)
{
ErrorLogging.Log(ex);
}
}
}
It constantly flickers while being updated. I have tried the DoubleBuffered fix (How to prevent flickering in ListView when updating a single ListViewItem's text?) but it didn't work.
Sorry if this is a duplicate post but I couldn't find any solution other than enabling double buffering.
Subclass ListView to enable double buffering:
namespace System.Windows.Forms
{
public class ListViewEx : ListView
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
}
}
Code provided in accepted answer works, but ruins selection rectangle. The solution below works like a charm. It enables double buffering and retains correct rectangle.
public partial class myListView : ListView
{
public myListView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
}
}
How to use it - add this code to your namespace and use myListView instead of conventional ListView.

WinForms child form to another process

I'm trying to create child form on other process form without activation, everything is ok except that my form appearing in front of screen when showing - after f.Visible=true - but i need to keep both of them somewhere behind.
Here's code:
public class OSD {
public class OSD_form:Form {
protected override bool ShowWithoutActivation {
get { return true; }
}
protected override CreateParams CreateParams {
get {
CreateParams p=base.CreateParams;
//p.Style |= 0x40000000; // WS_CHILD
p.ExStyle|=0x8000000; // WS_EX_NOACTIVATE
p.ExStyle|=0x00080000; // WS_EX_LAYERED
p.ExStyle|=0x0020; // WS_EX_TRANSPARENT
return p;
}
}
}
private OSD_form f=null;
public IntPtr AttachedTo=IntPtr.Zero;
public OSD() {
f=new OSD_form();
f.FormBorderStyle=FormBorderStyle.None;
f.ShowInTaskbar=false;
}
public void Show() {
long l2=winapi.GetWindowLong(f.Handle,winapi.GWL_EXSTYLE);
l2|=winapi.WS_EX_LAYERED;
l2|=winapi.WS_EX_TRANSPARENT;
winapi.SetWindowLong(f.Handle,winapi.GWL_EXSTYLE,new IntPtr(l2));
winapi.SetLayeredWindowAttributes(f.Handle,0,(255*90)/100,winapi.LWA_ALPHA);
window w=new window(AttachedTo);
f.Width=160;
f.Height=60;
f.Left=w.Client.Left+4;
f.Top=w.Client.Top+4;
f.Show(new WindowWrapper(AttachedTo));
f.Visible=true;
}
}

Opening a form in C# without focus

I am creating some always-on-top toasts as forms and when I open them I'd like them not to take away focus from other forms as they open. How can I do this?
Thanks
protected override bool ShowWithoutActivation
{
get
{
return true;
}
}
Override this property in your form code and it should do the trick for you.
It took me a few minutes using Adam's & Aaron's info above, but I finally got it to work for me. The one thing I had to do was make sure the form's Top Most property is set to false. Here is the code I used...
protected override bool ShowWithoutActivation { get { return true; } }
protected override CreateParams CreateParams
{
get
{
//make sure Top Most property on form is set to false
//otherwise this doesn't work
int WS_EX_TOPMOST = 0x00000008;
CreateParams cp = base.CreateParams;
cp.ExStyle |= WS_EX_TOPMOST;
return cp;
}
}

Categories

Resources