简体   繁体   English

C# Onpaint 不触发

[英]C# Onpaint is not firing

I have this code:我有这个代码:

    private void HandleGUI()
    {
        if (_currentForm == null)
        {
            navigationSideBar1.Visible = false;
            pnlToolbar.Visible = false;

            return;
        }

        if (_currentForm.ShowNavigationBar)
        {
            HandleNavigationButton(_currentForm);
        }

        btnSave.Visible = _currentForm.ShowSaveButton;
        btnClose.Visible = _currentForm.ShowCloseButton;
        btnSave.Paint += new PaintEventHandler(btnSave_Paint);
        navigationSideBar1.Visible = _currentForm.ShowNavigationBar;
        pnlToolbar.Visible = _currentForm.ShowToolBar;

        btnSave.Refresh();
        btnSave.Invalidate();
    }

I am registered on the onpaint event of the save button (btnSave), but this event is not fired, even when I call Refresh or Invalidate.我在保存按钮 (btnSave) 的 onpaint 事件上注册,但即使我调用 Refresh 或 Invalidate,也不会触发此事件。 How is this possible?这怎么可能?

EDIT: This is how the saave button class looks like:编辑:这是保存按钮类的样子:

public class SaveButton : ButtonX
{
    public SaveButton()
    {
        this.Image = Properties.Resources.Save;
        this.Text = "Opslaan";
        this.Size = new Size(108, 39);

    }
}

Try adding a regular DevComponent button (ie not a subclass of it) to a test form and see if it ever fires its Paint event.尝试向测试表单添加一个常规 DevComponent 按钮(即不是它的子类),看看它是否会触发它的 Paint 事件。 They may have exposed a Paint event (so that their interface matches that of a regular button) but not actually implemented it.他们可能已经公开了一个 Paint 事件(以便他们的界面与常规按钮的界面相匹配)但实际上并没有实现它。

设置userpainttrue将触发onpaint事件

this.SetStyle(ControlStyles.UserPaint, true);

From MSDN :MSDN

Calling the Invalidate method does not force a synchronous paint;调用 Invalidate 方法不会强制同步绘制; to force a synchronous paint, call the Update method after calling the Invalidate method.要强制同步绘制,请在调用 Invalidate 方法后调用 Update 方法。

So, you need an Update call.所以,你需要一个 Update 调用。 Now, Refresh is just Invalidate w/ update children + Update, so theoretically you're taken care of.现在,Refresh 只是 Invalidate w/update children + Update,所以理论上你已经解决了。 All I can think is that Windows doesn't call Paint unless it really needs to, ie when the form is shown on the UI or written to a graphics device ("screenshot" of an invisible window).我所能想到的是,除非确实需要,否则 Windows 不会调用 Paint,即当表单显示在 UI 上或写入图形设备时(不可见窗口的“屏幕截图”)。 Are either of these the case?有这两种情况吗?

It looks like you're not calling the base class constructor in the constructor of your SaveButton component.看起来您没有在SaveButton组件的构造函数中调用基类构造函数。

public class SaveButton : ButtonX
{
    public SaveButton() : base()
...

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

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