国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【W(wǎng)PF】WindowChrome 自定義窗口完美實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了【W(wǎng)PF】WindowChrome 自定義窗口完美實(shí)現(xiàn)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

簡介

Microsoft官網(wǎng)關(guān)于 WindowChome 的介紹

截取Microsoft文章的一段話:
??若要在保留其標(biāo)準(zhǔn)功能時(shí)自定義窗口,可以使用該 WindowChrome 類。 該 WindowChrome 類將窗口框架的功能與視覺對象分開,并允許你控制應(yīng)用程序窗口的客戶端和非客戶端區(qū)域之間的邊界。 通過 WindowChrome 該類,可以通過擴(kuò)展工作區(qū)來覆蓋非工作區(qū),將 WPF 內(nèi)容置于窗口框架中。 同時(shí),它通過兩個(gè)不可見區(qū)域保留系統(tǒng)行為: 調(diào)整邊框 和 標(biāo)題 區(qū)域的大小。

效果圖

【W(wǎng)PF】WindowChrome 自定義窗口完美實(shí)現(xiàn)
【W(wǎng)PF】WindowChrome 自定義窗口完美實(shí)現(xiàn)

自定義最小化、最大化、關(guān)閉按鈕

  • 最小化按鈕
<Style
    x:Key="SystemCloseButtonStyle"
    BasedOn="{StaticResource SystemButtonStyleBase}"
    TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Background="{TemplateBinding Background}">
                    <Viewbox Width="12" Height="12">
                        <Path Data="M550.848 502.496l308.64-308.896a31.968 31.968 0 1 0-45.248-45.248l-308.608 308.896-308.64-308.928a31.968 31.968 0 1 0-45.248 45.248l308.64 308.896-308.64 308.896a31.968 31.968 0 1 0 45.248 45.248l308.64-308.896 308.608 308.896a31.968 31.968 0 1 0 45.248-45.248l-308.64-308.864z" Fill="{TemplateBinding BorderBrush}" />
                    </Viewbox>
                    <ContentPresenter
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Content="{TemplateBinding Content}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource CloseColor}" />
                        <Setter Property="BorderBrush" Value="{StaticResource DominantColor}" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
  • 最大化按鈕
<Style
    x:Key="SystemMaxButtonStyle"
    BasedOn="{StaticResource SystemButtonStyleBase}"
    TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Background="{TemplateBinding Background}">
                    <Viewbox Width="12" Height="12">
                        <Path Data="M959.72 0H294.216a63.96 63.96 0 0 0-63.96 63.96v127.92H64.28A63.96 63.96 0 0 0 0.32 255.84V959.4a63.96 63.96 0 0 0 63.96 63.96h703.56a63.96 63.96 0 0 0 63.96-63.96V792.465h127.92a63.96 63.96 0 0 0 63.96-63.96V63.96A63.96 63.96 0 0 0 959.72 0zM767.84 728.505V959.4H64.28V255.84h703.56z m189.322 0H831.8V255.84a63.96 63.96 0 0 0-63.96-63.96H294.216V63.96H959.72z" Fill="{TemplateBinding BorderBrush}" />
                    </Viewbox>
                    <ContentPresenter
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Content="{TemplateBinding Content}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource SuspensionColor}" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
  • 關(guān)閉按鈕
<Style
    x:Key="SystemMinButtonStyle"
    BasedOn="{StaticResource SystemButtonStyleBase}"
    TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid Background="{TemplateBinding Background}">
                    <Viewbox Width="12" Height="10">
                        <Path Data="M928.2 548h-832c-17.7 0-32-14.3-32-32s14.3-32 32-32h832c17.7 0 32 14.3 32 32s-14.3 32-32 32z" Fill="{TemplateBinding BorderBrush}" />
                    </Viewbox>
                    <ContentPresenter
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Content="{TemplateBinding Content}" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="{StaticResource SuspensionColor}" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
/// <summary>
/// 窗口移動
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Move_Click(object sender, System.Windows.Input.MouseButtonEventArgs e) => this.DragMove();

