WPF is used to implement text prompt on screen

  • 2020-06-15 08:01:34
  • OfStack

1. Create 1 WPF Application.
2. Set the WindowStyle property of Window to "None" to remove the title bar, set the AllowsTransparency property to "True" to allow transparency, the Topmost property to "True" to make the prompt text appear on the top floor, and the Background property to "#00000000" to make the window transparent.
3. Set the Background property of Grid. For black text, use a translucent white background. The effect of transparency, if produced by the Opacity attribute, causes other elements in the container to also have a transparent effect. The easier way is to set the transparency by the first two digits of the Background attribute.
4. Add processing methods to MouseLeave, MouseDown, MouseEnter events of Window. The MouseEnter and MouseLeave events were added to increase the background transparency when the mouse cursor moved over the indicated area and decrease the background transparency when the mouse cursor moved over the indicated area. The MouseDown event handler causes the prompt to be cancelled by clicking on the prompt area.

 code  
/// <summary>
///  Mouse into the processing method 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseEnter(object sender, MouseEventArgs e)
{
    Brush brush = new SolidColorBrush(Color.FromArgb(0x78,0xFF,0xFF,0xFF));  //  create 1 A solid color brush
    grid.Background = brush;  //  Applied to the grid
}
/// <summary>
///  Mouse out method 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseLeave(object sender, MouseEventArgs e)
{
    Brush brush = new SolidColorBrush(Color.FromArgb(0x52, 0xFF, 0xFF, 0xFF));  //  create 1 A solid color brush
    grid.Background = brush;  //  Applied to the grid
}
/// <summary>
///  Mouse click processing method 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
    mainWindow.Close();  //  Or use mainWindow.Hide() Hide the window 
}

This article is applicable to.NET Framework 4

Related articles: