How to make a popup (tooltip, popup, popup) in Winforms?

How can I do what I call a popup in WinForms?


Since I used my own word “popup”, let me give examples of this so-called “popup” window:

  • a popup (may go beyond its parent form, does not appear on the taskbar, is not modal, and does not steal focus):

    enter image description here

  • A pop-up menu window (may go beyond its parent form, does not appear on the taskbar, is not modal, and does not steal focus):

    enter image description here

  • a drop-down box (may go beyond its parent form, does not appear on the taskbar, is not modal, and does not steal focus):

    enter image description here

  • A main menu (can extend beyond its parent form, does not appear on the taskbar, is not modal, and does not steal focus):

    enter image description here

  • Updating The pop-up window does not make itself an “active” window a> when interacting using the mouse or keyboard (the “owner” window remains the active window):

enter image description here

The attributes I'm looking for in this mythical pop-up are as follows:

  • can expand beyond its parent form (i.e. not a child window )
  • not displayed in the taskbar (i.e. the heuristic of the window from which the windows should appear does not work, and also does not have the extended window style WS_EX_APPWINDOW)
  • non- modal (ie does not disable its "owner" )
  • does not steal focus
  • always on top of this owner
  • does not become an "active" window during interaction (the owner remains active)

Windows applications already create such windows. How can I do this in a WinForms application?

Related Questions

  • How can I achieve everything higher in my own code?
  • How to create a popup in Delphi?
  • I have this own code to show a pop-up window - what P / Invokes are needed to perform the same actions in .NET?
  • I have a P / Invoke suite in .NET. Is it possible to reuse regular WinForm by overriding certain methods to achieve the same effect?
  • I have a WinForm that I show as a "popup", overriding certain methods - is there a built-in Control that can act as a popup for me?
  • How to simulate a popup in WinForms?

Attempt # 1

I tried the Show(onwer) + ShowWithoutActivation :

 PopupForm dd = new PopupForm (); dd.Show(this); 

with PopupForm:

 public class PopupForm: Form { public PopupForm() { InitilizeComponent(); } private void InitilizeComponent() { this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.WindowState = FormWindowState.Normal; this.ShowInTaskbar = false; } protected override bool ShowWithoutActivation { get { return true; } } } 

The problem was almost completely resolved, but then I discovered it was reminded of another property of the "pop-up" windows: they do not take focus from their form, the "owner" becomes active when interacting with the mouse or keyboard.

+6
source share
3 answers

You want to own a window. In your main form:

 private void showPopup_Click(object sender, EventArgs e) { PopupForm popupForm = new PopupForm(); // Make "this" the owner of form2 popupForm.Show(this); } 

PopupForm should look like this:

 public partial class PopupForm : Form { private bool _activating = false; public PopupForm() { InitializeComponent(); } // Ensure the popup isn't activated when it is first shown protected override bool ShowWithoutActivation { get { return true; } } private const int WM_NCACTIVATE = 0x86; [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); protected override void WndProc(ref Message m) { // The popup needs to be activated for the user to interact with it, // but we want to keep the owner window appearance the same. if ((m.Msg == WM_NCACTIVATE) && !_activating && (m.WParam != IntPtr.Zero)) { // The popup is being activated, ensure parent keeps activated appearance _activating = true; SendMessage(this.Owner.Handle, WM_NCACTIVATE, (IntPtr) 1, IntPtr.Zero); _activating = false; // Call base.WndProc here if you want the appearance of the popup to change } else { base.WndProc(ref m); } } } 

And make sure PopupForm.ShowInTaskbar = false .

+3
source

I was curious how combobox dropdowns and menus work, so I did some more research.

There are two main approaches.

Create a popup as an overlapping window belonging to the main window

This method is necessary if the popup has built-in controls or if you want the popup to appear as a modeless dialog box.

If the user interacts with child controls in a pop-up window, he must receive activation. (Thus, various activation blocking methods, such as WM_MOUSEACTIVATE processing, are red herrings.) And when it receives activation, Windows deactivates the main window. The fix for this is to send the WM_NCACTIVATE message to the parent to update its appearance without changing its activation status. This is the approach used by the .NET ToolStrip, and my other answer illustrates it with code.

Create a popup as a child of the desktop window

This method is used by combobox and (I think) drop-downs, but you cannot implement child controls so that it is not widely applicable.

The popup is a child window, so it does not interfere with activation. He is always on top. Mouse capture is used to detect clicks outside the pop-up window and dismiss it.

However, this is not so easy to implement. Activation remains with the main application, so it retains focus and receives keystrokes. It seems like this excludes the built-in controls in the popup because they cannot receive focus. Combobox handles this by forwarding keystroke messages to a drop-down list. (Note that menus, drop-down lists, drop-down lists, etc. are all completely owner-drawn in the sense that they don't have built-in windows.)

+2
source

Create a pop-up window as a child window of the desktop, and then show it without activating it.

 hWnd = CreateWindowEx(..., WS_CHILDWINDOW | WS_VISIBLE | WS_BORDER | WS_CLIPSIBLINGS, ..., GetDesktopWindow(), ...); SetWindowPos(hWnd, HWND_TOPMOST, ..., SWP_NOACTIVATE); 

After that, your original window will be activated, even if you click on the pop-up window. The pop-up window may have its own child controls. You can click the button on it. But if it is an edit control, you cannot edit it, I do not know why. Perhaps because the cursor in the original window is already blinking.

0
source

Source: https://habr.com/ru/post/904673/


All Articles