none
WPF 窗口尺寸改变,菜单怎样随窗口一起变化 RRS feed

  • 问题

  • 1.编写WPF窗口图示如下:

    窗口前台代码(MainWindow.xaml):

    <Window x:Class="Shanzhizi.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:windowMenucontrol="clr-namespace:Shanzhizi.Control.MenuControl"
            Title="私人空间"    Width="1360" Height="745" 
            WindowStartupLocation="CenterScreen"  WindowStyle="None"
            PreviewMouseMove="Window_PreviewMouseMove"
            MouseLeftButtonDown="WindowMove_MouseLeftButtonDown"
            ResizeMode="CanResizeWithGrip" WindowState="Normal" Topmost="True" >

        <Grid >
            <windowMenucontrol:windowMenuControl x:Name="WMC"
                LostFocus="windowMenuControl_LostFocus"
                GotFocus="windowMenuControl_GotFocus"
                ContextMenuOpening="windowMenuControl_ContextMenuOpening"
                Panel.ZIndex="1"/>
            <Grid Name="mainwindowgrid"></Grid>
        </Grid>
    </Window>

    窗口后台代码(MainWindow.xaml.cs):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;

    using Shanzhizi.Control.MenuControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            #region 窗口菜单自动显示/隐藏
            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                /*通过鼠标焦点获自定义用户控件自动显示与隐藏 假设标题栏在120和620之间*/
                if (e.GetPosition(this).Y >= 92.5 && e.GetPosition(this).Y <= 667.5)
                {
                    if (e.GetPosition(this).X>=1325)
                    {
                        if (WMC.Visibility != Visibility.Visible)
                        {
                            WMC.Visibility = Visibility.Visible;
                        }
                    }
                    else
                    {
                        WMC.Visibility = Visibility.Collapsed;
                    }
                }
                else
                { WMC.Visibility = Visibility.Collapsed;}       
            }
            //获得焦点
            private void windowMenuControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }
            //失去焦点
            private void windowMenuControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;         
            }
            //窗体事件焦点
            private void windowMenuControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            #endregion  

        }
    }

    2.代码功能说明

    a.我编写了一个WPF窗口,该窗口不含任何内容,作为WPF应用程序的载体(入口),简单设置了WPF窗口参数,窗口图例及参数见以上内容,主窗口宽为1360,高为745。

    d.上图WPF窗口靠右侧所见的菜单为引入的控件(windowMenuControl控件),具有菜单功能的作用,是主窗口(MainWindow.xaml)与后续程序的链接按钮,通过单击该控件(windowMenuControl控件)相应的按钮,将程序加载到主窗口(MainWindow.xaml)中;

    c.该控件(windowMenuControl控件)通过  xmlns:windowMenucontrol="clr-namespace:Shanzhizi.Control.MenuControl"及后台代码加入到主窗口(MainWindow.xaml)中。

     d. 该控件(windowMenuControl控件)宽为35,高位500,并靠主窗口(MainWindow.xaml)右边居中布置。该控件在主窗口中通Visibility.Collapsed、 Visibility.Visible 两个属性实现“隐藏”、“显示”,当鼠标处在(e.GetPosition(this).Y >= 120 && e.GetPosition(this).Y <= 620)
     、(e.GetPosition(this).X >= 1325)区域范围内,该控件处于“显示”状态,反之处于”隐藏“状态;

    3.问题

    启动窗口时,当鼠标处在(e.GetPosition(this).Y >= 120 && e.GetPosition(this).Y <= 620)、(e.GetPosition(this).X >= 1325)区域范围内,该控件处于“显示”状态,反之处于”隐藏“状态;应用过程中,当主窗口尺寸发生变化时(因为主窗口设置了 ResizeMode="CanResizeWithGrip" WindowState="Normal" Topmost="True" 等属性——可改变窗口的大小),鼠标无论处于窗口的任何部位,控件(windowMenuControl控件)均不显示。

    4.提问

    当主窗口尺寸发生改变时,鼠标在窗口右边35范围内(控件宽为35,高位500)立即显示,既控件位置随窗口尺寸变化而变化,总靠主窗口右边居中布置,鼠标处于主窗右边35,右边居中上500的范围内均能显示(控件宽为35,高位500),我该如何编写后台程序(MainWindow.xaml.cs)。

    小弟在此感谢各位师兄弟姐妹的帮忙,请各位师兄弟姐妹给予详细的解答,谢谢。

    2012年9月3日 15:26

