如何复制此渐变面板控件? [英] how to replicate this gradient panel control?

查看:88
本文介绍了如何复制此渐变面板控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Pro .NET 2.0 Windows窗体和自定义控件这本书.
在第12章中,创建了一个渐变面板.我尝试复制该过程,但是不能.

如果我将项目创建为Windows控件,则将有一个名为GradientPanel.cs的文件.该文件与 GradientPanel.Designer.cs 相关联.

我下载了在线资源并找到了这个项目.我们有GradientPanel.cs文件,
但是此文件与 GradientPanel.resx 相关联.

有人可以告诉我如何将这个自定义控件复制为在线源代码示例吗?这两个项目有什么区别?

谢谢一百万.


这是本书和源代码的链接.
http://www.apress.com/9781590594391 [^]
您可以查看此页面底部的源代码"标签.

您可以下载源代码并查看第12章-GradientPanel项目.
谢谢.

I am working on the book Pro .NET 2.0 Windows Forms and Custom Controls.
on chapter 12 a gradient panel is created. I try to replicate the process but can not.

if I create a project as windows control then I will have a file named as GradientPanel.cs. this file is assoicated with GradientPanel.Designer.cs.

I downloaded the online source and find this project. we have GradientPanel.cs file,
but this file is assoicated with GradientPanel.resx.

Can anybody tell me how to replicate this custom control as online source code example? what is the difference between the two projects?

Thanks a million.


here is the link for the book and source code.
http://www.apress.com/9781590594391 [^]
you can look at the source code tab in the bottom section of this page.

you can download the source code and look at chapter 12 - GradientPanel project.
Thanks.

推荐答案

我没有您的书的代码,但我认为这并不重要.当您更好地了解文件的用途时,它可能会有所帮助.我假设您使用的是Visual Studio版本.您的书是有关Win Forms 2.0的,因此您至少需要VS2005.任何版本,甚至VS2005 Express都可以.

将项目添加到项目时,Visual Studio提供了可供选择的模板.在您的情况下,这将是UserControl或CustomControl的模板.请查阅您的书以找出正确的书.

选择您的控件模板并为其命名.然后,Visual Studio会将用于空控件的文件添加到项目中.有些文件是可选的(例如xxx.designer.cs),取决于是否立即添加它们取决于模板.不要为此感到困惑.对于名称为xxx的控件,您最终将在项目文件夹中获得以下文件:

xxx.cs:这是源代码文件,在其中声明了代表您的控件的类.它由您编辑.在这里,您应该填写书中的代码,通常由类构造函数,帮助器方法,事件处理程序,属性和成员变量组成.请记住:这是您的游乐场.您编写的任何代码都必须放入该文件.

xxx.designer.cs:这个文件是可选的,因为您可以将其中的所有代码都写到xxx.cs中并且可以工作.最重要的是:您几乎不必查看此文件,也根本不应该在其中写入任何内容.当您使用设计器向控件中添加其他控件时,设计器将自动编写所需的代码.此自动生成的代码进入此文件.这样设计人员就不会干扰您在xxx.cs中编写的代码,为避免麻烦,您不应更改此文件中的任何内容.使用设计视图,工具箱和属性窗口可从书中重新创建控件的设计.稍后,您可以尝试对其进行修改,甚至可以自己设计.

对于初学者,您应该精确地从书中复制设计.特别是这适用于控件的类型及其名称.如果将控件替换为另一种类型,或者为该控件赋予的名称不同于代码中使用的名称,则它将显然不起作用.

xxx.resx:在设计器中进行的某些设置在代码中实现是不切实际的.现在,仅将该文件视为由设计者维护的属性及其值的列表.当您设计控件以支持不同的语言时,这些东西就变得非常重要.然后,您将拥有每种支持的语言的资源文件,并且将在运行时选择正确的语言.现在,只需使用设计器和属性视图更改布局中控件的属性,然后让设计器担心此文件的内容.


