Detect if pressing a mouse button and which WinForm c# - c#

I don't want to click on a button or the form, I just want to know if user is pressing the left mouse button while the cursor is in the form.
I've tried this:
private void PlayForm_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.LButton)
{
...
}
}
but it doesn't work.
I also tried PlayForm_Click() but it works only when the click is on the 'canvas' if there's something else on top it won't work

If you just want to know if the left mouse button is down while executing some other code in the Form you can look at the static property Control.MouseButtons, which returns a value from the MouseButtons enumeration .E.g.:
if ((Control.MouseButtons & MouseButtons.Left) != 0)

you could use the mouse enter/leave to set a boolean that the mouse cursor is over the form, then you could Use the Mouse.
...
bool mouseOverMe;
public MainWindow()
{
InitializeComponent();
mouseOverMe = false;
}
private void Window_MouseEnter(object sender, MouseEventArgs e)
{
mouseOverMe = true;
}
private void Window_MouseLeave(object sender, MouseEventArgs e)
{
mouseOverMe = false;
}
void doSomething()
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
if (mouseOverMe)
MessageBox.Show("Im a mouse down in the window");
}
...
something sorta like this.

As far as I've understood, you want a handler for a click on whatever is in the form. I'd suggest you could iterate trough all the controls in the form on Form_Load event and just set a common handler for the MouseClick (or KeyPressed or whichever event you want according to your current need) for all controls present in the .Controls collection in the moment the form is loaded and you should register the same handler for the form itself (a.k.a. this.MouseClick). This will be a bit of an overkill if you'd later want to register a MouseClick handler for a particular control, but you can always compare the sender object and get the data from there. Example code is not present for now, since I'm typing from my phone. Will update later.

The main problem is that the form doesn't get any messages when a message is sent directly to a child control.
One way around this is to register an application-wide message filter. Note that the following implementation is rather inefficient (and quite ugly), but it should show you the basic idea:
void Main()
{
var form = new Form();
form.Load += (s, _) => Application.AddMessageFilter(new MyFilter((Form)s));
var pnl = new Panel();
pnl.Controls.Add(new Button());
form.Controls.Add(pnl);
Application.Run(form);
}
public class MyFilter : IMessageFilter
{
Form form;
public MyFilter(Form form)
{
this.form = form;
this.form.Disposed += (_, __) => Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message msg)
{
const int WM_LMOUSEDOWN = 0x0201;
if (msg.Msg == WM_LMOUSEDOWN && msg.HWnd != IntPtr.Zero
&& Control.FromHandle(msg.HWnd).TopLevelControl == form)
{
Console.WriteLine("Hi!");
}
return false;
}
}

Related

Prevent selecting text in a TextBox

