码迷,mamicode.com
首页 > 其他好文 > 详细

获取位置及窗口的打开和隐藏

时间:2015-06-29 13:17:37      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

xaml代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="你好" Closing="Window_Closing_1" Height="233.792" Width="329.201">
    <Grid Margin="0,0,4.2,5.6">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Button Content="位置" HorizontalAlignment="Left" Margin="30,30,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1" Grid.ColumnSpan="2"/>
        <Button Content="打开" HorizontalAlignment="Left" Margin="30,109,0,0" VerticalAlignment="Center" Width="75" Click="Button_Click_2" Grid.ColumnSpan="2"/>
        <Button Content="隐藏" HorizontalAlignment="Left" Margin="226,110,0,0" VerticalAlignment="Center" Width="75" Click="Button_Click_3" Grid.ColumnSpan="2"/>

    </Grid>
</Window>

  xaml.cs代码:

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 WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //设置窗口状态
            this.WindowState = Properties.Settings.Default.MainWindowState;
            //设置位置 大小
            Rect rect = Properties.Settings.Default.MainWindowRect;
            this.Top = rect.Top;
            this.Left = rect.Left;
            this.Width = rect.Width;
            this.Height = rect.Height;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(this.RestoreBounds.ToString());//RestoreBounds,获取窗口在最小化或最大化之前的大小和位置,有4个枚举值,Top、Left、Width、Height。
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //保存当前位置、大小和状态,到配置文件
            Properties.Settings.Default.MainWindowState = this.WindowState;
            Properties.Settings.Default.MainWindowRect = this.RestoreBounds;
            Properties.Settings.Default.Save();
            if (n != null)
            {
                n.Close();
            }
        }
        NewWindow n;
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if(n==null)
            {
                n = new NewWindow();
                n.Show();
            }
            else
            {
                n.Show();
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            if (n!=null)
            {
                 n.Hide();
            }
        }

    }
}

  

获取位置及窗口的打开和隐藏

标签:

原文地址:http://www.cnblogs.com/Mr-xue/p/4607246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!