|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
c++是语言,其实C++和java的应用范围根本就不一样的。在java应用的领域内,c++是不合适的。所以微软才搞了C#和Java对抗。上个礼拜三入手下手学自界说控件,做了很多实习。花了一上中午间写了一个imageButton,之前就像写这个控件,只是不会。图片
注释
这个控件仿照winform中的button,能够撑持图片和笔墨。能够选择实行服务器端程序仍是客户端程序,另有一些复杂的设置。
不敷的是不撑持款式,下次但愿能够写一个工具条。
以下就是代码
以下为援用的内容:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web.UI.WebControls;
usingSystem.ComponentModel;
usingSystem.Web.UI;
usingSystem.Drawing.Design;
usingSystem.Drawing.Drawing2D;
namespaceClassLibrary1
{
[Serializable]
publicclassPicture
{
privateUnitheight=16;
privatestringsrc=string.Empty;
[NotifyParentProperty(true)]
[Browsable(true),Bindable(true),Description("图片路径"),Category("Appearance")]
[Editor("System.Web.UI.Design.ImageUrlEditor,System.Design,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a",typeof(UITypeEditor))]
publicstringSrc
{
get{returnthis.src;}
set{this.src=value;}
}
[DefaultValue(typeof(Unit),"16px")]
[NotifyParentProperty(true)]
publicUnitHeight
{
get{returnheight;}
set{height=value;}
}
privateUnitwidth=16;
[NotifyParentProperty(true)]
[DefaultValue(typeof(Unit),"16px")]
publicUnitWidth
{
get{returnwidth;}
set{width=value;}
}
publicenumAlign{Left,Right}
}
[Serializable]
publicclassLabel
{
privatestringtext=string.Empty;
[NotifyParentProperty(true)]
publicstringText
{
get{returntext;}
set{text=value;}
}
privateSystem.Drawing.FontfontFamily=newSystem.Drawing.Font("宋体",8);
[NotifyParentProperty(true)]
publicSystem.Drawing.FontFont
{
get{returnthis.fontFamily;}
set{this.fontFamily=value;}
}
}
[PersistChildren(false)]
[ParseChildren(true)]
publicclassImageButton:Control,INamingContainer,IPostBackEventHandler
{
publicenumRaiseEventType{Client,Server}
privatePicturepic=newPicture();
privatePicture.AlignpicAlign=Picture.Align.Left;
privateLabellabel=newLabel();
privatestringjsFunction=string.Empty;
privatestaticreadonlyobjectclickKey=newobject();
publicenumTextAlign{Left,Center,Right}
[Browsable(true),Bindable(true),Description("javascript办法"),Category("Action")]
publicstringJSFunction
{
get{returnthis.jsFunction;}
set{this.jsFunction=value;}
}
privateRaiseEventTyperaiseEvent=RaiseEventType.Server;
[Browsable(true),Bindable(true),Description("呼应事务体例"),Category("Action")]
publicRaiseEventTypeRaiseEvent
{
get{returnthis.raiseEvent;}
set{this.raiseEvent=value;}
}
privateTextAlignalign=TextAlign.Left;
[Browsable(true),Bindable(true),Description("笔墨的对齐体例"),Category("Appearance")]
publicTextAlignALign
{
get{returnalign;}
set{align=value;}
}
privateUnitwidth=80;
[Browsable(true),Bindable(true),Description("控件宽度"),Category("Appearance")]
[DefaultValue(typeof(Unit),"80px")]
publicUnitWidth
{
get{returnthis.width;}
set{this.width=value;}
}
[Browsable(true),Bindable(true),Category("Action")]
publiceventEventHandlerOnClick
{
add
{
Events.AddHandler(clickKey,value);
}
remove
{
Events.RemoveHandler(clickKey,value);
}
}
[Browsable(true),Bindable(true),Description("图片类"),Category("Appearance")]
publicPicture.AlignPicAlign
{
get{returnpicAlign;}
set{picAlign=value;}
}
[Browsable(true),Bindable(true),Description("图片类"),Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(ExpandableObjectConverter))]
[PersistenceMode(PersistenceMode.InnerProperty)]
publicPicturePic
{
get{returnpic;}
}
[Browsable(true),Bindable(true),Description("笔墨类"),Category("Appearance")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof(ExpandableObjectConverter))]
[PersistenceMode(PersistenceMode.InnerProperty)]
publicLabelLabel
{
get{returnlabel;}
}
protectedoverridevoidRender(HtmlTextWriterwriter)
{
if(raiseEvent==RaiseEventType.Server)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,this.ClientID));
}
else
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,"javascript:"+this.jsFunction);
}
writer.AddStyleAttribute(HtmlTextWriterStyle.Cursor,"hand");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width,this.width.Value.ToString()+"px");
if(align==TextAlign.Left)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign,"left");
}
elseif(align==TextAlign.Center)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign,"center");
}
else
{
writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign,"right");
}
writer.RenderBeginTag(HtmlTextWriterTag.Div);
if(PicAlign==Picture.Align.Left)
{
AddPic(writer);
AddLabel(writer);
}
else
{AddLabel(writer);
AddPic(writer);
}
writer.RenderEndTag();
//base.Render(writer);
}
privatevoidAddPic(HtmlTextWriterwriter)
{
writer.AddAttribute(HtmlTextWriterAttribute.Src,base.ResolveClientUrl(pic.Src));
writer.AddAttribute(HtmlTextWriterAttribute.Height,pic.Height.ToString());
writer.AddAttribute(HtmlTextWriterAttribute.Width,pic.Width.ToString());
writer.RenderBeginTag(HtmlTextWriterTag.Img);
writer.RenderEndTag();
//writer.Write("<imagesrc="+this.Src+"height="+pic.Height+"width="+pic.Width+"/>");
}
privatevoidAddLabel(HtmlTextWriterwriter)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.VerticalAlign,"middle");
writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize,label.Font.Size.ToString()+"pt");
writer.AddStyleAttribute(HtmlTextWriterStyle.FontFamily,label.Font.FontFamily.Name);
if(label.Font.Bold)
{
writer.AddStyleAttribute(HtmlTextWriterStyle.FontWeight,"Bold");
}
writer.RenderBeginTag(HtmlTextWriterTag.Label);
writer.Write(label.Text==string.Empty?this.ClientID.ToString():label.Text);
writer.RenderEndTag();
//writer.Write("<label>"+Label.Text+"</label>");
}
#regionIPostBackEventHandler成员
publicvoidRaisePostBackEvent(stringeventArgument)
{
EventHandlere=(EventHandler)Events[clickKey];
if(e!=null)
{
e(this,EventArgs.Empty);
}
}
#endregion
究竟是刚学,一定会有良多不敷和毛病,但愿人人斧正,感谢。
开头,持续勉力,看完别的的材料。
以前学了大概半年时间的asp(没有机会做大系统,最多是自己对公司系统做些调整和修改还有一些小程序)。应该说开始接触asp.net是今年元月5号的事。现在很想把公司的系统重新用.net来架构,却不知道如何下手。 |
|