答案

  • 我使用Rectangle模拟了你的MenuControl,基本的思路是这样的:

            private const double RECT_WIDTH = 40;
            private const double RECT_HEIGHT = 400;
            private const double RECT_MARGIN_WIDTH = 10;
            private const double RECT_MARGIN_HEIGHT = 10;
    
            void Window_MouseMove(object sender, MouseEventArgs e) {
                MousePosition = e.GetPosition(this.container);
                double x = (this.container.ActualHeight - RECT_HEIGHT) / 2;
                if ((MousePosition.X > (this.container.ActualWidth - RECT_WIDTH - RECT_MARGIN_WIDTH)) 
                    && (MousePosition.Y > x - RECT_MARGIN_HEIGHT) 
                    && (MousePosition.Y < this.container.ActualHeight - x + RECT_MARGIN_HEIGHT)) {
                    ShowRect();
                } else {
                    HideRect();
                }
            }

    其中RECT_WIDTH, RECT_HEIGHT, RECT_MARGIN_WIDTH, RECT_MARGIN_HEIGHT分别为Rectangle的宽、高、宽边界、高边界,container为Window中的最外层容器Grid。

    宽和高的边界是可选的,可以为0。


    Wanpeng wanpeng.ones@gmail.com

    2012年9月4日 3:24
  • 非常感谢Wanpeng给予回答,在你所给的例子的引导下,我实现了我想要的功能,并优化了以前的代码。具体代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;

    using Shanzhizi.Control.MenuControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
               
            }
            #region 窗口菜单自动显示/隐藏

            private const double width = 35;
            private const double height = 625;

            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                /*通过鼠标焦点获自定义用户控件自动显示与隐藏 假设标题栏在120和620之间*/
                if ((e.GetPosition(this).Y >= ((this.ActualHeight - height) / 2)) &&
                    (e.GetPosition(this).Y <= ((this.ActualHeight - height) / 2) + this.ActualHeight) &&
                    (e.GetPosition(this).X >= (this.ActualWidth - width)))
                {
                    WMC.Visibility = Visibility.Visible;
                }else{
                    WMC.Visibility = Visibility.Collapsed;
                }       
            }
            private void windowMenuControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }
            //失去焦点
            private void windowMenuControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;         
            }
            //窗体事件焦点
            private void windowMenuControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            #endregion  

        }
    }

    但还有一个问题想请教一下:

    1、我所引入的控件(windowMenuControl.xaml)主要用作菜单控件,以后可能还要增加相应其他菜单按钮,既控件高度会相应的增加或缩小,

    我想让主窗口能自动识别引入控件(windowMenuControl.xaml)的高、宽,不想要写成固定模式(private const double width = 35;  private const double height = 625),我应该怎么改学该代码,让 private const double width = 35;  private const double height = 625为可变化;

    2、控件(windowMenuControl.xaml)使用“StackPanel”编写,相对固定,当主窗口高度变化时,控件(windowMenuControl.xaml)高度不会改变,部分菜单功能被窗口遮拦,操作非常不便,我想实现该控件高度随主窗口高度改变成一定比例缩放,该控件运如何改写,才能实现主窗口与控件的协调工作,既实现信息传递;

    3、如果以上的功能能实现,当主窗口和控件高度缩小时,控件部分功能按钮被隐藏,我想通过鼠标滚轮的滚动控制该菜单控件(windowMenuControl.xaml),让被覆盖的菜单按钮在鼠标滚轮的滚动下,能上下移动,呈现在主窗口的可见部位,我应该如何改写该控件代码(windowMenuControl.xaml)。控件代码如下:

    windowMenuControl.xaml前台代码:

    <UserControl x:Class="Shanzhizi.Control.MenuControl.windowMenuControl"
                 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"
                 mc:Ignorable="d" MaxHeight="700" MaxWidth="35"  HorizontalAlignment="Right" VerticalAlignment="Center" d:DesignHeight="565" d:DesignWidth="35" xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" >

        <UserControl.Resources>
            <ResourceDictionary Source="windowMenuControlDictionary.xaml" />
        </UserControl.Resources>
        <Grid Height="570">
            <StackPanel Style="{StaticResource StackPanelResource}">
                <StackPanel  Style="{StaticResource StackPanelLeftResource}" >
                    <Button  Name="home" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/home.png" />
                    </Button>
                    <Button  Name="favorite" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/stuff.png" />
                    </Button>
                    <Button  Name="album" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/TN_psd.JPG" />
                    </Button>
                    <Button  Name="study" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/study.png" />
                    </Button>
                    <Button  Name="diary" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/album.tif" />
                    </Button>
                    <Button  Name="office" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/RTF.File.png" />
                    </Button>
                    <Button Name="pdf" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/PDF.File.png" />
                    </Button>
                    <Button Name="iexplorer" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/IExplorer.png" />
                    </Button>
                    <Button Name="communication" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/mWorld_green.png" />
                    </Button>
                    <Button Name="music" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/Musica_min.png" />
                    </Button >
                    <Button Name="video" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/mWorld_cd_Yellow.png" />
                    </Button>
                    <Button Name="setting" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/applications.png" />
                    </Button>
                    <Button Name="crafting" Height="35"  Click="WindowMenu_Click" >
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/Crafting.jpg" />
                    </Button>
                </StackPanel>
                <StackPanel Style="{StaticResource StackPanelRightResource}" >
                    <Button  Name="MinWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/-.png" />
                    </Button>
                    <Button  Name="FullWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/+.png" />
                    </Button>
                    <Button  Name="CloseWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/delete.png" />
                    </Button>
                </StackPanel>
            </StackPanel>
        </Grid>
    </UserControl>

    windowMenuControl.xaml.cs后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;

    using Shanzhizi.Control.Blackboard;
    using Shanzhizi.Study.建筑工程造价;


    namespace Shanzhizi.Control.MenuControl
     
    {
        public partial class windowMenuControl : UserControl
        {
            public windowMenuControl()
            {
                InitializeComponent();
            }


            #region 主菜单操作事件
           
              // 菜单操作事件
            private void WindowMenu_Click(object sender, RoutedEventArgs e)
            {
                Button button = sender as Button;
                switch (button.Name)
                {
                    case "home":
                        break;

                    case "favorite":
                        break;

                    case "study":
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new 工程造价());
                        break;

                    case "album":
                       
                        break;

                    case "diary":
                        break;

                    case "office":
                        break;

                    case "pdf":
                        break;

                    case "iexplorer":               
                       break;

                   case "communication":
                        break;

                    case "music":
                        break;

                    case "video":
                        break;

                    case "setting":
                  break;

       case "crafting":
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new blackboardControl());
                        break;

                    case "MinWindow":
                        Application.Current.MainWindow.WindowState = WindowState.Minimized;
                        break;

                    case "FullWindow":
                        Application.Current.MainWindow.WindowState = WindowState.Maximized;
                        break;

                    case "CloseWindow":
                        Application.Current.MainWindow.Close();
                        break;

                    default:
                       
                        break;
                }
            }
            #endregion
        }
    }

    我要实现以上两个功能,我该如何改写现有代码,请各位师兄弟姐妹帮忙,小弟在此感谢各位博友的帮组,本人再次在此感谢Wanpeng给予的帮助。

    2012年9月4日 17:23