/// <summary>
/// 最小化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnMin_Click(object sender, RoutedEventArgs e) => WindowState = WindowState.Minimized;

/// <summary>
/// 最大化/還原
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnMax_Click(object sender, RoutedEventArgs e) => WindowState = WindowState is WindowState.Normal ? WindowState.Maximized : WindowState.Normal;

/// <summary>
/// 關(guān)閉
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BtnClose_Click(object sender, RoutedEventArgs e) => Application.Current.Shutdown();

布局實(shí)現(xiàn)

首先我們需要在 MainWindow 也就是我們的主窗口中的 Window.Resources 中實(shí)現(xiàn) WindowChrome 的基本樣式:
WindowChrome.ResizeBorderThickness 設(shè)置不可見邊框?qū)挾?br>WindowChrome.CaptionHeight> 設(shè)置屬于標(biāo)題欄的范圍——高度
WindowChrome.UseAeroCaptionButtons 是否啟用默認(rèn)系統(tǒng)按鈕功能——三大金剛鍵
WindowChrome.NonClientFrameEdges 設(shè)置客戶區(qū)域,使用 bottom 可以實(shí)現(xiàn)加載時(shí)空白窗口而不顯示默認(rèn)窗口,提升用戶體驗(yàn)

<Window
    x:Class="SignalRClient.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:local="clr-namespace:SignalRClient"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title=""
    Width="880"
    Height="620"
    MinWidth="700"
    MinHeight="500"
    Style="{StaticResource mainWindow}"
    WindowChrome.WindowChrome="{DynamicResource WindowChromeKey}"
    WindowStartupLocation="CenterScreen"
    mc:Ignorable="d">

    <Window.Resources>
        <WindowChrome x:Key="WindowChromeKey">
            <WindowChrome.ResizeBorderThickness>
                <Thickness>5</Thickness>
            </WindowChrome.ResizeBorderThickness>
            <WindowChrome.CaptionHeight>60</WindowChrome.CaptionHeight>
            <WindowChrome.UseAeroCaptionButtons>false</WindowChrome.UseAeroCaptionButtons>
            <WindowChrome.NonClientFrameEdges>bottom</WindowChrome.NonClientFrameEdges>
        </WindowChrome>
    </Window.Resources>
</Window>

重寫窗口,實(shí)現(xiàn)最大化窗口下,標(biāo)題欄及客戶區(qū)域偏移問題的修正。
通過代碼獲取當(dāng)前窗口的工作區(qū)域,及任務(wù)欄以外的其他區(qū)域
System.Windows.SystemParameters.WorkArea.Width 獲取工作區(qū)域的寬
System.Windows.SystemParameters.WorkArea.Height 獲取工作區(qū)域的高
為什么要使用 ValueConverter 主要是因?yàn)?WorkArea 返回的類型無法直接 binding xaml

<ValueConverters:WorkAreaWidth x:Key="workAreaWidth" />
<ValueConverters:WorkAreaHeight x:Key="workAreaHeight" />

<Style x:Key="mainWindow" TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Window">

                <ContentControl x:Name="window" Content="{TemplateBinding Content}" />

                <ControlTemplate.Triggers>
                    <Trigger Property="WindowState" Value="Maximized">
                        <Setter TargetName="window" Property="MaxHeight" Value="{Binding Converter={StaticResource workAreaHeight}}" />
                        <Setter TargetName="window" Property="MaxWidth" Value="{Binding Converter={StaticResource workAreaWidth}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
using System;
using System.Globalization;
using System.Windows.Data;

namespace SignalRClient.ValueConverters
{
    internal class WorkAreaWidth : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Windows.SystemParameters.WorkArea.Width;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
using System;
using System.Globalization;
using System.Windows.Data;

namespace SignalRClient.ValueConverters
{
    internal class WorkAreaHeight : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Windows.SystemParameters.WorkArea.Height;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

結(jié)語

一開始,確實(shí)很難搞,Microsoft 的文檔,里面也并沒有,詳細(xì)介紹窗口內(nèi)容溢出的問題,但是只要仔細(xì)研究過 WPF 的同學(xué)都知道,很多東西是可以通過 Trigger 來實(shí)現(xiàn)的。Get 到這一點(diǎn)很多問題就迎刃而解了。歡迎大家留言通過探討 ??????文章來源地址http://www.zghlxwxcb.cn/news/detail-457965.html

到了這里,關(guān)于【W(wǎng)PF】WindowChrome 自定義窗口完美實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 基于WPF Opencv實(shí)現(xiàn)一個(gè)圖像可移動、縮放和可交互的顯示窗口

