WPF透明框设置Demo

发布时间 2023-12-14 14:10:40作者: JohnYang819
<Window x:Class="GuiDB.EBMultiEditTextWin"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:GuiDB"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800" Background="Transparent" 
       Opacity="1"
        WindowStyle="None"
        Topmost="True"
        AllowsTransparency="True" ShowInTaskbar="False"
        KeyDown="Window_KeyDown" 
        Loaded="Window_Loaded"
        >
    <Grid>
        <TextBox x:Name="editTextBox" Visibility="Hidden" Background="White"  Foreground="Black" 
                 BorderBrush="White" CaretBrush="White"
                 TextWrapping="Wrap" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
                  
                 PreviewLostKeyboardFocus="editTextBox_PreviewLostKeyboardFocus"
                 />
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GuiDB
{
    /// <summary>
    /// EBMultiEditTextWin.xaml 的交互逻辑
    /// </summary>
    public partial class EBMultiEditTextWin : Window
    {
        EBMultiTableControl _tableControl;
        EBColumn _columnHeader;

        public EBMultiEditTextWin(EBMultiTableControl tableControl,EBColumn header,double textW,double textH,double top,double left)
        {
            InitializeComponent();

            _tableControl = tableControl;
            _columnHeader = header;
            this.Width = textW;
            this.Height = textH;
            editTextBox.Width = textW;
            editTextBox.Height = textH;
            this.Top = top;
            this.Left = left;

            
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key.ToString() == "Return")
            {
                _columnHeader.Content = editTextBox.Text;
                _tableControl.ReDraw();
                Close();
            }
            else if (e.Key.ToString() == "Escape")
            {
                _tableControl.ReDraw();
                Close();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            editTextBox.Visibility = Visibility.Visible;
            editTextBox.FontSize = 10;
            editTextBox.Text = _columnHeader.Content;

            editTextBox.SelectAll();
            editTextBox.Focus();
        }

        private void editTextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            _columnHeader.IfBeSel = false;
            _tableControl.DownPt = null;
            _tableControl.ReDraw();
            
            Task.Run(() =>
            {
                Dispatcher.Invoke(() => Close());
            });
        }
    }
}