简体   繁体   English

为c#windows应用程序提供自定义样式

[英]give custom style to c# windows application

I am wondering if there is a way to customize style of windows forms application built in visual studio using c#. 我想知道是否有一种方法来定制使用c#在visual studio中构建的Windows窗体应用程序的样式。 I have searched through the internet and couldn't find simple solution for overriding default view of the layout. 我在互联网上搜索过,无法找到覆盖默认布局视图的简单解决方案。 Is there a way to change layout with cascading style sheets? 有没有办法用级联样式表更改布局? Thanks in advance. 提前致谢。

Windows Forms apps do not support CSS , it is used when developing websites. Windows窗体应用程序不支持CSS ,它在开发网站时使用。

In Winforms you are limited to the styles which are listed in Properties window in GUI editor, unless you'd like to override OnPaint event and do custom drawing. 在Winforms中,您只能使用GUI编辑器的“ 属性”窗口中列出的样式,除非您要覆盖OnPaint事件并执行自定义绘图。

Some examples are: 一些例子是:

http://www.codeproject.com/Articles/8056/Creating-Custom-Shaped-Windows-Forms-in-NET http://www.codeproject.com/Articles/8056/Creating-Custom-Shaped-Windows-Forms-in-NET

http://geekswithblogs.net/kobush/archive/2005/07/04/CustomBorderForms.aspx http://geekswithblogs.net/kobush/archive/2005/07/04/CustomBorderForms.aspx

https://customerborderform.codeplex.com/ https://customerborderform.codeplex.com/

If you are looking for more customizable solution, you can turn to WPF. 如果您正在寻找更多可自定义的解决方案,可以转向WPF。

you can find answer here.. 你可以在这里找到答案..

or 要么

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        // Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

You can use a static resource such as this. 您可以使用此类静态资源。

Inside button tag Style="{DynamicResource sty3dBtn}" (You can set margins here too but sometimes thats best to do for each button depending on your own needs) 内部按钮标签Style =“{DynamicResource sty3dBtn}”(您也可以在这里设置边距,但有时根据您自己的需要最适合每个按钮)

You can use xml styling. 您可以使用xml样式。 The button is set to have a dropshadow and gradient and lights up when pressed etc. As I wanted to reuse these effects with similar buttons I created a reusable style in my Application.xaml enclosed in Application - Application.Resources tags and referenced its x:Key in style attribute of the button I wanted the effect on. 按钮设置为有阴影和渐变,按下时会亮起等等。因为我想用类似的按钮重用这些效果,我在Application.Appes.Resources标签中的Application.xaml中创建了一个可重用的样式并引用了它的x:键入我想要效果的按钮的样式属性。 You can do this on each page you choose but I think it can be better to place in a common area so its reusable throughout the scope of the class you place it in. Note the Target Type must match. 您可以在您选择的每个页面上执行此操作,但我认为最好放在公共区域中,以便它可以在您放置的类的整个范围内重复使用。请注意,目标类型必须匹配。

I'll post snippet of the xml style it turns a regular button into one with 3d effect. 我将发布xml样式的片段,它将常规按钮转换为具有3d效果的按钮。 You can reference this as many times as you need. 您可以根据需要多次引用它。 It cuts down on inline code clutter too so makes the page more readable in my opinion. 它也减少了内联代码的混乱,因此在我看来,页面更具可读性。

    <Style x:Key="sty3dBtn" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle x:Name="GelBackground" Opacity="1" RadiusX="9" RadiusY="9"
                               Fill="{TemplateBinding Background}" StrokeThickness="0.35"
                                   RenderTransformOrigin="0.5,0.5">
                            <Rectangle.Stroke>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="YellowGreen" Offset="0" />
                                    <GradientStop Color="Green" Offset="1" />
                                </LinearGradientBrush>
                            </Rectangle.Stroke>
                        </Rectangle>
                        <Rectangle x:Name="GelShine" Margin="2,2,2,0" VerticalAlignment="Top"
                                   RadiusX="6" RadiusY="6" Opacity="1" Stroke="Transparent" Height="15px">
                            <Rectangle.Fill>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="Yellow" Offset="0"/>
                                    <GradientStop Color="Transparent" Offset="1"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Brown">
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Fill" TargetName="GelBackground">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="Yellow" Offset="0"/>
                                        <GradientStop Color="Green" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="RenderTransform" TargetName="GelBackground">
                                <Setter.Value>
                                    <TransformGroup>
                                        <ScaleTransform ScaleX="0.9" ScaleY="0.9"/>
                                    </TransformGroup>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Fill" TargetName="GelBackground" Value="LightGray">

                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <!-- Contains animation code-->
        <Setter Property="Background" Value="Green"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Width" Value="55"/>
        <Setter Property="Height" Value="30"/>
    </Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM