委托的应用(自定义控件)

委托的应用(自定义控件)
委托的应用(自定义控件)

委托的应用(自定义控件)

骤一:

打开Visual Studio 2005,点击创建项目,选择Visual C#语言在右边的模版中选择Windows 控件库,给项目取个名同时点击浏览选择一个存储位置单击确定,设置该控件的Name属性为ColorHatch,然后拖曳七个panel控件,设置它们的BackColor,例如上图的设置,接着选中这些panel设置它们的边框样式BorderStyle为Fixed 3D使其有凹凸感,以上这些是界面设计,接下来进入代码部分。

步骤二:

选中该控件右键单击查看代码,添加如下代码:

public ColorHatch()

{

InitializeComponent();

currentColor = panel1.BackColor;//panel1的背景色为当前颜色

}

private Color currentColor;

public Color CurrentColor

{

get { return currentColor; }

set

{

currentColor = panel1.BackColor = value;

}

}

//自定义事件参数类

public class ColorChangedEventArgs

{

private Color c1, c2;

public ColorChangedEventArgs(Color c1, Color c2)

{

this.c1 = c1;

this.c2 = c2;

}

public Color ColorBeforeChange

{

get { return c1; }

}

public Color ColorAfterChange

{

get { return c2; }

}

}

public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs e);//声明委托

public event ColorChangedEventHandler ColorChanged;//声明事件

protected virtual void OnColorChanged(ColorChangedEventArgs e)//当颜色改变时的方法,将自定义事件作为参数传递,从而触发颜色改变事件发生

{

if (ColorChanged != null)//如果有对象注册

ColorChanged(this, e);//调用所有注册对象的方法

}

步骤三:

选中这7个面板,到属性窗口事件里面双击Click添加这些代码:

Panel p = (Panel)sender;//强制转化为panel,从而观察到用户点击的是哪个小面板

Color c1 = currentColor;

currentColor = panel1.BackColor = p.BackColor;//每次点击的哪个面板都将该面板的颜色赋值给panel1的背景色同时也赋值给当前颜色

ColorChangedEventArgs args = new ColorChangedEventArgs(c1, currentColor);//创建事件参数类对象

OnColorChanged(args);//调用 OnColorChanged方法

步骤四:

接着新建一个Window应用程序项目,将我们之前写好的ColorHatch控件拖放进来,拖曳一个TextBox,设置其Text属性为Hello World!,找到该文本框的TextChanged事件双击进去

步骤五:

在窗体中确保选中ColorHatch这个控件,然后在属性窗口找到ColorChanged事件双击添加代码如下; textBox1.ForeColor = e.ColorAfterChange;//面板颜色的改变文本框的字体颜色也相应跟着改变

android 自定义圆角头像以及使用declare-styleable进行配置属性解析

android 自定义圆角头像以及使用declare-styleable进行配置属性解析由于最新项目中正在检查UI是否与效果图匹配,结果关于联系人模块给的默认图片是四角稍带弧度的圆角,而我们截取的图片是正方形的,现在要给应用统一替换。应用中既用到大圆角头像(即整个头像是圆的)又用到四角稍带弧度的圆角头像,封装一下以便重用。以下直接见代码 [java] view plain copy 在CODE上查看代码片派生到我的代码片 package com.test.demo; import com.test.demo.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Shader.TileMode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Parcelable; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.widget.ImageView; /** * 圆角imageview */ public class RoundImageView extends ImageView { private static final String TAG = "RoundImageView"; /** * 图片的类型,圆形or圆角 */ private int type; public static final int TYPE_CIRCLE = 0; public static final int TYPE_ROUND = 1; /** * 圆角大小的默认值

用户控件事件执行顺序

在WebForm1页面上直接放置用户控件WebUserControl1时的事件顺序:WebUserControl1_Init WebForm1_Init Page_Load WebUserControl1_Load WebForm1_PreRender WebUserControl1_PreRender WebUserControl1_Unload WebForm1_Unload 在WebForm1页面的OnInit方法中用代码Load用户控件WebUserControl1时的事件顺序:WebForm1_Init: OnInit WebForm1: Loading controls WebUserControl1_Init : OnInit WebForm1: added to Controls WebForm1: LoadViewState WebUserControl1: LoadViewState WebUserControl1: LoadPostData Begin WebUserControl1: LoadPostData End WebForm1_PageLoad: OnLoad after base's Onload. WebUserControl1_Load: after base's OnLoad iWebUserControl1: RaisePostDataChange WebForm1: OnPreRender WebUserControl1: OnPreRender WebForm1: SavedViewState WebUserControl1: SaveViewState 在WebForm1页面的Page_Load方法中用代码Load用户控件WebUserControl1时的事件顺序:WebForm1_Init : OnInit WebForm1: LoadViewState WebForm1_PageLoad: OnLoad after base's Onload. WebForm1: Loading controls WebUserControl1_Init : OnInit WebUserControl1: LoadViewState WebForm1: added to Controls WebUserControl1_Load: after base's OnLoad WebUserControl1: LoadPostData Begin WebUserControl1: LoadPostData End iWebUserControl1: RaisePostDataChange WebForm1: OnPreRender WebUserControl1: OnPreRender WebForm1: SavedViewState WebUserControl1: SaveViewState 在WebForm1页面的Button1_Click事件中用代码Load用户控件WebUserControl1时的事件顺序:WebForm1_Init Page_Load Button1_Click WebUserControl1_Init WebUserControl1_Load WebForm1_PreRender WebUserControl1_PreRender WebUserControl1_Unload WebForm1_Unload 在WebForm1页面的Page_Load事件中用代码Load用户控件WebUserControl1,WebUserControl1又在Page_Load事件中用代码Load用户控件WebUserControl2时的事件顺序:WebForm1_Init Page_Load WebUserControl1_Init WebUserControl1_Load WebUserControl2_Init WebUserControl2_Load WebForm1_PreRender WebUserControl1_PreRender WebUserControl2_PreRender WebUserControl2_Unload WebUserControl1_Unload WebForm1_Unload

C# 自定义控件制作和使用实例

C# 自定义控件制作和使用实例 第一步:新建一个控件库项目:myControl 第二步:从工具箱里面拖动1个PictureBox、1个Button、6个Lable控件到用户界面上,布局如下: 如上图,设置pictureBox的Name为picBox,背景为白色,Button的Name为btnOpen,

另外靠左的三个Lable的Text属性分别为:文件名称,文件大小,文件尺寸,靠右的三个Lable的Name分别为:lblName, lblLength, lblSize. 第三步:添加处理程序代码 在btnOpen的Click事件写入代码,打开一个打开文件对话框,选择一个图形文件,打开并将它显示在picBox上。 private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog ofdPic = new OpenFileDialog(); ofdPic.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif"; ofdPic.FilterIndex = 1; ofdPic.RestoreDirectory = true; ofdPic.FileName = ""; if (ofdPic.ShowDialog() == DialogResult.OK) { string sPicPaht = ofdPic.FileName.ToString(); FileInfo fiPicInfo = new FileInfo(sPicPaht); long lPicLong = fiPicInfo.Length / 1024; string sPicName = https://www.360docs.net/doc/c69828471.html,; string sPicDirectory = fiPicInfo.Directory.ToString(); string sPicDirectoryPath = fiPicInfo.DirectoryName; Bitmap bmPic = new Bitmap(sPicPaht); if (lPicLong > 400) { MessageBox.Show("此文件大小為" + lPicLong + "K;已超過最大限制的K范圍!"); } else { Point ptLoction = new Point(bmPic.Size); if (ptLoction.X > picBox.Size.Width || ptLoction.Y > picBox.Size.Height) { picBox.SizeMode = PictureBoxSizeMode.Zoom; } else { picBox.SizeMode = PictureBoxSizeMode.CenterImage; } } picBox.LoadAsync(sPicPaht); lblName.Text = sPicName; lblLength.Text = lPicLong.ToString() + " KB"; lblSize.Text = bmPic.Size.Width.ToString() + "×" + bmPic.Size.Height.ToString(); }

.NET4.0 用户控件的概述

https://www.360docs.net/doc/c69828471.html,4.0 用户控件的概述 用户控件是页面的一段,包含了静态HTML代码和服务器控件。其优点在于一旦创建了一个用户控件,可以在同一个应用的多个页面中重用。并且,用户可以在Web用户控件中,添加该控件的属性、事件和方法。 1.什么是用户控件 用户控件(后缀名为.ascx)文件与https://www.360docs.net/doc/c69828471.html,网页窗体(后缀名为.aspx)文件相似。就像网页窗体一样,用户控件由用户接口部分和控制标记组成,而且可以使用嵌入脚本或者.cs代码后置文件。用户控件能够包含网页所能包含的任何东西,包括静态HTML内容和https://www.360docs.net/doc/c69828471.html,控件,它们也作为页面对象(Page Object)接收同样的事件(如Load和PreRender),也能够通过属性(如Application,Session,Request 和Response)来展示https://www.360docs.net/doc/c69828471.html,内建对象。 用户控件使程序员能够很容易地跨Web应用程序划分和重复使用公共UI功能。与窗体页相同,用户可以使用任何文本编辑器创作用户控件,或者使用代码隐藏类开发用户控件。 此外,用户控件可以在第一次请求时被编译并存储在服务器内存中,从而缩短以后请求的响应时间。与服务器端包含文件(SSI)相比,用户控件通过访问由https://www.360docs.net/doc/c69828471.html,提供的对象模型支持,使程序员具有更大的灵活性。程序员可以对在控件中声明的任何属性进行编程,而不只是包含其他文件提供的功能,这与其他任何https://www.360docs.net/doc/c69828471.html,服务器控件一样。 此外,可以独立于包含用户控件的窗体页中除该控件以外的部分来缓存该控件的输出。这一技术称作片段缓存,适当地使用该技术能够提高站点的性能。例如,如果用户控件包含提出数据库请求的https://www.360docs.net/doc/c69828471.html,服务器控件,但该页的其余部分只包含文本和在服务器上运行的简单代码,则程序员可以对用户控件执行片段缓存,以改进应用程序的性能。 用户控件与普通网页页面的区别是: ●用户控件开始于控件指令而不是页面指令。 ●用户控件的文件后缀是.ascx,而不是.aspx。它的后置代码文件继承于 https://www.360docs.net/doc/c69828471.html,erControl类.事实上,UserControl类和Page类都继承于同一个 TemplateControl类,所有它们能够共享很多相同的方法和事件。 ●没有@Page指令,而是包含@Control指令,该指令对配置及其他属性进行定义。 ●用户控件不能被客户端直接访问,不能作为独立文件运行,而必须像处理任何控件一 样,将它们添加到https://www.360docs.net/doc/c69828471.html,页中。 ●用户控件没有html、body、form元素,但同样可以在用户控件上使用HTML元素和 Web控件。 用户可以将常用的内容或者控件以及控件的运行程序逻辑,设计为用户控件,

android studio 控件常用属性

android studio 控件常用属性 下面是RelativeLayout各个属性 1.android:layout_above="@id/xxx" --将控件置于给定ID控件之上 2.android:layout_below="@id/xxx" --将控件置于给定ID控件之下 3. android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐 4.android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐 5. android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐 6.android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐 7.android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐 8.android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐 9.android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐 10. android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐 11. android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐 12.android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐 13.android:layout_centerInParent="true" --将控件置于父控件的中心位置 14.android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置 15.android:layout_centerVertical="true" --将控件置于垂直方向的中心位置 android:layout_width 设置组件的宽度 android:layout_height 设置组件的高度 android:id 给组件定义一个id值,供后期使用 android:background 设置组件的背景颜色或背景图片 android:text 设置组件的显示文字 android:textColor 设置组件的显示文字的颜色 android:layout_below 组件在参考组件的下面 android:alignTop 同指定组件的顶平行

Android平台我的日记设计文档

Android平台我的日记 设计文档 项目名称:mydiray 项目结构示意: 阶段任务名称(一)布局的设计 开始时间: 结束时间: 设计者: 梁凌旭 一、本次任务完成的功能 1、各控件的显示 二、最终功能及效果 三、涉及知识点介绍 四、代码设计 activity_main.xml:

android:layout_centerHorizontal="true" android:layout_marginTop="88dp" android:text="@string/wo" android:textSize="35sp"/>

相关文档
最新文档