    基于WPF Opencv實(shí)現(xiàn)一個(gè)圖像可移動、縮放和可交互的顯示窗口

    學(xué)習(xí)Halcon的HSmartWindowControl窗口控件,用WPF和Opencv仿照了一個(gè)。 顯示控件的主體是兩個(gè)Canvas: Root Canvas是背景,刷上了網(wǎng)格; 把要顯示的圖像作為Image Canvas的圖像背景, 后面圖像的移動,縮放,實(shí)質(zhì)都是Image Canvas。 可交互,學(xué)習(xí)的是Halcon的繪圖對象,WPF的Path來實(shí)現(xiàn)的,Path類

    2023年04月09日
    瀏覽(66)
  • wpf 在MVVM中使用IOC思想通過注冊窗體的方法實(shí)現(xiàn)在ViewModel里面打開其他窗口的方法

    理論:在MVVM中,View通過添加ViewModel的引用來指定DataContext,也就是View可以引用ViewModel,但ViewModel不可以引用View,而ViewModel在實(shí)現(xiàn)邏輯過程中如果需要訪問其他的窗體該怎么辦呢,比如打開一個(gè)對話框,這里通過IOC思想來解決該問題。 1 新建wpf應(yīng)用 2 新建Views,Models,ViewModel

    2024年02月12日
    瀏覽(18)
  • 使用WPF開發(fā)自定義用戶控件,以及實(shí)現(xiàn)相關(guān)自定義事件的處理

    使用WPF開發(fā)自定義用戶控件,以及實(shí)現(xiàn)相關(guān)自定義事件的處理

    在前面隨筆《使用Winform開發(fā)自定義用戶控件,以及實(shí)現(xiàn)相關(guān)自定義事件的處理》中介紹了Winform用戶自定義控件的處理,對于Winform自定義的用戶控件來說,它的呈現(xiàn)方式主要就是基于GDI+進(jìn)行渲染的,對于數(shù)量不多的控件呈現(xiàn),一般不會覺察性能有太多的問題,隨著控件的數(shù)量

    2024年02月02日
    瀏覽(40)
  • WPF --- 非Button自定義控件實(shí)現(xiàn)點(diǎn)擊功能

    今天在做一個(gè)設(shè)置文件夾路徑的功能,就是一個(gè)文本框,加個(gè)按鈕,點(diǎn)擊按鈕,彈出 FolderBrowserDialog 再選擇文件夾路徑,簡單做法,可以直接 StackPanel 橫向放置一個(gè) TextBox 和一個(gè) Image Button ,然后點(diǎn)擊按鈕在 后臺代碼中給 ViewModel 的 FilePath 賦值。但是這樣屬實(shí)不夠優(yōu)雅,UI 不

    2024年02月12日
    瀏覽(27)
  • vue前端實(shí)現(xiàn)圖片下載,實(shí)現(xiàn)點(diǎn)擊按鈕彈出本地窗口,選擇自定義保存路徑

    vue前端實(shí)現(xiàn)圖片下載,實(shí)現(xiàn)點(diǎn)擊按鈕彈出本地窗口,選擇自定義保存路徑

    直接上代碼,廢話不多說,點(diǎn)關(guān)注,不迷路 一、下載代碼 二、別找代碼了,不用代碼就可以實(shí)現(xiàn) 以下按照步驟一步一步來 按照紅色箭頭所指,用鼠標(biāo)戳它 恭喜你,功能完成了

    2024年02月13日
    瀏覽(33)
  • 【W(wǎng)PF.NET開發(fā)】WPF中的窗口

    【W(wǎng)PF.NET開發(fā)】WPF中的窗口

