﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-doneykoo [Ouditian]-随笔分类-C/C++</title><link>http://www.blogjava.net/doneykoo/category/37896.html</link><description>DKzone- Ouditian Technology</description><language>zh-cn</language><lastBuildDate>Fri, 16 Oct 2009 10:35:45 GMT</lastBuildDate><pubDate>Fri, 16 Oct 2009 10:35:45 GMT</pubDate><ttl>60</ttl><item><title>Where is Form's Loaded event?   [FW]</title><link>http://www.blogjava.net/doneykoo/archive/2009/10/16/298505.html</link><dc:creator>DoNeY</dc:creator><author>DoNeY</author><pubDate>Fri, 16 Oct 2009 01:48:00 GMT</pubDate><guid>http://www.blogjava.net/doneykoo/archive/2009/10/16/298505.html</guid><wfw:comment>http://www.blogjava.net/doneykoo/comments/298505.html</wfw:comment><comments>http://www.blogjava.net/doneykoo/archive/2009/10/16/298505.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/doneykoo/comments/commentRss/298505.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/doneykoo/services/trackbacks/298505.html</trackback:ping><description><![CDATA[<h2>
Where is Form's Loaded event?
</h2>
<p>[FW]<em><a target="_blank" href="http://weblogs.asp.net/kennykerr/archive/2004/11/26/where-is-form-s-loaded-event.aspx">http://weblogs.asp.net/kennykerr/archive/2004/11/26/where-is-form-s-loaded-event.aspx</a></em><br />
</p>
<p>Wow, it&#8217;s been a while since I last posted something here. We&#8217;ve recently <a href="http://blogs.kennyandkarin.com/kenny/category/33.aspx">moved to BC</a> so I&#8217;ve been pretty distracted. I hope to be able to post more regularly in the coming weeks.</p>
<p>This is a bit off-topic for me but here goes. </p>
<p>Recently I needed to run some code right after a form is displayed for the first time. The <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsFormClassLoadTopic.asp">Form.Load</a>
event is handy for performing various tasks when a form is loading but
before it is displayed for the first time. Unfortunately there is no
corresponding Form.Loaded event to notify the application that the form
has actually loaded and is visible.</p>
<p>Fortunately it&#8217;s quite easy to pull it off without resorting to the <a href="http://msdn.microsoft.com/library/en-us/dllproc/base/waitforinputidle.asp">WaitForInputIdle</a> function. All you need to do is override Form&#8217;s <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsFormClassOnLoadTopic.asp">OnLoad</a> method and add an event handler for the <a href="http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFormsApplicationClassIdleTopic.asp">Application.Idle</a>
event. Since we only want to be notified a single time that the form is
loaded, we immediately remove the delegate in the event handler. You
can of course register the event handler earlier in the form or
application&#8217;s lifetime but I prefer to keep delegates registered for as
short a period as possible. </p>
<p>Here&#8217;s a simple example:</p>
<br />
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br />
<br />
Code highlighting produced by Actipro CodeHighlighter (freeware)<br />
http://www.CodeHighlighter.com/<br />
<br />
--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #0000ff;">protected</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">override</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;OnLoad(EventArgs&nbsp;args)<br />
</span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">{<br />
</span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnLoad(args);<br />
</span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;Application.Idle&nbsp;</span><span style="color: #000000;">+=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;EventHandler(OnLoaded);<br />
</span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">}<br />
</span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #0000ff;">private</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;OnLoaded(</span><span style="color: #0000ff;">object</span><span style="color: #000000;">&nbsp;sender,<br />
</span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EventArgs&nbsp;args)<br />
</span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">{<br />
</span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;Application.Idle&nbsp;</span><span style="color: #000000;">-=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">new</span><span style="color: #000000;">&nbsp;EventHandler(OnLoaded);<br />
</span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;"><br />
</span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;TODO:&nbsp;add&nbsp;relevant&nbsp;code&nbsp;here</span><span style="color: #008000;"><br />
</span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}<br />
</span><span style="color: #008080;">15</span>&nbsp;</div>
<p>This
might be useful, for example, if you need to prompt the user (the
horror!) for something but would prefer the dialog box to appear in the
context of your application&#8217;s main window.</p>
<hr />
<p>&#169; 2004 Kenny Kerr</p>
<img src ="http://www.blogjava.net/doneykoo/aggbug/298505.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/doneykoo/" target="_blank">DoNeY</a> 2009-10-16 09:48 <a href="http://www.blogjava.net/doneykoo/archive/2009/10/16/298505.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>