当前位置:网站首页>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);
}
}
边栏推荐
- WinForm实现窗口始终在顶层
- php入门基础记录
- 57. 插入区间
- Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
- Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
- Different paths ii[dynamic planning improvement for DFS]
- 微信小程序入门记录
- Estimation of dense forest volume based on LIDAR point cloud with few ground points
- Vscode official configuration synchronization scheme
- Evolution of Alibaba e-commerce architecture
猜你喜欢

Introduction to Sichuan Tuwei ca-is3082wx isolated rs-485/rs-422 transceiver

Function template_ Class template

【深度学习 轻量型backbone】2022 EdgeViTs CVPR

海思3559 sample解析:vio

Sichuan earth microelectronics ca-is1200 isolated operational amplifier for current detection

函数模板_类模板

ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性

Modular programming of digital light intensity sensor module gy-30 (main chip bh1750fvi) controlled by single chip microcomputer (under continuous updating)

一“石”二“鸟”,PCA有效改善机载LiDAR林下地面点部分缺失的困局

Evolution of Alibaba e-commerce architecture
随机推荐
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
对链表进行插入排序[dummy统一操作+断链核心--被动节点]
This year, I graduated
【Qt】快捷键
ELK + filebeat日志解析、日志入库优化 、logstash过滤器配置属性
Hisilicon 3559 sample parsing: Vio
C#入门教程
【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection
【QT】Qt 5 的程序:打印文档
基于激光雷达的林业调查常用术语及含义锦集
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘
1464. 数组中两元素的最大乘积
Pit encountered by pytorch: why can't l1loss decrease during model training?
差点被这波Handler 面试连环炮带走~
如何用svn新建属于自己的分支
Mysql面试-执行sql响应比较慢,排查思路。
Pytorch遇到的坑:为什么模型训练时,L1loss损失无法下降?
Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]
Can I open a stock account with a compass? Is it safe?