{"id":733,"date":"2009-11-13T20:53:00","date_gmt":"2009-11-13T12:53:00","guid":{"rendered":""},"modified":"2013-11-17T16:58:05","modified_gmt":"2013-11-17T08:58:05","slug":"settimer%e5%87%bd%e6%95%b0%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/733.html","title":{"rendered":"SetTimer\u51fd\u6570\u7528\u6cd5"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nprogram Project2;\r\n\r\n{$APPTYPE CONSOLE}\r\n\r\nuses\r\n  SysUtils,windows;\r\n\r\nvar msg:TMsg;\r\n\r\nprocedure OK;\r\nbegin\r\n  Writeln('fuck');\r\nend;\r\n\r\nbegin\r\n  settimer(0,10,1000,@OK);   \/\/0\u4e3a\u53e5\u67c4\uff0c10\u4e3aID \uff0c1000\u4e3a\u6beb\u79d2\uff0c@OK\u4e3a\u51fd\u6570\u5730\u5740\r\n  while GetMessage(msg, 0, 0, 0) do DispatchMessage(msg);\r\nend.\r\n<\/pre>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\r\n\/\/\u521b\u5efa\u5b9a\u65f6\u5668\u51fd\u6570\u7684\u58f0\u660e:\r\nSetTimer(\r\n  hWnd: HWND;               {\u4e0e\u5b9a\u65f6\u5668\u76f8\u5173\u8054\u7684\u7a97\u53e3\u53e5\u67c4}\r\n  nIDEvent: UINT;           {\u6307\u5b9a\u4e00\u4e2a\u975e 0 \u7684\u5b9a\u65f6\u5668\u6807\u8bc6\u7b26}\r\n  uElapse: UINT;            {\u6307\u5b9a\u95f4\u9694\u65f6\u95f4, \u5355\u4f4d\u662f\u6beb\u79d2}\r\n  lpTimerFunc: TFNTimerProc {\u6bcf\u5230\u65f6\u95f4\u540e, \u8981\u8c03\u7528\u7684\u51fd\u6570\u7684\u6307\u9488}\r\n): UINT;                    {\u8fd4\u56de\u5b9a\u65f6\u5668\u6807\u8bc6\u7b26; \u5931\u8d25\u8fd4\u56de 0}\r\n\/\/\u79fb\u9664\u5b9a\u65f6\u5668\u51fd\u6570\u7684\u58f0\u660e:\r\nKillTimer(\r\n  hWnd: HWND;    {\u4e0e\u5b9a\u65f6\u5668\u76f8\u5173\u8054\u7684\u7a97\u53e3\u53e5\u67c4}\r\n  uIDEvent: UINT {\u5b9a\u65f6\u5668\u6807\u8bc6\u7b26}\r\n): BOOL;\r\n\/\/\u5904\u7406 WM_TIMER \u6d88\u606f\u7684\u56de\u8c03\u51fd\u6570\u7684\u683c\u5f0f:\r\nTimerProc(\r\n  hWnd: HWND;    {\u4e0e\u5b9a\u65f6\u5668\u76f8\u5173\u8054\u7684\u7a97\u53e3\u53e5\u67c4}\r\n  uMsg: UINT;    {WM_TIMER \u6d88\u606f}\r\n  idEvent: UINT; {\u5b9a\u65f6\u5668\u7684\u6807\u8bc6\u7b26}\r\n  Time: DWORD    {\u4ee5\u4e16\u754c\u65f6\u95f4\u516c\u7ea6\u683c\u5f0f(UTC)\u6307\u5b9a\u7684\u7cfb\u7edf\u65f6\u95f4}\r\n);               {\u8fd9\u662f\u4e2a\u8fc7\u7a0b, \u65e0\u8fd4\u56de\u503c}\r\n\r\n<\/pre>\n<p>\u5e94\u8be5\u5148\u7406\u89e3\u4e00\u4e0b\u518d\u4e3e\u4f8b:<\/p>\n<p>SetTimer \u7684\u53c2\u65701: hWnd, \u4e00\u822c\u6307\u5b9a Self.Handle \u5c31\u53ef\u4ee5\u4e86;<\/p>\n<p>SetTimer \u7684\u53c2\u65702: nIDEvent \u662f\u67d0\u4e2a\u5b9a\u65f6\u5668\u7684\u6807\u8bc6\u7b26, \u8bf4\u660e\u53ef\u4ee5\u521b\u5efa\u82e5\u5e72\u4e2a\u5b9a\u65f6\u5668, \u968f\u4fbf\u7ed9\u4e2a\u6b63\u6574\u6570\u5373\u53ef;<\/p>\n<p>SetTimer \u7684\u53c2\u65703: uElapse, \u8fd9\u76f8\u5f53\u4e8e VCL \u4e2d Timer \u7c7b\u7684 Interval \u5c5e\u6027;<\/p>\n<p>\u770b\u6765\u53c2\u6570 1-3 \u8fd8\u662f\u975e\u5e38\u7b80\u5355, \u4e0d\u597d\u7406\u89e3\u7684\u5c31\u662f\u53c2\u65704: lpTimerFunc, \u8fd9\u662f\u4e00\u4e2a\u8fc7\u7a0b\u7684\u5730\u5740.<\/p>\n<p>\u4efb\u610f\u4e00\u4e2a\u8fc7\u7a0b\u7684\u5730\u5740\u5417? \u5f53\u7136\u4e0d\u662f; \u8fd9\u4e2a\u8fc7\u7a0b\u7684\u53c2\u6570\u7ed3\u6784\u662f Windows \u7cfb\u7edf\u89c4\u5b9a\u7684, \u6211\u4eec\u5fc5\u987b\u6309\u89c4\u5b9a\u53bb\u5b9a\u4e49\u8fd9\u4e2a\u8fc7\u7a0b; \u5373\u4f7f\u4f60\u7528\u4e0d\u5230\u5176\u4e2d\u7684\u53c2\u6570\u4e5f\u5fc5\u987b\u5982\u6b64.<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\r\n\/\/\u4e3e\u4f8b:\r\nvar\r\n  i: Integer;\r\n\/\/\u5148\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\r\nprocedure MyTimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; Time: DWORD);\r\nbegin\r\n  Form1.Text := IntToStr(i);\r\n  Inc(i);\r\nend;\r\n\/\/\u521b\u5efa\u5b9a\u65f6\u5668\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nbegin\r\n  SetTimer(Handle, 1, 10, @MyTimerProc); {\u6bcf 1\/100 \u79d2\u8c03\u7528\u4e00\u6b21 MyTimerProc}\r\nend;\r\n\/\/\u5220\u9664\u5b9a\u65f6\u5668\r\nprocedure TForm1.Button2Click(Sender: TObject);\r\nbegin\r\n  KillTimer(Handle, 1); {\u521b\u5efa\u65f6\u6307\u5b9a\u7684\u5b9a\u65f6\u5668\u6807\u8bc6\u662f 1, \u8fd9\u91cc\u5fc5\u987b\u8981\u4e00\u81f4}\r\nend;\r\n\r\n\/\/\u5982\u679c\u56de\u8c03\u8fc7\u7a0b\u662f\u7a97\u4f53\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5, \u9700\u8981\u8fd9\u6837:\r\nvar\r\n  i: Integer;\r\n\/\/\u5148\u5b9a\u4e49\u56de\u8c03\u51fd\u6570\r\nprocedure TForm1.MyTimerProc(hWnd: HWND; uMsg: UINT; idEvent: UINT; Time: DWORD);\r\nbegin\r\n  Text := IntToStr(i);\r\n  Inc(i);\r\nend;\r\n\/\/\u521b\u5efa\u5b9a\u65f6\u5668\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nbegin\r\n  SetTimer(Handle, 1, 10, @TForm1.MyTimerProc); {\u6bcf 1\/100 \u79d2\u8c03\u7528\u4e00\u6b21 MyTimerProc}\r\nend;\r\n\/\/\u5220\u9664\u5b9a\u65f6\u5668\r\nprocedure TForm1.Button2Click(Sender: TObject);\r\nbegin\r\n  KillTimer(Handle, 1); {\u521b\u5efa\u65f6\u6307\u5b9a\u7684\u5b9a\u65f6\u5668\u6807\u8bc6\u662f 1, \u8fd9\u91cc\u5fc5\u987b\u8981\u4e00\u81f4}\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>program Project2; {$APPTYPE CONSOLE} uses SysUtils,wind [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-733","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/733\/revisions"}],"predecessor-version":[{"id":4713,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/733\/revisions\/4713"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}