• WPF学习笔记“窗口”一:入门


      学习目标:显示和定位窗口的各种方法;  

          窗口类之间应当如何交互以及WPF提供的内置对话框;  

          制作奇异的窗口效果(例如:非矩形窗口  透明窗口以及具有Aero玻璃效果的窗口);

          探索WPF对Windows7任务栏编程的支持;

     一 windows类继承于ContentControl类,它只能包含单个字元素;

         比较实用的属性:

          AllowsTransparecy属性设置为true;并且Background为透明色时;窗口将不会接受鼠标事件;呈现一种镂空的感觉;

          WindowStyle为None时,可以创建形状不规则的窗口;

          WindowStartupLocation属性,决定窗口在界面中出现的位置;

          WindowState属性,决定决定窗口最大化.最小化.正常的状态;

       显示窗口的两种方式:Show()和ShowDialog()

         窗口的关闭:this.Close();

       窗口的隐藏:this.Hide();this.Visibility = Visibility.Hidden;

       窗口的定位:一般使用WindowStartupLocation属性来决定窗口出现的位置;

            还可以使用Top和Left属性来决定窗口出现的位置;

            可以采用System.Windows.SystemParameters类(此类用于对系统设置进行检索)来检索有关屏幕实际大小的基本信息;    

            double screenHeight = System.Windows.SystemParameters.FullPrimaryScreenHeight;

            double screenWidth = System.Winodws.SystemParameters.FullPrimaryScreenWidth;

            this.Top = (screenHeight - this.Height) / 2;

            this.Left = (screenWidth - this.Width) / 2;

            还可以使用Systemparameters.WorkArea矩形:使窗口位于可用屏幕区域的中央.工作区域不包括停靠任务栏的区域;

            double workHeight = System.Windows.SystemParameters.WorkArea.Height;

            double workWidth = System.Winodws.SystemParameters.WorkArea.Width; 

            this.Top = (workHeight- this.Height) / 2;

            this.Left = (workWidth- this.Width) / 2;

          注:需要把位置设置好之后再show(),否则窗口会出现两次明显的位置移动;

        保存和还原窗口位置:

    View Code
     1 public class WindowClass
     2     {
     3         public static string RegPath = @"Software\MyApp\";
     4 
     5         public static void SaveSize(Window win)
     6         {
     7             // Create or retrieve a reference to a key where the settings
     8             // will be stored.
     9             RegistryKey key;
    10             key = Registry.CurrentUser.CreateSubKey(RegPath + win.Name);
    11 
    12             key.SetValue("Bounds", win.RestoreBounds.ToString(System.Globalization.CultureInfo.InvariantCulture));
    13         }
    14 
    15         public static void SetSize(Window win)
    16         {
    17             RegistryKey key;
    18             key = Registry.CurrentUser.OpenSubKey(RegPath + win.Name);
    19 
    20             if (key != null)
    21             {
    22                 Rect bounds = Rect.Parse(key.GetValue("Bounds").ToString());
    23                 if (Rect.Empty == bounds)
    24                 {
    25                     return;
    26                 }
    27                 win.Top = bounds.Top;
    28                 win.Left = bounds.Left;
    29 
    30                 // Only restore the size for a manually sized
    31                 // window.
    32                 if (win.SizeToContent == SizeToContent.Manual)
    33                 {
    34                     win.Width = bounds.Width;
    35                     win.Height = bounds.Height;
    36                 }
    37             }
    38         }
    View Code
    1         private void button1_Click(object sender, RoutedEventArgs e)
    2         {
    3             Test.WindowClass.SetSize(this);
    4         }
    5 
    6         private void button2_Click(object sender, RoutedEventArgs e)
    7         {
    8             Test.WindowClass.SaveSize(this);
    9         }

      

        窗口交互:当窗口之间交互时,Window.Activate()方法通常可以带来方便,该方法可以将所期望的窗口转换为活动窗口.还可以使用Window.IsActive属性测试一个窗口

    当前是否是活动窗口,以及是否是唯一的活动窗口;

        窗口的所有权:为了支持窗口之间的拥有关系,Window类增加了两个属性.Owner属性是一个指向窗口的引用,引用的窗口拥有当前窗口.OwnedWindows属性是当前窗口

    拥有的所有窗口的结合;可以简单地设置窗口的Owner属性;

          ToolWinodw winTool =  new ToolWindow();

          winTool.Owner = this;

          winTool.Show();

          注:被拥有的窗口始终以非模态方式显示,为了移出一个被拥有的窗口,需要将其Owner属性设置为空引用;

        对话框模型:

          DialogWindow dialog = new DialogWindow();

          if(dialog.ShowDialog() == true)

          {

          }

          else

          {

          }

        通用对话框:

          WPF没有为所有的Windows API通用对话框提供包装程序,WPF的一个目标是与Windows API互相独立,从而可以在其他环境(如浏览器)中使用或移植到其他平台.

      此外,许多内置对话框是非常古老的,对于现代应用程序它们不应当是第一选择,而且Windows 7 不鼓励使用对话框,而鼓励使用基于任务的窗格和导航;

        MessageBox.Show();

    View Code
     OpenFileDialog myDialog = new OpenFileDialog();
    
                myDialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF" +
                  "|All files (*.*)|*.*";
                myDialog.CheckFileExists = true;
                myDialog.Multiselect = true;
    
                if (myDialog.ShowDialog() == true)
                {
                    lstFiles.Items.Clear();
                    foreach (string file in myDialog.FileNames)
                    {
                        lstFiles.Items.Add(file);
                    }                
                }

        WPF没有提供颜色选取器 字体选取器以及文件夹浏览器,但是可以使用.net的system.windows.forms类获取这些要素

  • 相关阅读:
    洗礼灵魂,修炼python(20)--自定义函数(1)—基础概念
    洗礼灵魂,修炼python(19)--文件I/O操作,linecache,fileinput模块
    洗礼灵魂,修炼python(18)--温故加知新
    洗礼灵魂,修炼python(17)--跨平台操作三剑客—os,os.path.sys模块
    洗礼灵魂,修炼python(16)--列表进阶话题—>上节作业讲解+copy模块,浅拷贝,深拷贝
    洗礼灵魂,修炼python(15)--列表进阶话题—>列表解析/列表生成器
    洗礼灵魂,修炼python(14)--模块decimal, fractions,operator,collections以及精度介绍
    洗礼灵魂,修炼python(13)--模块random,math,pickle
    748. Shortest Completing Word
    542. 01 Matrix
  • 原文地址:https://www.cnblogs.com/gengyuanchao/p/2725011.html
Copyright © 2020-2023  润新知