程序化构建的窗口始终位于顶部

时间:2014-12-15 23:52:18

标签: c# wpf

在我的源代码中,我启动了一个新的工具窗口并纯粹在C#中生成它。这是我的窗口构造和事件处理程序代码。

    public void InitToolsWindow()
    {
        if (!ToolsOpen)
        {
            Tools.Height = 300;
            Tools.Width = 229.819;
            Tools.Left = this.Left + this.ActualWidth - Tools.Width - 10;
            Tools.Top = this.Top + this.ActualHeight - Tools.Height - 10;
            tposx = Tools.Left;
            tposy = Tools.Top;
            Tools.Background = Brushes.White;
            Tools.ResizeMode = System.Windows.ResizeMode.CanResize;
            Tools.WindowStyle = System.Windows.WindowStyle.ToolWindow;
            Tools.WindowState = WindowState.Normal;

            Tools.ShowInTaskbar = false;
            Tools.Show();
            Tools.Topmost = true;

            Grid ToolsGrid = new Grid();
            Tools.BorderThickness = new Thickness(0, 0, 0, 0);
            Tools.BorderBrush = Brushes.Black;
            ToolsGrid.Width = Tools.Width;
            ToolsGrid.Height = Tools.Height;

            var brush = new ImageBrush();
            brush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/List Icon.png"));
            Button ListIcon = new Button();
            ListIcon.Width = 25;
            ListIcon.Height = 23;
            ListIcon.Background = brush;

            var brush2 = new ImageBrush();
            brush2.ImageSource = new BitmapImage(new Uri("pack://application:,,,/images/Grid Icon.png"));
            Button GridIcon = new Button();
            GridIcon.Width = 25;
            GridIcon.Height = 23;
            GridIcon.Background = brush2;

            Canvas UpperImages = new Canvas();
            UpperImages.Children.Add(ListIcon);
            UpperImages.Children.Add(GridIcon);
            Canvas.SetLeft(ListIcon, 1);
            Canvas.SetTop(ListIcon, 1);
            Canvas.SetLeft(GridIcon, 28);
            Canvas.SetTop(GridIcon, 1);

            ToolsGrid.Children.Add(UpperImages);

            Line topline = new Line();
            topline.X1 = 0;
            topline.Y1 = 25;
            topline.X2 = Tools.Width;
            topline.Y2 = 25;
            topline.Stroke = Brushes.Black;//Brushes.White;
            topline.StrokeThickness = 0.75;
            ToolsGrid.Children.Add(topline);

            //Tools Container
            //================================================================
            ScrollViewer sv2 = new ScrollViewer();
            sv2.Height = ToolsGrid.Height - 70;
            sv2.Width = Tools.Width - 30;
            sv2.Margin = new Thickness(5, 27, 0, 0);
            sv2.BorderThickness = new Thickness(0);
            sv2.BorderBrush = Brushes.Transparent;
            sv2.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            sv2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            sv2.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
            sv2.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            StackPanel sp2 = new StackPanel();
            sp2.Width = sv2.Width;
            //================================================================


            //Base Tools
            //================================================================
            // - Square
            Image scImg = new Image();
            scImg.Source = new BitmapImage(new Uri("pack://application:,,,/images/Square.png"));
            Image scImg2 = new Image();
            scImg2.Source = new BitmapImage(new Uri("pack://application:,,,/images/Square - Selected.png"));
            TextBlock sdesc = new TextBlock();
            sdesc.FontSize = 12;
            sdesc.FontFamily = new FontFamily("Calibri");
            sdesc.Text = "R e c t a n g l e  O b j e c t";
            Canvas SquareContent = new Canvas();
            SquareContent.Children.Add(scImg);
            SquareContent.Children.Add(sdesc);
            Canvas.SetLeft(sdesc, 40);
            Canvas.SetTop(sdesc, 5);
            Canvas.SetLeft(scImg, 1);
            Canvas.SetTop(scImg, 1);
            Button Square = new Button();
            Square.Width = Tools.Width - 40;
            Square.Height = 30;
            Square.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            Square.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            Square.Content = SquareContent;
            Square.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
            Square.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
            Square.Click += delegate(object sender, RoutedEventArgs e) { 
                SquareContent.Children.Remove(scImg); 
                SquareContent.Children.Add(scImg2); 
                Square.Style = this.Resources["myStyle"] as Style;
            };


            // - Ellipses
            Image ecImg = new Image();
            ecImg.Source = new BitmapImage(new Uri("pack://application:,,,/images/Ellipse.png"));
            TextBlock edesc = new TextBlock();
            edesc.FontSize = 12;
            edesc.FontFamily = new FontFamily("Calibri");
            edesc.Text = "E l l i p s e s  O b j e c t";
            Canvas EllipContent = new Canvas();
            EllipContent.Children.Add(ecImg);
            EllipContent.Children.Add(edesc);
            Canvas.SetLeft(edesc, 40);
            Canvas.SetTop(edesc, 5);
            Canvas.SetLeft(ecImg, 1);
            Canvas.SetTop(ecImg, 1);
            Button Ellip = new Button();
            Ellip.Width = Tools.Width - 40;
            Ellip.Height = 30;
            Ellip.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            Ellip.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            Ellip.Margin = new Thickness(0, 3, 0, 0);
            Ellip.Content = EllipContent;
            Ellip.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
            Ellip.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
            Ellip.Click += delegate(object sender, RoutedEventArgs e) {  };

            // - Line
            Image lcImg = new Image();
            lcImg.Source = new BitmapImage(new Uri("pack://application:,,,/images/Line.png"));
            TextBlock ldesc = new TextBlock();
            ldesc.FontSize = 12;
            ldesc.FontFamily = new FontFamily("Calibri");
            ldesc.Text = "L i n e  O b j e c t";
            Canvas LineContent = new Canvas();
            LineContent.Children.Add(lcImg);
            LineContent.Children.Add(ldesc);
            Canvas.SetLeft(ldesc, 40);
            Canvas.SetTop(ldesc, 5);
            Canvas.SetLeft(lcImg, 1);
            Canvas.SetTop(lcImg, 1);
            Button Lin = new Button();
            Lin.Width = Tools.Width - 40;
            Lin.Height = 30;
            Lin.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            Lin.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            Lin.Margin = new Thickness(0, 3, 0, 0);
            Lin.Content = LineContent;
            Lin.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
            Lin.VerticalContentAlignment = System.Windows.VerticalAlignment.Top;
            Lin.Click += delegate(object sender, RoutedEventArgs e) { };
            //================================================================


            sp2.Children.Add(Square);
            sp2.Children.Add(Ellip);
            sp2.Children.Add(Lin);


            Button[] ToolSlots = new Button[24];
            for (int i = 0; i < ToolSlots.Length; i++)
            {
                ToolSlots[i] = new Button();
                ToolSlots[i].Width = Tools.Width - 40;
                ToolSlots[i].Height = 30;
                ToolSlots[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                ToolSlots[i].VerticalAlignment = System.Windows.VerticalAlignment.Top;
                ToolSlots[i].Margin = new Thickness(0, 3, 0, 0);
                sp2.Children.Add(ToolSlots[i]);
            }
            sp2.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            sv2.Content = sp2;
            TextBlock tx1 = new TextBlock();
            tx1.Text = "text";
            tx1.Foreground = Brushes.Black;
            tx1.Background = Brushes.White;

            Tools.Title = "T o o l s";

            ToolsGrid.Children.Add(sv2);
            Image ToolsIcon = new Image();
            ToolsIcon.Width = 13;
            ToolsIcon.Height = 13;
            ToolsIcon.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            ToolsIcon.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            ToolsIcon.Margin = new Thickness(3, 3, 0, 0);
            ToolsGrid.Children.Add(ToolsIcon);


            TextBlock buttontxt = new TextBlock();
            buttontxt.Text = "x";
            buttontxt.FontWeight = FontWeights.Bold;
            buttontxt.Foreground = Brushes.Black;//Brushes.White;
            Button exit = new Button();
            exit.Width = 22;
            exit.Height = 18;
            exit.BorderThickness = new Thickness(0);
            exit.BorderBrush = Brushes.Transparent;
            exit.Background = Brushes.Transparent;
            exit.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
            exit.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            exit.Margin = new Thickness(0, -1, 5, 0);
            exit.Content = buttontxt;
            exit.Click += delegate(object sender, RoutedEventArgs args)
            {
                var thisAnim5 = new DoubleAnimation();
                thisAnim5.From = 0.8;
                thisAnim5.To = 0.0;
                thisAnim5.Duration = new Duration(TimeSpan.FromSeconds(0.5));
                Tools.Topmost = true;
                Tools.BeginAnimation(Window.OpacityProperty, thisAnim5);
                ToolsHidden = true;

            };

            Tools.MouseUp += delegate(object sender, MouseButtonEventArgs e)
            {
                double lft = Tools.Left;
                double tp = Tools.Top;
                if ((lft - tposx > -25) && (lft - tposx < 25) && (tp - tposy > -25) && (tp - tposy < 25))
                {
                    Tools.Left = tposx;
                    Tools.Top = tposy;
                }
            };
            Tools.MouseWheel += delegate(object sender, MouseWheelEventArgs e)
            {
                if (Math.Round(Tools.Opacity, 1) == 0.1) 
                {
                    var thisAnim5 = new DoubleAnimation();
                    thisAnim5.From = Tools.Opacity;
                    thisAnim5.To = 0.0;
                    thisAnim5.Duration = new Duration(TimeSpan.FromSeconds(0.01));
                    Tools.Topmost = true;
                    Tools.BeginAnimation(Window.OpacityProperty, thisAnim5);
                    ToolsHidden = true;
                    //ToolsElem.Background = Brushes.Transparent;
                    //ToolsElem.Foreground = Brushes.Black; 
                }
                else 
                { 
                    double val;
                    if (e.Delta < 0) val = -0.1;
                    else val = 0.1;
                    double mth = Tools.Opacity + val;

                    var thisAnim5 = new DoubleAnimation();
                    thisAnim5.From = Tools.Opacity;
                    thisAnim5.To = mth;
                    thisAnim5.Duration = new Duration(TimeSpan.FromSeconds(0.01));
                    Tools.Topmost = true;
                    Tools.BeginAnimation(Window.OpacityProperty, thisAnim5);
                }
            };
            Tools.Closing += delegate(object sender, System.ComponentModel.CancelEventArgs e) 
            {
                e.Cancel = true;
                Tools.Visibility = System.Windows.Visibility.Hidden;
                ToolsHidden = true;
                ToolsObj.IsChecked = false;
            };
            Tools.KeyDown += delegate(object sender, KeyEventArgs e)
            {
                if (e.Key == Key.PageUp)
                {
                    double val = 0.1;
                    double mth = Tools.Opacity + val;

                    var thisAnim5 = new DoubleAnimation();
                    thisAnim5.From = Tools.Opacity;
                    thisAnim5.To = mth;
                    thisAnim5.Duration = new Duration(TimeSpan.FromSeconds(0.01));
                    Tools.Topmost = true;
                    Tools.BeginAnimation(Window.OpacityProperty, thisAnim5);
                }
                else if (e.Key == Key.PageDown)
                {
                    if (Math.Round(Tools.Opacity, 1) == 0.1)
                    {
                        var thisAnim5 = new DoubleAnimation();
                        thisAnim5.From = Tools.Opacity;
                        thisAnim5.To = 0.0;
                        thisAnim5.Duration = new Duration(TimeSpan.FromSeconds(0.01));
                        Tools.Topmost = true;
                        Tools.BeginAnimation(Window.OpacityProperty, thisAnim5);
                        ToolsHidden = true;
                        //ToolsElem.Background = Brushes.Transparent;
                        //ToolsElem.Foreground = Brushes.Black;
                    }
                    else
                    {
                        double val = 0.1;
                        double mth = Tools.Opacity - val;

                        var thisAnim5 = new DoubleAnimation();
                        thisAnim5.From = Tools.Opacity;
                        thisAnim5.To = mth;
                        thisAnim5.Duration = new Duration(TimeSpan.FromSeconds(0.01));
                        Tools.Topmost = true;
                        Tools.BeginAnimation(Window.OpacityProperty, thisAnim5);
                    }
                }

            };

            Tools.SizeChanged += delegate(object sender, SizeChangedEventArgs e)
            {
                ToolsGrid.Height = e.NewSize.Height;
                ToolsGrid.Width = e.NewSize.Width;
                sv2.Height = e.NewSize.Height - 70;
                sv2.Width = e.NewSize.Width - 30;
                topline.X2 = e.NewSize.Width;
            };

            ToolsGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            ToolsGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            Tools.Content = ToolsGrid;
            ToolsOpen = true;


        }
        else if (ToolsHidden)
        {
            Tools.Visibility = System.Windows.Visibility.Visible;
            ToolsHidden = false;
        }
        else
        {
            Tools.Visibility = System.Windows.Visibility.Hidden;
            ToolsHidden = true;
        }
    }

如果我放置&#34;工具&#34;窗口为&#34; TopMost&#34;。它适用于我的MainWindow,以及我打开的每个其他窗口。

Window Does Stay Topmost

Tools Container is Topmost all applications

另一方面,如果我不把它作为&#34; TopMost&#34;它隐藏在MainWindow的后面。

Tools Window hides behind Main Screen

但我需要的是工具窗口只能在MainWindow的顶部显示。我怎么能做到这一点?

1 个答案:

答案 0 :(得分:1)

我看不到您为工具窗口设置Owner的位置。

Tools.Owner设置为父表单,应该允许您跳过TopMost的集合,以便它不会出现在非应用程序窗口上,也允许它保留在父表单上。

MSDN Window.Owner