Similar questions have been already asked (e.g., here), however I've not found an answer for my specific case. I'm building a custom control based on a DevExpress control, which in turns is based on standard TextBox and I've a flickering problem that seems due to the base TextBox component, which tries to update selection.
Without explaining all the details of my custom control, to reproduce the problem you just need to place a TextBox inside a Form and then use this code:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.MouseMove += TextBox1_MouseMove;
}
private void TextBox1_MouseMove(object sender, MouseEventArgs e) {
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
}
}
If you launch it, you click on the TextBox and then you move the cursor toward right you will notice the flickering problem (see video here). For my custom control I would need to avoid this flickering. I'm bound to use a TextBox (so no RichTextBox). Any idea?
The solution has been provided in the meantime by Reza Aghaei by overriding WndProc and intercepting WM_SETFOCUS messages. See here
Depending on what u want to do there are several solutions:
If you want to prevent the selection it would be:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
(sender as TextBox).SelectionLength = 0;
}
Or for selecting all:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
(sender as TextBox).SelectAll();
}
And besides that u also could specify the conditions for selecting, for example:
private void TextBox1_MouseMove(object sender, MouseEventArgs e)
{
(sender as TextBox).Text = DateTime.Now.Ticks.ToString();
if (MouseButtons == MouseButtons.Left) (sender as TextBox).SelectAll();
else (sender as TextBox).SelectionLength = 0;
}
But as long as you want to select the text you always will get some flickering, because a normal Textbox has not the possibility to use things like BeginEdit and EndEdit and so it will change the text first and then select it.
Looking at the video it looks like your textbox is calling WM_ERASEBKGND unnecessarily. In order to remedy this problem you can subclass the textbox class and intercept these messages. Below is sample code which should do the trick (untested) Disclaimer: I have used this technique for other WinForm controls that had the type of flicker shown in your video but not TextBox. If it does work for you, please let me know. Good luck!
// textbox no flicker
public partial class TexttBoxNF : TextBox
{
public TexttBoxNF()
{
}
public TexttBoxNF(IContainer container)
{
container.Add(this);
InitializeComponent();
//Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
//Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
//http://stackoverflow.com/questions/442817/c-sharp-flickering-listview-on-update
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}

C# Apply focused properties to FlowLayoutPanel like button behavior

I'm trying to apply focus behavior, similar to button blue border to FlowLayoutPanel. I tried to use GotFocus and LostFocus, but clearly that's not the way to go.
private void FlowLayoutPanel_Click(object sender, EventArgs e)
{
(sender as Control).BackColor = SystemColors.GradientActiveCaption;
//More operations.
}
private void Panel_LostFocus(object sender, System.EventArgs e)
{
(sender as Control).BackColor = default(Color);
//More operations.
}
While clicking on the FlowLayoutPanel nothing happens and while using tab the two event are invoked one after another.
Any suggestions?
FlowLayoutPanel is not a selectable control by default. You can create a custom flow layout panel by deriving from FlowLayoutPanel and set Selectable and UserMouse control styles to make it selectable by mouse. Also to accept tab stop, set TabStop property to true:
class ExFlowLayoutPanel:FlowLayoutPanel
{
public ExFlowLayoutPanel():base()
{
SetStyle(ControlStyles.Selectable, true);
SetStyle(ControlStyles.UserMouse, true);
TabStop = true;
}
}
Then you can handle GotFocus and LostFocus or Enter and Leave events.
The only point of using a FLP is to get it to arrange child controls. It is always a child control that gets the focus, not the FLP. So sure, nothing happens. You'd have to subscribe the Enter events of all the child controls to see the focus entering the panel or one of its children. Leave is much harder to get right, that's going to flicker like a cheap motel.
Very ugly solution, you don't want to it that way. Use the Application.Idle event instead, the best alternative when getting a reliable event just isn't practical. Check the Parent of this.ActiveControl, like this:
public Form1() {
InitializeComponent();
Application.Idle += CheckFlpFocus;
this.Disposed += delegate { Application.Idle -= CheckFlpFocus; };
}
private bool FlpHasFocus;
private void CheckFlpFocus(object sender, EventArgs e) {
bool hasFocus = false;
for (var ctl = this.ActiveControl; ctl != null; ctl = ctl.Parent) {
if (ctl == flowLayoutPanel1) hasFocus = true;
}
if (hasFocus != FlpHasFocus) {
FlpHasFocus = hasFocus;
flowLayoutPanel1.BackColor = hasFocus ? Color.Black : Color.White;
}
}

Register MouseEnter/MouseLeave events for disabled controls in windows form?

I want to register MouseEnter/MouseLeave events for disabled buttons. It does not work allthough it does work for enabled buttons..
//Enable Disable controls on form load
EnableDisableControls("Load");
var grupButtons = control.Controls.OfType<Button>();
foreach (Button btns in grupButtons)
{
//btns.MouseMove += new MouseEventHandler(MainframeDataExchangeTool_MouseMove);
btns.MouseEnter += new EventHandler(btns_MouseEnter);
btns.MouseLeave += new EventHandler(btns_MouseLeave);
}
private void btns_MouseEnter(object sender, EventArgs e)
{
}
private void btns_MouseLeave(object sender, EventArgs e)
{
var parent = sender as Control;
string tipstring = string.Empty;
if (parent == null)
{
return;
}
string enter = sender.GetType().ToString() + ": MouseEnter";
}
It is working for enable button ...but what to do for disable button ... I have to show tooltip operation on mouseenter and make it disapper immediately on mouseleave ?
yes , when you disable button , events will disable.
you can use this trick:
put your button in panel1 ,
then use the same event of button for panel1. like this:
btns.MouseEnter += new EventHandler(btns_MouseEnter);
btns.MouseLeave += new EventHandler(btns_MouseLeave);
panel1.MouseEnter += new System.EventHandler(btns_MouseEnter);
panel1.MouseLeave += new System.EventHandler(btns_MouseLeave);
it will work.
You can try some Form-wide Mouse message solution like this:
//Suppose your disabled Button is button1
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
button1.Enabled = false;
button1.BackColor = Color.Green;
//Try this to see it in action
button1.MouseEnter += (s, e) => {
button1.BackColor = Color.Red;
};
button1.MouseLeave += (s, e) => {
button1.BackColor = Color.Green;
};
Application.AddMessageFilter(this);//Add the IMessageFilter to the current Application
}
bool entered;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x200) //WM_MOUSEMOVE = 0x200
{
if (Control.FromHandle(m.HWnd) == button1.Parent &&
button1.ClientRectangle.Contains(button1.PointToClient(MousePosition)))
{
if (!entered) {
entered = true;
//Raise the MouseEnter event via Reflection
typeof(Button).GetMethod("OnMouseEnter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Invoke(button1, new[] { EventArgs.Empty });
}
}
else if (entered) {
//Raise the MouseLeave event via Reflection
typeof(Button).GetMethod("OnMouseLeave", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Invoke(button1, new []{EventArgs.Empty});
entered = false;
}
}
return false;
}
}
Why can't you try this?
By adding MouseMove event on the form...
Alternatively, if you want an easy way of maintaining the event handling, you could just never actually disable the button. Add some sort of wrapper class around a button that changes the the implementation of your buttons.
The new disable property could just change some CSS on the button and change a property that would make the click handler not do anything (or other relevant events).
I tried many but ended up using this simple trick which I think it is more effective.
Create a subclass(CustomControl with just base control in it) which extends UserControl
then instead of setting "Enabled" property to false create a Method which disables just basecontrol in it instead of whole CustomControl.
Set the tool tip on CustomControl still will be able to fire eventhandlers setting the basecontrol disabled. This works wherever CustomControl is in use rather than coding on every form you use with.
Here is the hint.. :)
public partial class MyTextBox : UserControl
{
...
...
...
public void DisableMyTextBox()
{
this.txt.Enabled = false; //txt is the name of Winform-Textbox from my designer
this.Enabled = true;
}
public void EnableMyTextBox()
{
this.txt.Enabled = true;
this.Enabled = true;
}
//set the tooltip from properties tab in designer or wherever
}

Detecting when my form has focus

I'm working in C# with WinForms in a large application with multiple forms.
At several points I have another form coming up as a progress screen. Because I can't freeze my UI thread, I have to start the new form in a new thread. I'm using progressform.ShowDialog() to start the form, but because it's in a new thread, it is possible to Click or Alt + Tab back to the main form. I want to disable this.
My thought is that I can put an EventHandler on the mainForm.GotFocus event and redirect focus to progressForm if it is shown. However, the GotFocus event isn't being triggered when you switch applications or move between the progressForm and mainForm. I'm guessing that it's because some element in mainForm has focus, not the form itself.
If anyone knows of a better way to do this (I'm not committed to the EventHandler approach) or working code for the EventHandler approach, it would solve my problem.
Edit
As per the comment, I tried using the Activated event.
// in InitializeForm()
this.Activated += FocusHandler;
// outside of InitializeForm()
void FocusHandler(object sender, EventArgs e)
{
if (ProgressForm != null)
{
ProgressForm.Focus();
}
}
But it still allowed me to click back to the main form and click buttons.
I've tried some ways and found this which does work as you want, the whole idea is to filter some message from your main UI when your progress form is shown:
public partial class Form1 : Form
{
[DllImport("user32")]
private static extern int SetForegroundWindow(IntPtr hwnd);
public Form1()
{
InitializeComponent();
}
ChildUI child = new ChildUI();
bool progressShown;
IntPtr childHandle;
//I suppose clicking on the button1 on the main ui form will show a progress form.
private void button1_Click(object sender, EventArgs e)
{
if(!progressShown)
new Thread(() => { progressShown = true; childHandle = child.Handle; child.ShowDialog(); progressShown = false; }).Start();
}
protected override void WndProc(ref Message m)
{
if (progressShown&&(m.Msg == 0x84||m.Msg == 0xA1||m.Msg == 0xA4||m.Msg == 0xA3||m.Msg == 0x6))
//0x84: WM_NCHITTEST
//0xA1: WM_NCLBUTTONDOWN
//0xA4: WM_NCRBUTTONDOWN
//0xA3 WM_NCLBUTTONDBLCLK //suppress maximizing ...
//0x6: WM_ACTIVATE //suppress focusing by tab...
{
SetForegroundWindow(childHandle);//Bring your progress form to the front
return;//filter out the messages
}
base.WndProc(ref m);
}
}
if you want to show your progress form normally (not a Dialog), using Application.Run(), showing form normally (using Show()) without processing some while loop will terminate the form almost right after showing it:
private void button1_Click(object sender, EventArgs e)
{
//progressShown = true;
//child.Show();
if (!progressShown)
{
new Thread(() => {
progressShown = true;
if (child == null) child = new ChildUI();
childHandle = child.Handle;
Application.Run(child);
child = null;
progressShown = false;
}).Start();
}
}
I've tested and it works like a charm.

C# capture main form keyboard events

How to catch keyboard events of the WinForm main form, where other controls are.
So I want to catch one event Ctrl + S and doesn't matter where focus is.
But without Pinvoke (hooks and such ...)
Only .NET managed internal power.
Try this code. Use the interface IMessageFilter you can filter any ctrl+key.
public partial class Form1 :
Form,
IMessageFilter
{
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(this);
this.FormClosed += new FormClosedEventHandler(this.Form1_FormClosed);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m)
{
//here you can specify which key you need to filter
if (m.Msg == 0x0100 && (Keys)m.WParam.ToInt32() == Keys.S &&
ModifierKeys == Keys.Control)
{
return true;
}
else
{
return false;
}
}
}
I tested this and worked for me.
the Form Class (System.Windows.Forms) has OnKeyDown, OnKeyPress, and OnKeyUp event methods that you can use to detect Ctrl + S
use the KeyEventArgs in those methods to determine which keys were pressed
EDIT
be sure to enable Form.KeyPreview = true; so the form will capture the events regardless of focus.
Handle the KeyDown on the form and all its controls.
private void OnFormLoad(object sender, EventArgs e)
{
this.KeyDown += OnKeyDown;
foreach (Control control in this.Controls)
{
control.KeyDown += OnKeyDown;
}
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyValue == (int)Keys.S)
{
Console.WriteLine("ctrl + s");
}
}
}
You may add a MenuStrip and then create a menu strip item named save and give it a short cut Ctrl + S. Add a event handler for that. This will fire even if the focus is on other control on the form. If you don't like to see the MenuStrip; you can set visible = false too. I must admit this is ugly.

Categories

Resources