[01] Borderless WPF program with drag, adjustable size, custom button style

Source: Internet
Author: User

Beginner WPF, using VS2012 to write a drag, resizable, custom button style of the borderless program.
The effect is as follows:

Implementation process:
First, drag, adjust the size, no border main reference: http://blog.csdn.net/dlangu0393/article/details/12548731

Two, the main completion of their own custom button effect.

1. WPF defines button style methods

(1) Add a resource dictionary file, such as Style.xaml. For example:

<ResourceDictionaryxmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml">    <BitmapImagex:key= "Bgmin">/images/bg_min.png</BitmapImage>    <BitmapImagex:key= "Bgminover">/images/bg_min_over.png</BitmapImage>    <Stylex:key= "Btntemplate"TargetType= "button">        <Setter Property= "Template">            <Setter.value>                <ControlTemplateTargetType="{x:type Button}">                    <BorderBackground="{TemplateBinding Background}">                        <ContentPresenterHorizontalAlignment= "Center"VerticalAlignment= "Center"/>                    </Border>                </ControlTemplate>            </Setter.value>        </Setter>    </Style>    <Stylex:key= "Btnminstyle"TargetType= "button"BasedOn="{StaticResource Btntemplate}">        <Setter Property= "Background">            <Setter.value>                <ImageBrushImageSource="{StaticResource Bgmin}"Stretch= "Fill"/>            </Setter.value>        </Setter>        <style.triggers>            <Trigger Property= "IsMouseOver"Value= "true">                <Setter Property= "Background">                    <Setter.value>                        <ImageBrushImageSource="{StaticResource Bgminover}"Stretch= "Fill"/>                    </Setter.value>                </Setter>            </Trigger>        </style.triggers>    </Style></ResourceDictionary>

(2) Application style

Referencing a resource file in a form XAML file Style.xaml

<window.resources>        <ResourceDictionary>            <resourcedictionary.mergeddictionaries>                <ResourceDictionarySource= "/theme/style.xaml"/>            </resourcedictionary.mergeddictionaries>        </ResourceDictionary></window.resources>

Set Button Style Property

<Style= "{StaticResource Btnminstyle}"/> 

(3) If you want to set the style of the button in the form class, use the following method

this. Minimizebutton.style = (Style) resources["btnminstyle"];

2. The position of the button changes with the window size

Define rows and columns in a grid in form XAML, for example, set grid to two column

<Grid>    ...    <grid.columndefinitions>                    <ColumnDefinition/>                    <ColumnDefinitionWidth= " the"/>    </grid.columndefinitions>    ...    ...</Grid>

The grid containing the buttons as child elements of the grid above, set in the second column

<x:name= "Systemcmdbar"Width= "  grid.column"= "1">... </ Grid >

The Systemcmdbar contains three buttons. This way, if the width of the first column is not set, the Systemcmdbar in the second column will always be on the right side of the window.

Third, the source program: Ase0701.zip

[01] Borderless WPF program with drag, adjustable size, custom button style

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.