Activate pressing a form and process button at the same time?

I am using Windows Forms in C #.

I have a main form with several toolbars that contain toolStripButtons. After working with another form containing data, the main form loses focus and clicking on toolStripButton does not raise an OnClick event: the first click activates the main form, and only the second click actually presses the button. I need the user to click the button only once to trigger the Click event, any ideas on how to do this? Thanks.

Notes:

  • I used MDI and there was no problem clicking on the buttons of the parent form. But now it’s important to have shapes floating freely on multiple displays.
  • Work forms have the main form as the property of the owner, so they remain on top of the main form.
  • When I click the inactive form button, none of MouseHover, MouseEnter, MouseDown, and MouseUp work. This is just the main form of an Activate event that fires.
  • There is a TreeView tree (inside tabControl, inside splitContainer, inside the panel), in the main form. Its elements are selected at the first click of the mouse, even if the main form is inactive. I think that not all controls are equal!
+7
source share
2 answers

What you need to do is create a class that inherits ToolStrip and processes WndProc . This is one way to do this. There are others.

 private class MyToolStrip : ToolStrip { private const uint WM_LBUTTONDOWN = 0x201; private const uint WM_LBUTTONUP = 0x202; private static bool down = false; protected override void WndProc(ref Message m) { if (m.Msg == WM_LBUTTONUP && !down) { m.Msg = (int)WM_LBUTTONDOWN; base.WndProc(ref m); m.Msg = (int)WM_LBUTTONUP; } if (m.Msg == WM_LBUTTONDOWN) down = true; if (m.Msg == WM_LBUTTONUP) down = false; base.WndProc(ref m); } } 

I also saw this solution:

 protected override void WndProc(ref Message m) { // WM_MOUSEACTIVATE = 0x21 if (m.Msg == WM_MOUSEACTIVATE && this.CanFocus && !this.Focused) this.Focus(); base.WndProc(ref m); } 

I came across this at the last place of work, I think that the solution that I came up with was more like the last, but I do not have access to the exact code that I used.

+6
source

if u has a shape without borders, so this logic works for you :)

 form.FormBorderStyle = FormBorderStyle.None 
-4
source

All Articles