重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
建一个自定义的Web控件MyContro的步骤:
成都创新互联公司主营宁河网站建设的网络公司,主营网站建设方案,重庆APP开发,宁河h5小程序开发搭建,宁河网站营销推广欢迎宁河等地区企业咨询
1)引用
using System;
using System.IO;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
2)保证MyContro是从WebControl继承的。
public class MyControl:WebControl
3)重写下面两个函数,这是必须的
protected override void Render(HtmlTextWriter writer)
{
this.CreateChildControls();
base.Render (writer);
}
protected override void CreateChildControls()
{
// 清除现有的子控件及其 ViewState
this.Controls.Clear();
this.ClearChildViewState();
// 生成控件树
// 生成环境表格(一行,两个单元格)
Table myTable = new Table();
//build the table row生成表格中的行
TableRow row = new TableRow();
myTable.Rows.Add(row);
// 生成单元格
TableCell myCell = new TableCell();
//用来生成链接按钮导航条的代码。每个按钮都显示有一个 Webdings 字符,可以根据需//禁用,并被绑定到内部的 Click 事件处理程序。
LinkButton myLinkButton = new LinkButton();
myLinkButton.ID = "MyLinkButton";
myLinkButton.Click += new EventHandler(myLinkButton_Click);
myLinkButton.Font.Name = "宋体";
myLinkButton.ToolTip = "好玄啊!";
myLinkButton.Text = "请点我";
myCell.Controls.Add(myLinkButton);
row.Cells.Add(myCell);
Controls.Add(t);
}
4)自定义的事件的方法
private void myLinkButton_Click(object sender,System.EventArgs e)
{
Page.Response.Write("想干点什么就写点什么吧,就这么简单!");
}
5)编译一下,然后点工具—》添加/移除工具箱项
浏览到你编译生成的dll
6)使用
控件(包括字体)随窗体按比例缩放
Public Class frmDl'根据自己窗体名变更
Dim x As Single = 0
Dim y As Single = 0
Private Sub frmDl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
x = Me.Width
y = Me.Height
setTag(Me)
End Sub
'递归取控件的原始大小和位置,用tag来纪录
Private Sub setTag(ByVal obj As Object)
For Each con As Control In obj.Controls
con.Tag = con.Width ":" con.Height ":" con.Left ":" con.Top ":" con.Font.Size
'如果是容器控件,则递归继续纪录
If con.Controls.Count 0 Then
setTag(con)
End If
Next
End Sub
'递归重新设定控件的大小和位置
Private Sub setControls(ByVal newx As Single, ByVal newy As Single, ByVal obj As Object)
For Each con As Control In obj.Controls
con.AutoSize = False
Dim mytag() As String = con.Tag.ToString.Split(":")
con.Width = mytag(0) * newx
con.Height = mytag(1) * newy
con.Left = mytag(2) * newx
con.Top = mytag(3) * newy
'计算字体缩放比例,缩放字体
Dim currentSize As Single = (mytag(1) * newy * mytag(4)) / mytag(1)
con.Font = New Font(con.Font.Name, currentSize, _
con.Font.Style, con.Font.Unit)
'如果是容器控件,则递归继续缩放
If con.Controls.Count 0 Then
setControls(newx, newy, con)
End If
Next
End Sub
Private Sub frmDl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
'得到现在窗体的大小,然后根据原始大小计算缩放比例
Dim newx As Single = Me.Width / x
Dim newy As Single = Me.Height / y
setControls(newx, newy, Me)
End Sub
End Class
控件.location=New Point(X,Y)
X,Y为整数,即控件坐标,原点在窗体左上角
以下代码测试成功,图片大小和位置改变后,标签控件依然在这个点上。
Dim px, py, lx, ly As Integer
Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
If px 0 And py 0 Then
Label1.Location = New Point(PictureBox1.Size.Width / px * lx, PictureBox1.Size.Height / py * ly)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
px = PictureBox1.Size.Width
py = PictureBox1.Size.Height
lx = Label1.Location.X
ly = Label1.Location.Y
End Sub
这是控件被锁定,要移动控件在窗体中单击右键点击【锁定控件】后就可以移动了。
两种方法,一种是固定窗口大小,一种是控件在窗口大小改变时,自动按窗口位置进行改变;
以上为个人看法,手打很辛苦,希望采纳,谢谢您的支持。
谢谢您的支持,有任何疑问欢迎您再次通过此渠道提问,让大家共同解决,共同进步!
以上回答仅为“百度规则”,没有任何违法行为,请楼主采纳,谢谢!