简体   繁体   English

如何自动检测我正在使用的控制油漆事件?

[英]How can I automatically detect what control paint event I'm using?

I have for example Form1 paint event: 我有例如Form1 paint事件:

private void Form1_Paint(object sender, PaintEventArgs e)
{
     TextDrawer draw = new TextDrawer(e.Graphics,this,8.25);
}

In the new class TextDrawer I need to enter the control name in this case its form1 so I type: 在新类TextDrawer我需要输入控件名称,在这种情况下是form1,所以我输入:

This is the new class: 这是新课程:

class TextDrawer
{
        private readonly Graphics g;
        private readonly Control c ;
        private readonly double font_size;

        public TextDrawer(Graphics g,Control c,
                          double font_size)
        {
            this.g = g;
            this.c = c;
            this.font_size = font_size;
        }

        public void DrawText(string text,Color pen_color,Color brushes_color, Point point1, Point point2, Point point3)
        {
            c.Font = new Font(c.Font.FontFamily.Name, (float)font_size);
            SolidBrush brush = new SolidBrush(brushes_color);
            using (Pen pen = new Pen(pen_color, 10f))
            {
                Point pt1 = point1;
                Point pt2 = point2;
                g.DrawLine(pen, point1, point2);
            }

            g.DrawString(text,
                    c.Font, brush, point3);
        }
    }

I want to make somehow that once i type inside a paint event of any control is its form1 pictureBox1 label any control that have a paint event once i make a new instance for the class for example: 我想以某种方式做出一旦我在任何控件的paint事件中键入它的form1 pictureBox1标签任何具有paint事件的控件,一旦我为该类创建一个新实例,例如:

TextDrawer draw = new TextDrawer(e.Graphics,8.25);

And the new class will detect/find automatic the control name so the user wont need to type in: this or pictureBox1 or label1... 并且新类将检测/查找自动控件名称,以便用户无需键入:this或pictureBox1或label1 ...

Is there any way to do it ? 有什么办法吗?

For example in the TextChanged event of the TextBox you can pass the sender argument as the Control. 例如,在TextBox的TextChanged事件中,您可以将sender参数作为Control传递。

private void YourTextBoxOne_TextChanged(object sender, EventArgs e)
{
    TextBoxTextChanged((TextBox)sender);
}

private void YourTextBoxTwo_TextChanged(object sender, EventArgs e)
{
    TextBoxTextChanged((TextBox)sender);
}

private void TextBoxTextChanged(TextBox tb)
{
    var draw = new TextDrawer(tb, 8.25);
    //Do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM