【WPF】C# WPF Windows窗体设置

1、WPF自定义窗口最大化后遮挡任务栏的解决方法

WPF自定义窗口最大化后遮挡任务栏的解决方法

public class ConstrainedWindow : Window
{
    public ConstrainedWindow()
    {
        this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
        this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
    }

}

窗体初始化时限定最大化大小即可

或则直接在xaml文件定义MaxHeight和MaxWidth(也可通过属性设置界面应用资源):

<Window x:Class="GUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="960" Width="1280" WindowStyle="None" MaxWidth="{StaticResource {x:Static SystemParameters.MaximizedPrimaryScreenWidthKey}}" MaxHeight="{StaticResource {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}">

 2、获取父级窗体

方式一、通过当前控件名获取父级窗体

Window targetWindow = Window.GetWindow(button);


方式二、通过当前控件获取父级窗体

Window parentWindow = Window.GetWindow(this);


方式三、类--Application

Window mainwin = Application.Current.MainWindow;
MessageBox.Show(mainwin.Title, mainwin.Title);
 

3、WPF 窗体属性设置

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        MinWidth="300" MinHeight="800"
        Background="Transparent" //透明
        WindowStartupLocation="CenterScreen" //窗体开始出现在屏幕中的位置位置
        AllowsTransparency="True"//窗体设置成透明
        WindowStyle="None"  // 隐藏窗体外形
        ResizeMode="CanResizeWithGrip"  //调整窗体大小
         TopMost="true"  这是wpf 程序 重绘造成的。可以通过设置window的TopMost属性解决。TopMost属性让窗体一直保持桌面最上层。微信桌面客户端 就是采用这种方式。 >

 4、双击窗体标题栏和正常化、最大化按钮的联动

双击窗体标题栏 然后改变图标

 

 

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 LYFWPFUI.Theme.CustomeControls
{
    /// <summary>
    /// CMNButton.xaml 的交互逻辑
    /// </summary>
    public partial class CMNButton : UserControl
    {
        public CMNButton()
        {
            InitializeComponent();
            Application.Current.MainWindow.SizeChanged += ChangeIco;
        }

        private void ChangeIco(object? sender, EventArgs e)
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized && btnWindowNormal.IsChecked==false)
            {
                btnWindowNormal.IsChecked = true;
            }else if (Application.Current.MainWindow.WindowState == WindowState.Normal && btnWindowNormal.IsChecked == true)
            {
                btnWindowNormal.IsChecked = false;
            }
        }

        private void closebtn_Click(object sender, RoutedEventArgs e)
        {
            Window win = Window.GetWindow(this);
            //提示是否要关闭
            if (MessageBox.Show("确定要退出系统?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question) ==
               MessageBoxResult.Yes)
            {
                Application.Current.MainWindow.SizeChanged -= ChangeIco;//删除委托,防止内存泄漏
                System.Windows.Application.Current.Shutdown();
            }
        }

        private void btnWindowNormal_Checked(object sender, RoutedEventArgs e)
        {
            Window win = Application.Current.MainWindow;
            if ((bool)btnWindowNormal.IsChecked)
            {
                win.WindowState = WindowState.Maximized;             
            };
          

        }
        private void minbtn_Click(object sender, RoutedEventArgs e)
        {
            Window win = Application.Current.MainWindow;
            if ( win != null)
            {
                win.WindowState = WindowState.Minimized;
            }
        }
        private void btnWindowNormal_Unchecked(object sender, RoutedEventArgs e)
        {
            Window win = Application.Current.MainWindow;
            if (win != null)
            {
                win.WindowState = WindowState.Normal;        
            }
        }

    }
}

 

posted @ 2023-03-14 01:10  小林野夫  阅读(595)  评论(0编辑  收藏  举报
原文链接:https://www.cnblogs.com/cdaniu/