    目錄 1、窗口類 2、實(shí)現(xiàn)窗口 2.1 為 MSBuild 配置窗口 3、窗口生存期 3.1 打開窗口 3.2 窗口激活 3.3 關(guān)閉窗口 3.4 窗口生存期事件 4、窗口位置 4.1 最頂層窗口和 z 順序 5、窗口大小 6、大小調(diào)整屬性的優(yōu)先級順序 7、窗口狀態(tài) 8、窗口外觀 8.1 重設(shè)大小模式 8.2 Window style 8.3 任務(wù)欄顯示

    2024年02月04日
    瀏覽(29)
  • Qt自定義窗口部件/控件(實(shí)現(xiàn)一個(gè)十六進(jìn)制微調(diào)框SpinBox)

    Qt自定義窗口部件/控件(實(shí)現(xiàn)一個(gè)十六進(jìn)制微調(diào)框SpinBox)

    在某些情況下,我們發(fā)現(xiàn)Qt窗口控件需要更多的自定義定制,這些定制可能要比它在Qt設(shè)計(jì)師里可設(shè)置的屬性或者對它調(diào)用的那些函數(shù)更多一些。一個(gè)簡單而直接的解決方法就是對相關(guān)的窗口部件類進(jìn)行子類化并且使它能夠滿足我們的需要。 本文主要是通過實(shí)現(xiàn)一個(gè)十六進(jìn)制微調(diào)

    2024年02月11日
    瀏覽(28)
  • Qt 自定義窗口的標(biāo)題欄,重寫鼠標(biāo)事件實(shí)現(xiàn),關(guān)閉隱藏,最大化/最小化,重寫窗口事件函數(shù),實(shí)現(xiàn)鼠標(biāo)選中邊框拉大拉小,雙擊標(biāo)題欄切換窗口最大化和最小化

    Qt 自定義窗口的標(biāo)題欄,重寫鼠標(biāo)事件實(shí)現(xiàn),關(guān)閉隱藏,最大化/最小化,重寫窗口事件函數(shù),實(shí)現(xiàn)鼠標(biāo)選中邊框拉大拉小,雙擊標(biāo)題欄切換窗口最大化和最小化

    Qt 自定義窗口的標(biāo)題欄,重寫鼠標(biāo)事件實(shí)現(xiàn),關(guān)閉隱藏,最大化/最小化,重寫窗口事件函數(shù),實(shí)現(xiàn)鼠標(biāo)選中邊框拉大拉小,雙擊標(biāo)題欄切換窗口最大化和最小化 1、main.cpp 2、widget.h 3、widget.cpp 4、效果展示 5、完成

    2024年02月16日
    瀏覽(93)
  • 在WPF窗口中增加水印效果

    在WPF窗口中增加水印效果

    ** ** 以Canvas作為水印顯示載體,在Canvas中創(chuàng)建若干個(gè)TextBlock控件用來顯示水印文案,如下圖所示 然后以每一個(gè)TextBlock的左上角為中心旋轉(zhuǎn)-30°,最終效果會是如圖紅線所示: 為了達(dá)到第一行旋轉(zhuǎn)后剛好與窗口上邊沿齊平,需要計(jì)算第一行其實(shí)位置的Top坐標(biāo),由于旋轉(zhuǎn)角度為

    2024年02月04日
    瀏覽(29)
  • 界面控件DevExpress WPF流程圖組件,完美復(fù)制Visio UI!(一)

    界面控件DevExpress WPF流程圖組件,完美復(fù)制Visio UI!(一)

    DevExpress WPF Diagram(流程圖)控件幫助用戶完美復(fù)制Microsoft Visio UI,并將信息豐富且組織良好的圖表、流程圖和組織圖輕松合并到您的下一個(gè)WPF項(xiàng)目中。 P.S :DevExpress WPF擁有120+個(gè)控件和庫,將幫助您交付滿足甚至超出企業(yè)需求的高性能業(yè)務(wù)應(yīng)用程序。通過DevExpress WPF能創(chuàng)建有

    2024年02月04日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包