希望对您有所帮助,但我想知道您的书中没有提及任何内容.您是否跳过了一些无趣的"部分?
I don''t have your book''s code, but I think it does not really matter. Perhaps it helps when you understand the purpose of the files a little better. I assume you use a version of Visual Studio. Your book is about Win Forms 2.0, so you will need at least VS2005. Any version, even VS2005 Express will do.

When you add an item to a project, Visual Studio offers templates from which you can choose. In your case this would be the template for a UserControl or a CustomControl. Consult your book to find out which is the right one.

Select the template for your control and give it a name. Visual Studio will then add the files for an empty control to the project. Some of the files are optional (like xxx.designer.cs) and it depends on the template wether they are added right away or not. Do not get confused by that. For a control with the name xxx you would eventually get the following files in the project folder:

xxx.cs: This is the source code file where the class which represents your control is declared. It is meant to be edited by you. Here you should fill in the code from your book, usually consisting of the classes constructors, helper methods, event handlers, properties and member variables. Just remember: This is your playground. Any code you write has to go into this file.

xxx.designer.cs: This file is optional in the sense that you could just as well write all the code which is in here into xxx.cs and it would work. Most important: You rarely have to look at this file and are not supposed to write anything into it at all. When you use the designer to add further controls to your control, the designer automaticcly writes the needed code. This automatically generated code goes into this file. This way the designer does not interfere with the code you have written in xxx.cs and, to avoid trouble, you should not change anything in this file. Use the design view, the toolbox and the properties window to recreate the control''s design from the book. Later you can try to modify it or even come up with a design of your own.

For starters, you should replicate the design from the book precisely. Especially this applies to the types of controls and their Names. It will obviously not work if you substitute a control for another type or if you give it another name than the one used in the code.

xxx.resx: Some of the settings you make in the designer are impractical to implement in code. For now, just regard this file as a list of properties and their values which is maintained by the designer. This stuff becomes really important when you design your control to support different languages. Then you will have a resource file for every supported language and the right one will be selected at runtime. For now, just change the properties of the controls in your layout with the designer and the properties view and let the designer worry about the contents of this file.


I hope this helps a little, but I wonder that your book does not mention any of this. Have you skipped some ''uninteresting'' parts perhaps?


Designer.CS 文件与控件/窗体关联,以保留序列化的属性和值在设计器中,例如,您向表单添加了一个按钮,并更改了其DockStyle,

在这种情况下,当值更改为非控件默认值时,设计人员将序列化DockStyle.

可以通过将Initialize component方法移动到相应的窗体中来删除此设计器文件.

Designer.resx:例如,您创建一个控件并更改其图像/图标,该信息将作为流序列化并保存在resx文件中.

并非所有控件都需要设计者/资源,例如,下面的一个简单的渐变面板就可以了!.

Designer.CS file is associate with the controls / forms to keep the properties and values that are serialized in the designer, For instance you added a button to the form and change its DockStyle,

In this case the designer will serialize the DockStyle when the value is changed and is not the default value of the control.

This designer file can be removed by moving the Initialize component method to the corresponding form.

Designer.resx : For instance you create a control and change its Image / Icon, this information will be serialized as a stream and kept in resx files.

Not all the controls requires a designer / resources, for instance a simple gradient panel could be the following one!.

public class GradientPanel : Panel
{
    public Color GradientBegin { get; set; }
    public Color GradientEnd { get; set; }

    public GradientPanel()
        : base()
    {
        this.GradientBegin = Color.Yellow;
        this.GradientEnd = Color.Green;
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        using (LinearGradientBrush brush = new                          
                       LinearGradientBrush(this.ClientRectangle, 
                       this.GradientBegin,
                       this.GradientEnd,
                       LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, this.ClientRectangle);
        }
    }
}



希望这会有所帮助!

Vallarasu S.



Hope this helps!

Vallarasu S.


这篇关于如何复制此渐变面板控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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