Public Class UserControl1 Dim a, b, c, d As Double
Private Sub UserControl1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'a = (Me.Label1.Location.X + Me.Label1.Width / 2) / Me.PictureBox1.Image.Size.Width
'b = (Me.Label1.Location.Y + Me.Label1.Height / 2) / Me.PictureBox1.Image.Size.Height
'c = Me.Label1.Width / Me.PictureBox1.Image.Size.Width
'd = Me.Label1.Height / Me.PictureBox1.Image.Size.Height
End Sub
Dim IsLoading As Boolean = True
Sub New() ' 此调用是设计器所必需的。
InitializeComponent() ' 在 InitializeComponent() 调用之后添加任何初始化。 a = (Me.Label1.Location.X + Me.Label1.Width / 2) / Me.PictureBox1.Image.Size.Width
b = (Me.Label1.Location.Y + Me.Label1.Height / 2) / Me.PictureBox1.Image.Size.Height
c = Me.Label1.Width / Me.PictureBox1.Image.Size.Width
d = Me.Label1.Height / Me.PictureBox1.Image.Size.Height
IsLoading = False
End Sub
Private Sub UserControl1_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
If IsLoading Then Exit Sub
Dim Isize0, Isize1 As Size
Isize0 = Me.PictureBox1.Image.Size
Dim d0, d1 As Double
d0 = Isize0.Width / Isize0.Height
d1 = sender.width / sender.height
If d1 >= d0 Then
Isize1 = New Size(sender.height * d0, sender.height)
Else
Isize1 = New Size(sender.width, sender.width / d0)
End If
With Me.Label1
.Size = New Size(Isize1.Width * c, Isize1.Height * d)
.Location = New Point(Isize1.Width * a - .Width / 2 + (sender.width - Isize1.Width) / 2, Isize1.Height * b - .Height / 2 + (sender.Height - Isize1.Height) / 2)
End With End Sub
End Classusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace UiLib
{
public partial class UiBeng : UserControl
{
double a, b, c, d;
bool IsLoading = true;
public UiBeng()
{
InitializeComponent();
//中心点与picbox比例
a = (double)(lbl_ShowValue.Location.X + lbl_ShowValue.Width / 2) / pictureBox1.Image.Size.Width;
b = (double)(lbl_ShowValue.Location.Y + lbl_ShowValue.Height / 2) / pictureBox1.Image.Size.Height; //比例
c = (double)lbl_ShowValue.Width / pictureBox1.Image.Size.Width;
d = (double)lbl_ShowValue.Height / pictureBox1.Image.Size.Height; IsLoading = false; } Size Isize0, Isize1;
double d0, d1;
private void UiBeng_Resize(object sender, EventArgs e)
{
if (IsLoading == true)
return; Isize0 = pictureBox1.Image.Size; //picbox的宽高比例
d0 = (double)Isize0.Width / Isize0.Height;
//usercontrol的宽高比例
d1 = (double)this.Width / this.Height;
if (d1 >= d0)
{
Isize1 = new Size((int)(this.Height * d0), (int)(this.Height));
}
else
Isize1 = new Size((int)(this.Width), (int)((double)this.Width / d0));
lbl_ShowValue.Size = new Size((int)(Isize1.Width * c), (int)(Isize1.Height * d));
lbl_ShowValue.Location = new Point((int)(Isize1.Width * a - lbl_ShowValue.Width / 2 + (this.Width - Isize1.Width) / 2), (int)(Isize1.Height * b - lbl_ShowValue.Height / 2 + (this.Height - Isize1.Height) / 2));
}
}
}
我是根据上面的vb.net转录成C#的,但是发现执行效果不一致,不明白有什么不一样的,请教
Private Sub UserControl1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'a = (Me.Label1.Location.X + Me.Label1.Width / 2) / Me.PictureBox1.Image.Size.Width
'b = (Me.Label1.Location.Y + Me.Label1.Height / 2) / Me.PictureBox1.Image.Size.Height
'c = Me.Label1.Width / Me.PictureBox1.Image.Size.Width
'd = Me.Label1.Height / Me.PictureBox1.Image.Size.Height
End Sub
Dim IsLoading As Boolean = True
Sub New() ' 此调用是设计器所必需的。
InitializeComponent() ' 在 InitializeComponent() 调用之后添加任何初始化。 a = (Me.Label1.Location.X + Me.Label1.Width / 2) / Me.PictureBox1.Image.Size.Width
b = (Me.Label1.Location.Y + Me.Label1.Height / 2) / Me.PictureBox1.Image.Size.Height
c = Me.Label1.Width / Me.PictureBox1.Image.Size.Width
d = Me.Label1.Height / Me.PictureBox1.Image.Size.Height
IsLoading = False
End Sub
Private Sub UserControl1_SizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
If IsLoading Then Exit Sub
Dim Isize0, Isize1 As Size
Isize0 = Me.PictureBox1.Image.Size
Dim d0, d1 As Double
d0 = Isize0.Width / Isize0.Height
d1 = sender.width / sender.height
If d1 >= d0 Then
Isize1 = New Size(sender.height * d0, sender.height)
Else
Isize1 = New Size(sender.width, sender.width / d0)
End If
With Me.Label1
.Size = New Size(Isize1.Width * c, Isize1.Height * d)
.Location = New Point(Isize1.Width * a - .Width / 2 + (sender.width - Isize1.Width) / 2, Isize1.Height * b - .Height / 2 + (sender.Height - Isize1.Height) / 2)
End With End Sub
End Classusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace UiLib
{
public partial class UiBeng : UserControl
{
double a, b, c, d;
bool IsLoading = true;
public UiBeng()
{
InitializeComponent();
//中心点与picbox比例
a = (double)(lbl_ShowValue.Location.X + lbl_ShowValue.Width / 2) / pictureBox1.Image.Size.Width;
b = (double)(lbl_ShowValue.Location.Y + lbl_ShowValue.Height / 2) / pictureBox1.Image.Size.Height; //比例
c = (double)lbl_ShowValue.Width / pictureBox1.Image.Size.Width;
d = (double)lbl_ShowValue.Height / pictureBox1.Image.Size.Height; IsLoading = false; } Size Isize0, Isize1;
double d0, d1;
private void UiBeng_Resize(object sender, EventArgs e)
{
if (IsLoading == true)
return; Isize0 = pictureBox1.Image.Size; //picbox的宽高比例
d0 = (double)Isize0.Width / Isize0.Height;
//usercontrol的宽高比例
d1 = (double)this.Width / this.Height;
if (d1 >= d0)
{
Isize1 = new Size((int)(this.Height * d0), (int)(this.Height));
}
else
Isize1 = new Size((int)(this.Width), (int)((double)this.Width / d0));
lbl_ShowValue.Size = new Size((int)(Isize1.Width * c), (int)(Isize1.Height * d));
lbl_ShowValue.Location = new Point((int)(Isize1.Width * a - lbl_ShowValue.Width / 2 + (this.Width - Isize1.Width) / 2), (int)(Isize1.Height * b - lbl_ShowValue.Height / 2 + (this.Height - Isize1.Height) / 2));
}
}
}
我是根据上面的vb.net转录成C#的,但是发现执行效果不一致,不明白有什么不一样的,请教
.Location = New Point(Isize1.Width * a - .Width / 2 + (sender.width - Isize1.Width) / 2, Isize1.Height * b - .Height / 2 + (sender.Height - Isize1.Height) / 2)
sender 不等于this