{"id":462,"date":"2009-06-15T14:52:00","date_gmt":"2009-06-15T06:52:00","guid":{"rendered":""},"modified":"2013-11-17T17:31:29","modified_gmt":"2013-11-17T09:31:29","slug":"%e5%87%a0%e4%b8%aa%e6%9c%89%e7%94%a8%e7%9a%84delphi%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/462.html","title":{"rendered":"\u51e0\u4e2a\u6709\u7528\u7684Delphi\u51fd\u6570"},"content":{"rendered":"<p>1.\u9632\u6b62\u5237\u65b0\u65f6\u95ea\u70c1\u7684\u7ec8\u6781\u89e3\u51b3\u529e\u6cd5<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n   { \u9632\u6b62\u5237\u65b0\u65f6\u95ea\u70c1\u7684\u7ec8\u6781\u89e3\u51b3\u529e\u6cd5(\u5bf9\u4ed8\u53cc\u7f13\u51b2\u65e0\u6548\u65f6) }\r\n   Perform($000B, 0, 0); \/\/\u9501\u5c4f\u5e55   \u9632\u6b62\u95ea\u70c1\r\n\r\n  \/\/ \u505a\u4e00\u4e9b\u4f1a\u53d1\u751f\u4e25\u91cd\u95ea\u70c1\u7684\u4e8b\u60c5..\r\n\r\n  \/\/\u89e3\u9501\u5c4f\u5e55\u5e76\u91cd\u753b\r\n   Perform($000B, 1, 0);\r\n   RedrawWindow(Handle, nil, 0, RDW_FRAME + RDW_INVALIDATE + RDW_ALLCHILDREN + RDW_NOINTERNALPAINT);\r\n<\/pre>\n<p>2.\u56fe\u7247\u4e0a\u663e\u793a\u900f\u660e\u6587\u5b57<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/\u56fe\u7247\u4e0a\u663e\u793a\u900f\u660e\u7684\u6587\u5b57\r\n\/\/\u76f4\u63a5\u7528.Canvas.Brush.Style:=bsClear;\r\n\/\/\u7136\u540e.Canvas.TextOut(x,y,'\u6587\u5b57\u663e\u793a\u900f\u660e');\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nvar\r\n   bitBuf:TBitmap;\r\nbegin\r\n   bitBuf := TBitmap.Create;\r\n  try\r\n     bitbuf.LoadFromFile('\u6d4b\u8bd5\u56fe\u7247.bmp');\r\n     Self.Canvas.Draw(0,0,bitbuf);\r\n\r\n     bitbuf.Transparent := True;\r\n     bitbuf.TransparentColor := clWhite;    \/\/\u6587\u5b57\u663e\u793a\u900f\u660e\r\n     bitbuf.Canvas.font.color := clBlue;     \/\/\u6587\u5b57\u989c\u8272\r\n     bitbuf.Canvas.TextOut(10,10,'\u8fd9\u6837\u5c31\u662f\u900f\u660e\u7684\u5b57\u4e86\uff01');\r\n\r\n     Self.Canvas.Draw(0,0,bitbuf);\r\n  finally\r\n     bitBuf.Free;\r\n  end;\r\nend;\r\n<\/pre>\n<p>3.\u53d6\u5f97\u672c\u673aIP\u5730\u5740(\u7cbe\u7b80\u7248)<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/\u53d6\u5f97\u672c\u5730IP\u5730\u5740(\u7cbe\u7b80\u7248)\r\n\/\/\u6ce8\uff1a\u4f7f\u7528\u51fd\u6570\u524d\u9700\u8981 WSAStartup($202, wsdata);\r\nfunction GetLocalIP(): String;\r\nvar\r\n   HostName: array&#x5B;0..255] of Char;\r\n   HostEnt: PHostEnt;\r\nbegin\r\n   Result := '';\r\n  if gethostname(HostName, 255) = 0 then\r\n  begin\r\n     HostEnt := gethostbyname(HostName);\r\n     Result := StrPas(inet_ntoa(PInAddr(PInAddr(HostEnt^.h_addr_list)^)^));\r\n  end;\r\nend;\r\n<\/pre>\n<p>4.\u62a5\u544a\u5185\u5b58\u6cc4\u6f0f<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  \/\/ \u5728\u7a0b\u5e8f\u4e2d\u52a0\u4e0a\u8fd9\u53e5\uff0c\u5f53\u9000\u51fa\u65f6\u4f1a\u62a5\u544a\u5185\u5b58\u6cc4\u6f0f\r\n   ReportMemoryLeaksOnShutdown := True;\r\n<\/pre>\n<p>5.\u91ca\u653e\u8d44\u6e90\u6587\u4ef6<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/ \u9996\u5148\u52a0\u5165.RC\u6587\u4ef6\uff0c\u5199\u4e0a MyDLL DAT testDLL.dll\r\n\/\/ \u7136\u540e\u7a0b\u5e8f\u91cc ExtractRes('DAT','MyDLL','123DLL.dll');\r\nprocedure ExtractRes(ResType, ResName, ResNewName:String);\r\nvar Res:TResourceStream;\r\nbegin\r\n   Res:=TResourceStream.Create(Hinstance, Resname, Pchar(ResType));\r\n  try\r\n     Res.SavetoFile('.\\'+ResNewName); \/\/ \u91ca\u653e\u5230\u5f53\u524d\u76ee\u5f55\r\n  finally\r\n     Res.Free;\r\n  end;\r\nend;\r\n<\/pre>\n<p>6.\u5ef6\u65f6<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/ \u6a21\u4effVB\u91ccDoEvents\u7684\u5ef6\u65f6\r\nprocedure Delay(const uDelay: DWORD);\r\nvar\r\n   n: DWORD;\r\nbegin\r\n   n := GetTickCount;\r\n  while ( (GetTickCount - n) &lt;= uDelay ) do\r\n     Application.ProcessMessages;\r\nend;\r\n<\/pre>\n<p>7.\u6807\u51c6C\u4e2d\u7684 itoa() \u51fd\u6570Delphi\u7248\uff0c\u5c06Int\u578b\u53d8\u91cf\u8f6c\u5316\u4e3a(radix)\u8fdb\u5236\u8f93\u51fa<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/\u4e8c\u8fdb\u5236    itoa(i, 2);\r\n\/\/\u516b\u8fdb\u5236    itoa(i, 8);\r\n\/\/\u5341\u516d\u8fdb\u5236 itoa(i, 16);\r\nfunction itoa(aData, radix: Integer): String;\r\nvar\r\n   t: Integer;\r\nbegin\r\n   Result := '';\r\n  repeat\r\n     t := aData mod radix;\r\n    if t &lt; 10 then\r\n       Result := InttoStr(t)+Result\r\n    else\r\n       Result := InttoHex(t, 1)+Result;\r\n     aData := aData div radix;\r\n  until (aData = 0);\r\nend;\r\n<\/pre>\n<p>8.\u7a0b\u5e8f\u5220\u9664\u81ea\u8eab<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/ \u5229\u7528\u6279\u5904\u7406\u6587\u4ef6\u6784\u9020\u4e00\u4e2a\u5faa\u73af\uff0c\u53ea\u8981\u5728 OnClose() \u4e2d\u8c03\u7528 DeleteMe() \u5c31\u53ef\u4ee5\u5220\u9664\u81ea\u8eab\r\nprocedure DeleteMe();\r\nvar\r\n   BatchFile: TextFile;\r\n   BatchFileName: string;\r\n   ProcessInfo: TProcessInformation;\r\n   StartUpInfo: TStartupInfo;\r\nbegin\r\n   BatchFileName := ExtractFilePath(ParamStr(0)) + '_deleteme.bat';\r\n   AssignFile(BatchFile, BatchFileName);\r\n   Rewrite(BatchFile);\r\n\r\n   Writeln(BatchFile, ':try');\r\n   Writeln(BatchFile, 'del &quot;' + ParamStr(0) + '&quot;');\r\n   Writeln(BatchFile,\r\n    'if exist &quot;' + ParamStr(0) + '&quot;' + ' goto try');\r\n   Writeln(BatchFile, 'del %0');\r\n   CloseFile(BatchFile);\r\n\r\n   FillChar(StartUpInfo, SizeOf(StartUpInfo), $00);\r\n   StartUpInfo.dwFlags := STARTF_USESHOWWINDOW;\r\n   StartUpInfo.wShowWindow := SW_HIDE;\r\n  if CreateProcess(nil, PChar(BatchFileName), nil, nil, False, IDLE_PRIORITY_CLASS, nil, nil, StartUpInfo, ProcessInfo) then\r\n  begin\r\n     CloseHandle(ProcessInfo.hThread);\r\n     CloseHandle(ProcessInfo.hProcess);\r\n  end;\r\nend;\r\n<\/pre>\n<p>9.\u5b89\u88c5\u670d\u52a1\u540e\u7acb\u5373\u542f\u52a8<\/p>\n<p>\u666e\u901a\u7f16\u5199\u7684\u670d\u52a1\uff0c\u5b89\u88c5\u540e\u5fc5\u987b\u91cd\u542f\u624d\u80fd\u542f\u52a8\uff0c\u8fd9\u662f\u81ea\u52a8\u542f\u52a8\u670d\u52a1\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\r\n{   Automatically start a service after using \/Install or \/Uniinstall switch\r\n\r\n   In the service unit add these statements before the 'end.' statement.\r\n   To automatically start or stop the service during install or uninstall\r\n   Tested only on Win2k and WinXP\r\n\r\n   Change the 'myservice' in both WinExec statements to your own service name.\r\n}\r\n\/\/ \u5728Service\u7684\u521d\u59cb\u5316\u548c\u7ed3\u675f\u90e8\u5206\u52a0\u5165\u5982\u4e0b\u4ee3\u7801\uff1a\r\n\r\ninitialization\r\n  if (ParamCount &gt;= 1) and (CompareText('\/uninstall',ParamStr(1))=0) then\r\n  begin\r\n    \/\/ \u5982\u679c\u662f\u5378\u8f7d\uff0c\u5148\u505c\u6b62\u670d\u52a1\u3002\u6ce8\u610f\u4fee\u6539 myservice \u4e3a\u4f60\u7684\u670d\u52a1\u540d\r\n     WinExec('cmd.exe \/c net stop myservice', sw_hide);\r\n     sleep(3000);\r\n  end;\r\n\r\nfinalization\r\n  if (ParamCount &gt;= 1) and (CompareText('\/Install',ParamStr(1))=0) then\r\n    \/\/ \u7528 net start \u6267\u884c\u670d\u52a1\r\n     WinExec('cmd.exe \/c net start myservice',sw_hide);\r\nend.\r\n<\/pre>\n<p>10.\u7ed9\u81ea\u5df1\u7f16\u5199\u7684\u670d\u52a1\u7a0b\u5e8f\u6dfb\u52a0\u63cf\u8ff0<\/p>\n<p>\u6ca1\u6709\u63cf\u8ff0\u7684\u670d\u52a1\u770b\u8d77\u6765\u8981\u591a\u53ef\u7591\u6709\u591a\u53ef\u7591\uff0c\u4eff\u7167\u5fae\u8f6f\u7684\u5199\u6cd5\u7ed9\u4f60\u7684\u670d\u52a1\u52a0\u4e2a\u63cf\u8ff0\u5427\uff1a<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nprocedure TMyService1.ServiceAfterInstall(Sender: TService);\r\nvar\r\n   MyReg: TRegistry;\r\nbegin\r\n   MyReg := TRegistry.Create;\r\n  try\r\n    with MyReg do\r\n    begin\r\n       RootKey := HKEY_LOCAL_MACHINE;\r\n      { Service1\u662f\u670d\u52a1\u7684\u540d\u5b57\uff0c\u6ce8\u610f\u4fee\u6539\u6210\u4f60\u81ea\u5df1\u7684 }\r\n      if Openkey('SYSTEM\\CurrentControlSet\\Services\\Service1', true) then\r\n         WriteString('Description', '\u4f60\u81ea\u5df1\u7684\u670d\u52a1\u63cf\u8ff0...');\r\n       CloseKey;\r\n    end;\r\n  finally\r\n     MyReg.Free;\r\n  end;\r\nend;\r\n<\/pre>\n<p>\u6bd4\u5982\u4f2a\u88c5\u88c5\u6210\u8fd9\u6837\uff1aSecurity Debug Manager (\u4effSecurity Accounts Manager)<br \/>\n\u63cf\u8ff0\uff1a\u7ba1\u7406\u7cfb\u7edf\u5b89\u5168\u8bbe\u7f6e\u548c\u914d\u7f6e\uff0c\u5e76\u63d0\u4f9b\u8c03\u8bd5\u4fe1\u606f\u3002\u5982\u679c\u6b64\u670d\u52a1\u88ab\u7ec8\u6b62\uff0c\u6b64\u7c7b\u578b\u5b89\u5168\u63aa\u65bd\u5c06\u4e0d\u53ef\u7528\u3002\u5982\u679c\u6b64\u670d\u52a1\u88ab\u7981\u7528\uff0c\u4efb\u4f55\u4f9d\u8d56\u5b83\u7684\u670d\u52a1\u5c06\u65e0\u6cd5\u542f\u52a8\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u9632\u6b62\u5237\u65b0\u65f6\u95ea\u70c1\u7684\u7ec8\u6781\u89e3\u51b3\u529e\u6cd5 { \u9632\u6b62\u5237\u65b0\u65f6\u95ea\u70c1\u7684\u7ec8\u6781\u89e3\u51b3\u529e\u6cd5(\u5bf9\u4ed8\u53cc\u7f13\u51b2\u65e0\u6548\u65f6) } Perform($ [&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-462","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/462","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=462"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions"}],"predecessor-version":[{"id":4746,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/462\/revisions\/4746"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}