全部回复

  • 我使用Rectangle模拟了你的MenuControl,基本的思路是这样的:

            private const double RECT_WIDTH = 40;
            private const double RECT_HEIGHT = 400;
            private const double RECT_MARGIN_WIDTH = 10;
            private const double RECT_MARGIN_HEIGHT = 10;
    
            void Window_MouseMove(object sender, MouseEventArgs e) {
                MousePosition = e.GetPosition(this.container);
                double x = (this.container.ActualHeight - RECT_HEIGHT) / 2;
                if ((MousePosition.X > (this.container.ActualWidth - RECT_WIDTH - RECT_MARGIN_WIDTH)) 
                    && (MousePosition.Y > x - RECT_MARGIN_HEIGHT) 
                    && (MousePosition.Y < this.container.ActualHeight - x + RECT_MARGIN_HEIGHT)) {
                    ShowRect();
                } else {
                    HideRect();
                }
            }

    其中RECT_WIDTH, RECT_HEIGHT, RECT_MARGIN_WIDTH, RECT_MARGIN_HEIGHT分别为Rectangle的宽、高、宽边界、高边界,container为Window中的最外层容器Grid。

    宽和高的边界是可选的,可以为0。


    Wanpeng wanpeng.ones@gmail.com

    2012年9月4日 3:24
  • 非常感谢Wanpeng给予回答,在你所给的例子的引导下,我实现了我想要的功能,并优化了以前的代码。具体代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;

    using Shanzhizi.Control.MenuControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
               
            }
            #region 窗口菜单自动显示/隐藏

            private const double width = 35;
            private const double height = 625;

            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                /*通过鼠标焦点获自定义用户控件自动显示与隐藏 假设标题栏在120和620之间*/
                if ((e.GetPosition(this).Y >= ((this.ActualHeight - height) / 2)) &&
                    (e.GetPosition(this).Y <= ((this.ActualHeight - height) / 2) + this.ActualHeight) &&
                    (e.GetPosition(this).X >= (this.ActualWidth - width)))
                {
                    WMC.Visibility = Visibility.Visible;
                }else{
                    WMC.Visibility = Visibility.Collapsed;
                }       
            }
            private void windowMenuControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }
            //失去焦点
            private void windowMenuControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;         
            }
            //窗体事件焦点
            private void windowMenuControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            #endregion  

        }
    }

    但还有一个问题想请教一下:

    1、我所引入的控件(windowMenuControl.xaml)主要用作菜单控件,以后可能还要增加相应其他菜单按钮,既控件高度会相应的增加或缩小,

    我想让主窗口能自动识别引入控件(windowMenuControl.xaml)的高、宽,不想要写成固定模式(private const double width = 35;  private const double height = 625),我应该怎么改学该代码,让 private const double width = 35;  private const double height = 625为可变化;

    2、控件(windowMenuControl.xaml)使用“StackPanel”编写,相对固定,当主窗口高度变化时,控件(windowMenuControl.xaml)高度不会改变,部分菜单功能被窗口遮拦,操作非常不便,我想实现该控件高度随主窗口高度改变成一定比例缩放,该控件运如何改写,才能实现主窗口与控件的协调工作,既实现信息传递;

    3、如果以上的功能能实现,当主窗口和控件高度缩小时,控件部分功能按钮被隐藏,我想通过鼠标滚轮的滚动控制该菜单控件(windowMenuControl.xaml),让被覆盖的菜单按钮在鼠标滚轮的滚动下,能上下移动,呈现在主窗口的可见部位,我应该如何改写该控件代码(windowMenuControl.xaml)。控件代码如下:

    windowMenuControl.xaml前台代码:

    <UserControl x:Class="Shanzhizi.Control.MenuControl.windowMenuControl"
                 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"
                 mc:Ignorable="d" MaxHeight="700" MaxWidth="35"  HorizontalAlignment="Right" VerticalAlignment="Center" d:DesignHeight="565" d:DesignWidth="35" xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" >

        <UserControl.Resources>
            <ResourceDictionary Source="windowMenuControlDictionary.xaml" />
        </UserControl.Resources>
        <Grid Height="570">
            <StackPanel Style="{StaticResource StackPanelResource}">
                <StackPanel  Style="{StaticResource StackPanelLeftResource}" >
                    <Button  Name="home" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/home.png" />
                    </Button>
                    <Button  Name="favorite" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/stuff.png" />
                    </Button>
                    <Button  Name="album" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/TN_psd.JPG" />
                    </Button>
                    <Button  Name="study" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/study.png" />
                    </Button>
                    <Button  Name="diary" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/album.tif" />
                    </Button>
                    <Button  Name="office" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/RTF.File.png" />
                    </Button>
                    <Button Name="pdf" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/PDF.File.png" />
                    </Button>
                    <Button Name="iexplorer" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/IExplorer.png" />
                    </Button>
                    <Button Name="communication" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/mWorld_green.png" />
                    </Button>
                    <Button Name="music" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/Musica_min.png" />
                    </Button >
                    <Button Name="video" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/mWorld_cd_Yellow.png" />
                    </Button>
                    <Button Name="setting" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/applications.png" />
                    </Button>
                    <Button Name="crafting" Height="35"  Click="WindowMenu_Click" >
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/Crafting.jpg" />
                    </Button>
                </StackPanel>
                <StackPanel Style="{StaticResource StackPanelRightResource}" >
                    <Button  Name="MinWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/-.png" />
                    </Button>
                    <Button  Name="FullWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/+.png" />
                    </Button>
                    <Button  Name="CloseWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/delete.png" />
                    </Button>
                </StackPanel>
            </StackPanel>
        </Grid>
    </UserControl>

    windowMenuControl.xaml.cs后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;

    using Shanzhizi.Control.Blackboard;
    using Shanzhizi.Study.建筑工程造价;


    namespace Shanzhizi.Control.MenuControl
     
    {
        public partial class windowMenuControl : UserControl
        {
            public windowMenuControl()
            {
                InitializeComponent();
            }


            #region 主菜单操作事件
           
              // 菜单操作事件
            private void WindowMenu_Click(object sender, RoutedEventArgs e)
            {
                Button button = sender as Button;
                switch (button.Name)
                {
                    case "home":
                        break;

                    case "favorite":
                        break;

                    case "study":
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new 工程造价());
                        break;

                    case "album":
                       
                        break;

                    case "diary":
                        break;

                    case "office":
                        break;

                    case "pdf":
                        break;

                    case "iexplorer":               
                       break;

                   case "communication":
                        break;

                    case "music":
                        break;

                    case "video":
                        break;

                    case "setting":
                  break;

       case "crafting":
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new blackboardControl());
                        break;

                    case "MinWindow":
                        Application.Current.MainWindow.WindowState = WindowState.Minimized;
                        break;

                    case "FullWindow":
                        Application.Current.MainWindow.WindowState = WindowState.Maximized;
                        break;

                    case "CloseWindow":
                        Application.Current.MainWindow.Close();
                        break;

                    default:
                       
                        break;
                }
            }
            #endregion
        }
    }

    我要实现以上两个功能,我该如何改写现有代码,请各位师兄弟姐妹帮忙,小弟在此感谢各位博友的帮组,本人再次在此感谢Wanpeng给予的帮助。

    2012年9月4日 17:23
  • 1. 放弃使用const变量,使用私有变量并且在Window.Loaded和Window.SizeChanged事件中修改这两个值:

            private double rectWidth = 0;
            private double rectHeight = 0;
    
            void Window_SizeChanged(object sender, SizeChangedEventArgs e) {
                this.rectWidth = this.rect.ActualWidth;
                this.rectHeight = this.rect.ActualHeight;
            }
    
            void Window_Loaded(object sender, RoutedEventArgs e) {
                this.rectWidth = this.rect.ActualWidth;
                this.rectHeight = this.rect.ActualHeight;
            }
    
            void Window_MouseMove(object sender, MouseEventArgs e) {
                MousePosition = e.GetPosition(this.container);
                double x = (this.container.ActualHeight - rectHeight) / 2;
                if ((MousePosition.X > (this.container.ActualWidth - rectWidth - RECT_MARGIN_WIDTH))
                    && (MousePosition.Y > x - RECT_MARGIN_HEIGHT)
                    && (MousePosition.Y < this.container.ActualHeight - x + RECT_MARGIN_HEIGHT)) {
                    ShowRect();
                } else {
                    HideRect();
                }
            }

    ps. 这时,在隐藏时,请将你的控件设为Hidden而非Collapsed,否则,你需要处理关于rectWidth和rectHeight的赋值问题。

    2. 鉴于你的需求,我更建议你使用Grid布局,而非StackPanel,并且将MenuBar设置Margin,而非VerticalAlignment。例如:

            <local:Menubar x:Name="rect" Width="40" Margin="0 100" HorizontalAlignment="Right" Visibility="Hidden" />

    3. 这个是控件的MouseWheel事件的处理,建议你使用ScrollViewer包裹该控件,并且参考如下案例处理该事件:

            void Menubar_MouseWheel(object sender, MouseWheelEventArgs e) {
                this.scrollView.ScrollToVerticalOffset(e.Delta);
            }


    Wanpeng wanpeng.ones@gmail.com

    2012年9月5日 3:39
  • 非常感谢Wanpeng 对我所提出的问题的意义讲解,经过调试,Wanpeng 给出的代码能达到我所期望的要求。但第二点未能达到我的目的,可能是我表达为清楚。

    我的意思是当程序在运行过程中,通过鼠标改变拖到改变主窗口的大小(MainWindow.xaml),控件的现实高度也能随之改变,既当主窗口的大小(MainWindow.xaml)变化是,控件(windowMenuControl.xaml)能检测到主窗口的变化,自动调整控件的高度,而不是通过控件在主窗口的引入过程中进行装饰( Margin="0 100")。Wanpeng 给出的代码在表面上达到了预期效果,但控件实践高度并未发生变化。例如主窗口启动时高为1000,控件高为600,通过鼠标拖到窗口变为900,这时控件能自动检测到主窗口的变化,windowMenuControl.xaml在主窗口中的高度按相应比例缩小60,及实际显示高度为740。这过变化包括windowMenuControl.xaml控件的Height、Grid的Height、ScrollViewer的Height均改变,而不是改变主窗口的现实效果( <local:Menubar x:Name="rect" Width="40" Margin="0 100" HorizontalAlignment="Right" Visibility="Hidden" />)。现将完整代码张贴如下,请指教:

    MainWindow.xaml 前台代码(主窗口代码):

    <Window x:Class="Shanzhizi.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:windowMenucontrol="clr-namespace:Shanzhizi.Control.MenuControl"
            Title="私人空间"    Width="1360" Height="745" 
            WindowStartupLocation="CenterScreen"  WindowStyle="None"
            PreviewMouseMove="Window_PreviewMouseMove"
            MouseLeftButtonDown="WindowMove_MouseLeftButtonDown"
            Loaded="Window_Loaded"
            SizeChanged="Window_SizeChanged"
            ResizeMode="CanResizeWithGrip" WindowState="Normal" Topmost="True" >

        <Grid >
            <windowMenucontrol:windowMenuControl x:Name="WMC"
                LostFocus="windowMenuControl_LostFocus"
                GotFocus="windowMenuControl_GotFocus"
                ContextMenuOpening="windowMenuControl_ContextMenuOpening"
                Panel.ZIndex="1" Height="600" Margin="0 50" />
            <Grid Name="mainwindowgrid"></Grid>
        </Grid>
    </Window>

    MainWindow.xaml 后台代码(主窗口代码):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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;

    using Shanzhizi.Control.MenuControl;

    namespace Shanzhizi
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
               
            }
            #region 窗口菜单自动显示/隐藏

            private double width = 35;
            private double height = 600;

            private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                this.width = this.WMC.ActualWidth;
                this.height = this.WMC.ActualHeight;
            }
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                this.width = this.WMC.ActualWidth;
                this.height = this.WMC.ActualHeight;
            }
           
            private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
            {
                /*通过鼠标焦点获自定义用户控件自动显示与隐藏 假设标题栏在120和620之间*/
                if ((e.GetPosition(this).Y >= ((this.ActualHeight - height) / 2)) &&
                    (e.GetPosition(this).Y <= ((this.ActualHeight - height) / 2) + height) &&
                    (e.GetPosition(this).X >= (this.ActualWidth - width)))
                {
                    WMC.Visibility = Visibility.Visible;
                }else{
                    WMC.Visibility = Visibility.Hidden;
                }       
            }
            //获得焦点
            private void windowMenuControl_LostFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Collapsed;
            }
            //失去焦点
            private void windowMenuControl_GotFocus(object sender, RoutedEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;         
            }
            //窗体事件焦点
            private void windowMenuControl_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                WMC.Visibility = Visibility.Visible;
            }
            #endregion 
        
            #region 窗口移动
            private void WindowMove_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                if (e.GetPosition(this).Y >= 120 && e.GetPosition(this).Y <= 620)
                {
                    if (e.GetPosition(this).X >= 1325)
                    {
                        if (WMC.Visibility == Visibility.Visible)
                        {
                            if (e.LeftButton == MouseButtonState.Pressed)
                                this.DragMove();
                        }
                    }
                }
            }
            #endregion

        }
    }

    windowMenuControl.xaml前台到码(所引入的控件代码):

    <UserControl x:Class="Shanzhizi.Control.MenuControl.windowMenuControl"
                 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"
                 mc:Ignorable="d" MaxHeight="600" MaxWidth="35"  HorizontalAlignment="Right" VerticalAlignment="Center" d:DesignHeight="645" d:DesignWidth="35" xmlns:my="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon" >

        <UserControl.Resources>
            <ResourceDictionary Source="windowMenuControlDictionary.xaml" />
        </UserControl.Resources>
        <Grid Height="600" Width="35" HorizontalAlignment="Center" VerticalAlignment="Center">
            <ScrollViewer x:Name="scrollView"  MouseWheel="MouseWheel_MouseWheel">
                <Grid Height="Auto" Width="35">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Button Grid.Row="0"  Name="home" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/home.png" />
                    </Button>
                    <Button Grid.Row="1" Name="favorite" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/stuff.png" />
                    </Button>
                    <Button Grid.Row="2" Name="album" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/TN_psd.JPG" />
                    </Button>
                    <Button Grid.Row="3" Name="study" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/study.png" />
                    </Button>
                    <Button Grid.Row="4" Name="diary" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/album.tif" />
                    </Button>
                    <Button Grid.Row="5" Name="office" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/RTF.File.png" />
                    </Button>
                    <Button Grid.Row="6" Name="pdf" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/PDF.File.png" />
                    </Button>
                    <Button Grid.Row="7" Name="iexplorer" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/IExplorer.png" />
                    </Button>
                    <Button Grid.Row="8" Name="communication" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/mWorld_green.png" />
                    </Button>
                    <Button Grid.Row="9" Name="music" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/Musica_min.png" />
                    </Button >
                    <Button Grid.Row="10" Name="video" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/mWorld_cd_Yellow.png" />
                    </Button>
                    <Button Grid.Row="11" Name="setting" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/applications.png" />
                    </Button>
                    <Button Grid.Row="12" Name="crafting" Height="35"  Click="WindowMenu_Click" >
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/Crafting.jpg" />
                    </Button>
                    <Button Grid.Row="13" Name="MinWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/-.png" />
                    </Button>
                    <Button  Grid.Row="14" Name="FullWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/+.png" />
                    </Button>
                    <Button Grid.Row="15" Name="CloseWindow" Click="WindowMenu_Click">
                        <Image Source="/Shanzhizi;component/Control/MenuControl/ico/delete.png" />
                    </Button>
                </Grid>
            </ScrollViewer>
        </Grid>
    </UserControl>

    windowMenuControl.xaml后台到码(所引入的控件代码):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 Shanzhizi.Control.MenuControl
     
    {
        public partial class windowMenuControl : UserControl
        {
            public windowMenuControl()
            {
                InitializeComponent();
            }

             // 菜单操作事件
            private void WindowMenu_Click(object sender, RoutedEventArgs e)
            {
                Button button = sender as Button;
                switch (button.Name)
                {
                    case "home":
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new homePage());
                        break;

                    case "favorite":
                       
                        break;

                    case "study":
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new 工程造价());
                        break;

                    case "album":
                       
                        break;

                    case "diary":
                        break;

                    case "office":
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new textEditor());
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new textEditorControl());
                        break;

                    case "pdf":
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new pdfReader());
                        break;

                    case "iexplorer":
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new iexplorer());
                       
                        break;

                    case "communication":
                       
                        break;

                    case "music":
                       
                        break;

                    case "video":
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        //(App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new video());
                        break;

                    case "setting":
                       
                        break;

                    case "crafting":
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new blackboardControl());
                        break;

                    case "MinWindow":
                        Application.Current.MainWindow.WindowState = WindowState.Minimized;
                        break;

                    case "FullWindow":
                        Application.Current.MainWindow.WindowState = WindowState.Maximized;
                        break;

                    case "CloseWindow":
                        Application.Current.MainWindow.Close();
                        break;

                    default:
                       
                        break;
                }
            }
            #endregion

            private void MouseWheel_MouseWheel(object sender, MouseWheelEventArgs e)
            {
                scrollView.ScrollToVerticalOffset(e.Delta);
            }
        }
    }

    我个人猜想要实现这样的信息功能,代码应该学在windowMenuControl.xaml后台到码(所引入的控件代码)中。

    还有也个问题,WPF中没有MDI功能,我又不想引入的控件放到主窗口中,我应该这样实现该功能。((App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Clear();
                        (App.Current.MainWindow as MainWindow).mainwindowgrid.Children.Add(new blackboardControl());不是加载到主窗口,而是建立新的窗口)

    2012年9月6日 15:03
  • 因为你的问题在MainWindow的布局上,所以不应当由你所说的,在MenuContorl中。

    你的前台布局中,给MenuControl设置了高度,它自然是不会变的,你使用Margin布局就可以了,不需要指定高度。

    新的问题请另外开贴。


    Wanpeng wanpeng.ones@gmail.com

    2012年9月7日 2:14