当前位置:网站首页>c# winform panel自定义图片和文字
c# winform panel自定义图片和文字
2022-06-25 06:41:00 【金士顿】
代码下载地址:https://download.csdn.net/download/wojiuguowei/85761936
参考地址:https://stackoverflow.com/questions/10386783/enter-event-not-firing-in-panel-inside-a-usercontrol?answertab=trending#tab-top
public class PanelEx : PictureBox
{
public PanelEx()
{
this.BackColor = Color.Lavender;
pic.BackColor = Color.Blue;
this.GotFocus += new EventHandler(txt_username_GotFocus);
this.LostFocus += new EventHandler(txt_username_LostFocus);
this.Enter += new System.EventHandler(this.CustomEnter);
this.Leave += new System.EventHandler(this.CustomLeave);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.CustomMouseClick);
}
private void CustomMouseClick(object sender, MouseEventArgs e)
{
this.Focus();
MessageBox.Show(this.Name + "GetFocus");
this.BackColor = Color.Blue;
}
private void CustomLeave(object sender, EventArgs e)
{
MessageBox.Show(this.Name + "LostFocus");
this.BackColor = Color.Lavender;
}
private void CustomEnter(object sender, EventArgs e)
{
}
PictureBox pic = new PictureBox();
//2、手写像事件方法一样的方法
private void txt_username_GotFocus(object sender, EventArgs e)
{
//获得焦点要执行的代码
}
private void txt_username_LostFocus(object sender, EventArgs e)
{
//失去焦点要执行的代码
}
private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
{
//获取图片宽度
int sourceWidth = imgToResize.Width;
//获取图片高度
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
//计算宽度的缩放比例
nPercentW = ((float)size.Width / (float)sourceWidth);
//计算高度的缩放比例
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
//期望的宽度
int destWidth = (int)(sourceWidth * nPercent);
//期望的高度
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((System.Drawing.Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
//绘制图像
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (System.Drawing.Image)b;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
//g.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
Point position = new Point(0, 30);
Font font = new Font("宋体", 10);
g.DrawString("宋体", font, Brushes.Red, position);//第一个参数是 在窗体显示的字符串,第二个是此字符串的字体,第三个是颜色,第四个是输出的起始位置
//position.Y += font.Height + 5; //每输出一个字体后,输出位置向下移
font.Dispose();
//this.AutoScrollMinSize = new Size(350, position.Y + 50);//滚动框
//
Rectangle ObjRct = new Rectangle();
Image ObjImg;
ObjImg = ((Bitmap)Image.FromFile(@"D:\work\develop\开发文档\canopen\测试代码\flowsharp\Plugins\PluginExample\Resources\DefaultImage.png"));
//ObjImg = resizeImage(ObjImg, new Size(this.Width - 20, this.Height - 20));
ObjRct.X = 20;
ObjRct.Y = 20;
ObjRct.Height = this.Height - 40;
ObjRct.Width = this.Width - 40;
g.DrawImage(ObjImg, ObjRct);
}
}
边栏推荐
- realsense d455 semantic_slam实现语义八叉树建图
- CPDA|数据分析师成长之路如何起步?
- Understand the reasons for impedance matching of PCB circuit board 2021-10-07
- Path planner based on time potential function in dynamic environment
- STL教程4-输入输出流和对象序列化
- Shell tips (134) simple keyboard input recorder
- VectorDraw Developer Framework 10.10
- [batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
- C reads XML on the web
- Manufacturing process of PCB 2021-10-11
猜你喜欢
剑指 Offer II 027. 回文链表
Home environment monitoring system design (PC version) (mobile app version to be determined)
Full range of isolator chips with integrated isolated power supply
STL tutorial 4- input / output stream and object serialization
JMeter introduction practice ----- use of global variables and local variables
test
基于地面点稀少的LiDAR点云的茂密森林蓄积量估算
ts环境搭建
函数模板_类模板
Function template_ Class template
随机推荐
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
搞清信息化是什么,让企业转型升级走上正确的道路
China Mobile MCU product information
Leetcode daily question - 515 Find the maximum value in each tree row
php入门基础记录
One "stone" and two "birds", PCA can effectively improve the dilemma of missing some ground points under the airborne lidar forest
指南针可以开股票账户吗?安全吗?
This year, I graduated
Home environment monitoring system design (PC version) (mobile app version to be determined)
OpenMP入门
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
OAuth 2.0一键登录那些事
Estimation of dense forest volume based on LIDAR point cloud with few ground points
57. 插入区间
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905
Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
Five causes of PCB board deformation and six solutions 2021-10-08
RTKLIB-b33版本中GALILEO广播星历存储问题
El input to add words to the tail
Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection