当前位置:网站首页>C WinForm panel custom picture and text

C WinForm panel custom picture and text

2022-06-25 07:49:00 Kingston

Code download address :https://download.csdn.net/download/wojiuguowei/85761936
Reference address :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、 Handwriting is like an event method 
    private void txt_username_GotFocus(object sender, EventArgs e)
    {
    
        // Get focus on the code to execute 
    }

    private void txt_username_LostFocus(object sender, EventArgs e)
    {
    
        // Lose focus on the code to execute 
    }


        private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size)
    {
    
        // Get picture width 
        int sourceWidth = imgToResize.Width;
        // Get the height of the picture 
        int sourceHeight = imgToResize.Height;

        float nPercent = 0;
        float nPercentW = 0;
        float nPercentH = 0;
        // Calculate the scaling of the width 
        nPercentW = ((float)size.Width / (float)sourceWidth);
        // Calculate the scaling of the height 
        nPercentH = ((float)size.Height / (float)sourceHeight);

        if (nPercentH < nPercentW)
            nPercent = nPercentH;
        else
            nPercent = nPercentW;
        // The desired width 
        int destWidth = (int)(sourceWidth * nPercent);
        // The height of expectations 
        int destHeight = (int)(sourceHeight * nPercent);

        Bitmap b = new Bitmap(destWidth, destHeight);
        Graphics g = Graphics.FromImage((System.Drawing.Image)b);
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        // The plot 
        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(" Song style ", 10);
        g.DrawString(" Song style ", font, Brushes.Red, position);// The first parameter is   String displayed on the form , The second is the font of this string , The third is color , The fourth is the starting position of the output 
        //position.Y += font.Height + 5; // After each output of a font , Output position moves down 
        font.Dispose();
        //this.AutoScrollMinSize = new Size(350, position.Y + 50);// Scroll box 

        //
        Rectangle ObjRct = new Rectangle();
        Image ObjImg;
        ObjImg = ((Bitmap)Image.FromFile(@"D:\work\develop\ Developing documents \canopen\ Test code \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);
    }
}
原网站

版权声明
本文为[Kingston]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250555577769.html