开发者

Which event is launched right after Control is fully loaded?

I've got user control that overrides WebBrowser control. There's a method whi开发者_JAVA百科ch takes an area of a BackgroundImage from its Parent Form and makes it its background in html code. It works like that:

  1. Makes itself invisible.
  2. Captures Parent form.
  3. Makes itself visible.
  4. Takes the specific area of captured image and sets it as its background.
  5. Refreshes itself.

I want that method to be fired right after the control is fully loaded. I can't do it in constructor, because some important objects and parent's properties which I need to use are still null. I put it in OnVisibleChanged event and set bool variable to false, so it runs only once, but it fires every time I rebuild my project (even without running - it creates bitmap that I use as background in html code, but different directory because I use Directory.CurrentDirectory() method). Tried also fire it with OnLoad event, but it makes the same problem as OnVisibleChanged. Is there more appropriate event for that than OnVisibleChanged?

PS. I was always wondering if every event has to fire event of it's base class. Is it necessary to do it? I don't see any diffrence at all, without it everything works great. If it's better to leave it there, should it be on the beginning or on the end of the event method?


There's no concept of 'fully loaded', creating a control is an atomic operation in Winforms and signaled by OnHandleCreated(). What you are looking for here is having the control fully painted. Painting is a low priority task in Windows, performed only when nothing else needs to be done.

Get that notification by overriding WndProc() and catching WM_PAINT:

bool fullyPainted = false;

protected override void WndProc(ref Message m) {
    base.WndProc(ref m);
    if (m.Msg == 15 && !fullyPainted) {
        fullyPainted = true;
        // etc...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