I am trying to create a method that will draw a window with some kind of notification. A notification is a form without a border on which a Label is placed with the required text. The notification should be displayed for some time, without capturing the focus.

In the end, I managed the window as needed, but in the end, instead of the control, the background behind the window is drawn, i.e. the window looks like a hole:

Notification

In the screenshot, the red frame is not a frame, but a form. On the "empty" place should be control.

The project itself, strictly speaking, is not WinForms, but a class library (4.6) with a reference to System.Wndows.Forms .

Her code:

 using System; using System.Collections.Generic; using System.Drawing; using System.Threading.Tasks; using System.Windows.Forms; namespace someNamespace { public class Overlay { class Notification : Form { protected override bool ShowWithoutActivation => true; private const int WS_EX_TOPMOST = 0x00000008; protected override CreateParams CreateParams { get { CreateParams createParams = base.CreateParams; createParams.ExStyle |= (WS_EX_TOPMOST); return createParams; } } public Notification(string Message): base() { BackColor = Color.OrangeRed; FormBorderStyle = FormBorderStyle.None; Bounds = new Rectangle(10, 10, 320, 240); var label = new Label(); label.Text = Message; label.Location = new Point(10, 10); label.Size = new Size(300, 220); Controls.Add(label); Enabled = false; } public void Show(int Delay) { Deactivate += ShowAgain(); Show(); Task.Delay(Delay).Wait(); Deactivate -= ShowAgain(); Close(); } EventHandler ShowAgain() { return (s, e) => Show(); } } static Dictionary<string, Notification> notifications = new Dictionary<string, Notification>(); public void Notify(string Message, int Delay) { Application.EnableVisualStyles(); if (!notifications.ContainsKey(Message)) notifications.Add(Message, new Notification(Message)); var n = notifications[Message]; n.Show(Delay); } } 

}

Console application that pulls the library:

 class Program { static Overlay overlay = new Overlay(); static void Main(string[] args) { overlay.Notify("Привет!", 5000); } } 

How to return control to the place?

  • four
    And where is the call to Application.Run? No Application.Run - no rendering, it's simple! - Pavel Mayorov
  • @PavelMayorov Baliyin !! 1 Thank you :) - eastwing

0