C# - OnPaintBackground事件,未在父级上调用继承的类 [英] C# - OnPaintBackground event with inherited class not being called on parent

查看:184
本文介绍了C# - OnPaintBackground事件,未在父级上调用继承的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个继承自B类并继承自C类的A类,C继承自UserControl。



A类有一个D子元素(RichTextBox)。



所有Paint事件都在D类中处理。当我将D类中的CreateParams覆盖为透明时,它将事件抛给父(A),如果我直接从UserControl继承A,但是当我继承自B类时,它从未被调用。


< p style ="padding-right:0px; font-size:15px; clear:both;颜色:#222222; font-family:'Helvetica Neue',Helvetica,Arial,sans-serif; line-height:21.6667px">
我已经搜索过,找不到任何解决方案。



这是我的D代码:



 

public 部分 RichTextBG RichTextBox
{
私人 TextView tParent ;

public RichTextBG TextView p
{
这个 tParent = p ;

InitializeComponent ();

这个 TextChanged + = EventHandler textView_TextChanged );
这个 ScrollBars = RichTextBoxScrollBars ;
这个 SelectionFont = 字体 this 字体 FontStyle Bold );
这个 SelectionColor = 颜色 白色 ;

这个 尺寸 = TextRenderer MeasureText "" this SelectionFont );
}

受保护 覆盖 CreateParams CreateParams
{
get
{
CreateParams parms = base CreateParams ;
parms
ExStyle | = 0x20 ; //启用WS_EX_TRANSPARENT
返回 parms ;
}
}

私人 无效 textView_TextChanged object sender EventArgs e
{
尺寸 size = TextRenderer MeasureText this 文字 this SelectionFont );

这个 宽度 = size 宽度 + 5 ;
这个 高度 = size 高度 + 15 ;

这个 宽度 = size 宽度 + 5 ;
这个 高度 = size 高度 + 15 ;

这个 ScrollToCaret ();
// this.Refresh();
}
}



这是我的父代码(A):



     public    部分       TextView   
AnimatedObjectView
{
私人 图像 backImage ;

public TextView int x int y
base false ObjectView OBJECT_TYPE TEXT_VIEW
{
InitializeComponent ();

SetStyle ControlStyles UserPaint true );
SetStyle ControlStyles ResizeRedraw true );
SetStyle ControlStyles AllPaintingInWmPaint true );
SetStyle ControlStyles OptimizedDoubleBuffer true );

这个 位置 = x y );
RichTextBG tx = RichTextBG this );
这个 尺寸 = tx 尺寸 ;
这个 控件 添加 tx );
这个 BorderStyle = 系统 Windows 表格 BorderStyle FixedSingle ;
}

受保护 覆盖 无效 OnMouseDown MouseEventArgs e
{
base OnMouseDown e );
//在这里派生类东西
}

受保护 覆盖 无效 OnMouseUp MouseEventArgs e
{
PranchetaApp Prancheta Board 控制 PlayerController OnMouseUp this e );
//在这里派生类东西
}

受保护 覆盖 无效 OnPaintBackground PaintEventArgs pevent
{
GDIWrapper RECT客户端 ;
backImage
= getBackGroundImage new Rectangle this Location X 这个 位置 Y 这个 尺寸 宽度 这个 尺寸 高度 ));
图形 g = pevent 图形 ;
g
DrawImage backImage < span class ="lit"style ="margin:0px; padding:0px; border:0px; color:#800000"> 0 0 new Rectangle 0 0 backImage 宽度 backImage 高度 ), GraphicsUnit 像素 );
}



解决方案

您好IvanFCFiho,


你的意思是当A i 从B继承时,OnPaintBackground事件无法提升吗?


最好的问候,

李旺



I have a Class A that inherited from class B and inherited from class C, and C inherited from UserControl.

Class A has a D child element (RichTextBox).

All Paint events are handled in D class. When i override CreateParams in D class to be transparent, it throw the event to parent (A) just if i inherited A directly from UserControl, but when i inherited from Class B, it's never called.

I've already searched and couldn't find any solution.

Here is my D code:

public partial class RichTextBG : RichTextBox { private TextView tParent; public RichTextBG(TextView p) { this.tParent = p; InitializeComponent(); this.TextChanged += new EventHandler(textView_TextChanged); this.ScrollBars = RichTextBoxScrollBars.None; this.SelectionFont = new Font(this.Font, FontStyle.Bold); this.SelectionColor = Color.White; this.Size = TextRenderer.MeasureText(" ", this.SelectionFont); } protected override CreateParams CreateParams { get { CreateParams parms = base.CreateParams; parms.ExStyle |= 0x20; // Turn on WS_EX_TRANSPARENT return parms; } } private void textView_TextChanged(object sender, EventArgs e) { Size size = TextRenderer.MeasureText(this.Text, this.SelectionFont); this.Width = size.Width + 5; this.Height = size.Height + 15; this.Parent.Width = size.Width + 5; this.Parent.Height = size.Height + 15; this.ScrollToCaret(); //this.Refresh(); } }

Here is my parent code (A):

 public partial class TextView 
    : AnimatedObjectView
{
    private Image backImage;

    public TextView(int x, int y) 
        : base(false, ObjectView.OBJECT_TYPE.TEXT_VIEW)
    {
        InitializeComponent();

        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.ResizeRedraw, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

        this.Location = new Point(x, y);
        RichTextBG tx = new RichTextBG(this);
        this.Size = tx.Size;
        this.Controls.Add(tx);
        this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        //Do derived class stuff here
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        PranchetaApp.Prancheta.Board.Control.PlayerController.OnMouseUp(this, e);
        //Do derived class stuff here
    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        GDIWrapper.RECT client;
        backImage = getBackGroundImage(new Rectangle(this.Location.X, this.Location.Y, this.Size.Width, this.Size.Height));
        Graphics g = pevent.Graphics;
        g.DrawImage(backImage, 0, 0, new Rectangle(0, 0, backImage.Width, backImage.Height), GraphicsUnit.Pixel);
    }

解决方案

Hi IvanFCFiho,

Do you mean OnPaintBackground event can't raise when A inherited from B?

Best Regards,
Li Wang


这篇关于C# - OnPaintBackground事件,未在父级上调用继承的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