onload()和onpaint()事件的执行顺序

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 
 8 namespace onload_demo_1
 9 {
10     class Program:Form
11     {
12         static void Main(string[] args)
13         {
14             Application.Run(new Program());
15         }
16         public Program()
17         {
18             Text = "Onload Demo";
19         }
20 
21         protected override void OnPaint(PaintEventArgs e)
22         {
23             Graphics grfx = e.Graphics;
24             grfx.DrawString("this is a demo", Font, new SolidBrush(Color.Red), 0, 0);
25         }
26 
27         protected override void OnLoad(EventArgs e)
28         {
29             base.OnLoad(e);
30             MessageBox.Show("click okay and the Form will appear");
31         }
32 
33         protected override void OnShown(EventArgs e)
34         {
35             base.OnShown(e);
36             MessageBox.Show("Are you ready ?");
37         }
38     }
39 }
复制代码

从以上示例可以看出,先onload 事件,再执行onpaint事件;

可以参考如下网址:

https://www.codeproject.com/questions/1114117/override-the-onload-function-of-a-form

Yes, you can override the Form OnLoad event:

C#
protected override void OnLoad(EventArgs e)
{
   base.OnLoad(e);
   MessageBox.Show("click okay and the Form will appear");
}

But, do you really want the user to see nothing but a MessageBox when they start the app ? If you want to first show the Form, and then put up a MessageBox, try this:

C#
protected override void OnShown(EventArgs e)
{
    base.OnShown(e);
    MessageBox.Show("Are you ready ?");
}

But, I wonder why it is ... what special circumstances require ... you ... need to do this, because:

Just double-clicking on the Form design-surface in design-mode will cause the automatic generation of a Form Load EventHandler:

C#
private void Form1_Load(object sender, EventArgs e)
{
    // do whatever ...
}

which is wired-up to the Form ... you can look in the Designer.cs file and see how the connection is made:

C#
this.Load += new System.EventHandler(this.Form1_Load);

Or, you can define instantiate (create) your own EventHandler and wire it up to the Form:

C#
public Form1()
{
    InitializeComponent();
    this.Load += MyOnLoad;
}

private void MYOnLoad(object sender, EventArgs e)
{
    // do whatever
}

Or, you can define an EventHandler for the 'Shown event, as shown in the second code example here in order to the Form appear before the MessageBox appears. 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2022年8月3日更新

Form在show的时候没有激发onshown事件处理

1. Form.OnShown是在Form.OnLoad方法中通过Form.BeginInvoke调用到的。在10000次Focus之后,OnShown就调不到了。

2. 通过跟踪当前Application的Message,发现9999次Focus会导致9999个自定义事件(0xc03b),然后会收到一个0xc337。但是10000次Focus之后,最后的0xc337就消失了。如果这时再发一个0xc03b,会收到两个0xc337。证明超过10000之后,0xc337的消息处理就出了问题。

3. 怀疑是BeginInvoke的问题,所以在10000Focus之后,没有调用Form2.Show,而是通过BeginInvoke调用自己的一个方法,果然会失败。但是,如果在10000Focus之后,调用Application.DoEvents,则不会出错。

4. 通过反编译代码,得知BeginInvoke是通过PostMessage发送一个自定义事件(这里即0xc337,Control.threadCallbackMessage),然后再WndProc中处理这个Message来实现的。

posted @   chenlight  阅读(162)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示