如何设置windowbase应用程序的默认字体,字体颜色,字体系列,字体大小 [英] how to set default font, font color, font family,font size for windowbase Application

查看:111
本文介绍了如何设置windowbase应用程序的默认字体,字体颜色,字体系列,字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生,

我想设置所有表格的默认背景,

默认字体,字体颜色,整个应用程序上所有控件的字体系列。



先生如何做到这一点。



提前谢谢。

解决方案

有几种方法可以做到这一点;我将在这里概述我认为最简单的方式,它可能会,也可能不会达到OP的目标。在未来,我打算在CP上发表一篇关于更好的方式来做这篇文章的文章。



但是,首先,请注意已经一种继承Color和Font,内置:当你创建一个WinForm,并设置它的Font和Fore / Back-Color属性时:当你将控件拖放到那个Form上时某些类型的控件将继承Font和Fore / Back-Color属性(例如,Panel,GroupBox,Button,Label,PictureBox都继承BackColor)。对于Controls按钮和Label,他们的Text将继承Form的ForeColor。



操作方法,简单:



1.像往常一样创建主UI表单。



2.在项目中添加另一个表单,给它命名为ThemeForm。



a。配置你的ThemeForm的大小,颜色,字体等设置,以便他们定义你希望其他表格继承的常见外观。



3.添加项目的其他表格:更改他们的类声明,以便他们继承您的ThemeForm:

  public   partial   class  SecondaryForm1:ThemeForm 
{
public SecondaryForm1()
{
InitializeComponent();
}
}

讨论:



1.如果你把控件放在你的ThemeForm上,那么这些控件也将以锁定形式出现在任何继承自ThemeForm的表单上。因此,如果您为ThemeForm上的Button定义了Button Click EventHandler,并且在派生的Form中单击该Button,则会触发ThemeForm中定义的EventHandler。



这可能是您不想要的行为,或者您希望利用的行为,以便在继承的表单之间启用某些类型的通信。



例如,如果我为ThemeForm中的'button1定义了Click EventHandler:

  private   void  button1_Click( object  sender,EventArgs e)
{
MessageBox.Show( string .Format( 在ThemeForm中:从{0},(sender as Control).Parent.Name));
}

,然后点击名为'Form2的派生表格中的'button1,我将收到消息:从:Form2开火



如何摆脱你不想在派生表格中使用的继承控件(不像你想象的那么简单),以及如何在派生控件中添加EventHandlers,同时删除或离开活动,它继承的EventHandlers ...是我打算在我打算写的文章中完全解决的问题。



2.警告:如果事情变得搞砸了所以您收到一条错误消息,试图显示继承表单的设计视图...但您仍然可以运行您的应用程序...只需保存当前项目中的所有内容,关闭它,然后在Visual Studio中重新打开它。 / blockquote>

没有自动的方法:当你创建一个子控件时,修改后的字体样式不会被继承。您必须创建一个Font类实例并循环遍历所有将其应用于其中的控件。您还必须对各个Color实例执行相同的操作。



一旦完成,将在重新绘制控件时应用对该Font实例的更改(您可以通过调用表单的Invalidate来强制执行此操作)。但是,颜色更改不会以这种方式传播,因为Color是 struct ,因此是值类型。每次更改时,您都必须自己更改每个控件的颜色值。



可能值得查看表单的外观,但这非常复杂和/或者很贵。


hello sir,
I want to set default background of all forms,
default font,font color,font family for all controls on whole Application.

Sir how to do this.

thanks in advance.

解决方案

There are several ways you can do this; I'll outline here what I think is the simplest way, which may, or may not, meet the OP's goals. In the future I intend to publish an article here on CP about a "fancier" way to do this.

But, first, note that there is already a kind-of inheritance of Color, and Font, built-in: when you create a WinForm, and you set its Font and Fore/Back- Color properties: when you drag-drop Controls onto that Form certain Types of Controls will inherit the Font and Fore/Back-Color properties (for example, Panel, GroupBox, Button, Label, PictureBox all inherit the BackColor). For the Controls Button, and Label, their Text will inherit the ForeColor of the Form.

How-to, Simple:

1. Create your main UI form as usual.

2. Add another Form to your Project, give it a name like "ThemeForm."

a. configure your ThemeForm's Size, Color, Font, etc., settings so they define the "common look and feel" that you want other Forms to inherit.

3. Add other Forms to your Project: change their Class declaration so they inherit from your ThemeForm:

public partial class SecondaryForm1 : ThemeForm
{
    public SecondaryForm1 ()
    {
        InitializeComponent();
    }
}

Discussion:

1. If you put Controls on your "ThemeForm," then those Controls will also appear in "locked" form on any Form that inherits from "ThemeForm." So, if you define a Button Click EventHandler for a Button on the ThemeForm, and you click that Button in a derived Form, the EventHandler defined in the ThemeForm will fire.

That may be a behavior you don't want, or a behavior you will want to take advantage of for enabling certain types of communication between inherited Forms.

For example, if I define this Click EventHandler for 'button1 in the ThemeForm:

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show(string.Format("in ThemeForm: firing from: {0}",(sender as Control).Parent.Name ));
}

, and I click on 'button1 in a derived Form named 'Form2, I will get the message: "firing from: Form2"

How to "get rid of" inherited Controls you don't want to use in derived Forms (not as simple as you might think), and how to add EventHandlers to the derived Controls, while either removing or leaving active, its inherited EventHandlers ... is something I intend to address fully in the article I plan to write.

2. Caution: if things get "messed up" so you get an error message trying to show the Design View of an inheriting Form ... but you can still run your application ... just save everything in the current Project, close it, and then re-open it in Visual Studio.


There is no automatic way to do this: a modified font style is not "inherited" when you cretae a child control. You will have to create a Font class instance and loop through all controls applying it to then individually. You will have to do the same for the individual Color instances as well.

Once you have done that, changes to that Font instance will be applied when the controls are repainted (which you can force by calling Invalidate for the form). However, Color changes will not be propagated in this way, as Color is a struct and thus a value type. You will have to change the Color values of each control yourself each time it changes.

It might be worth looking at skinning your form, but that gets very complex and / or expensive.


这篇关于如何设置windowbase应用程序的默认字体,字体颜色,字体系列,字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