c# winform panel自定义图片和文字

代码下载地址: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);
    }
}

你可能感兴趣的:(c#,#,绘图,GDI+,自定义图片,自定义文字)