Yanghn Notes

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            10 Posts :: 10 Stories :: 2 Comments :: 0 Trackbacks
          unit Unit1;
          
          interface
          
          uses
            Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
            Dialogs, StdCtrls;
          
          type
            TForm1 = class(TForm)
              Button1: TButton;
              procedure Button1Click(Sender: TObject);
            private
              { Private declarations }
            public
              { Public declarations }
            end;
          
          var
            Form1: TForm1;
          
          //MB 函數的聲明:
          function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer; stdcall;
          
          implementation
          
          {$R *.dfm}
          {調用外部 DLL 中的函數,譬如調用系統 user32.dll 中的 MessageBoxA}
          //function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer;
          //  stdcall; external user32 name 'MessageBoxA';
          
          {其中 user32 是 Delphi 定義的常量 'user32.dll',可以直接寫成:}
          //function MB(hWnd: HWND; lpText, lpCaption: PChar; uType: UINT): Integer;
          //  stdcall; external 'user32.dll' name 'MessageBoxA';
          
          {name 后面說明函數的真實名字}
          {external 子句說明單元載入時就加載函數,也就是早綁定;如果晚綁定需要用 LoadLibrary}
          {stdcall 指令表示參數傳遞是從右到左(Pascal則反之),不通過CPU寄存器傳遞}
          
          {4個參數的數據類型可以使用對應的 Delphi 數據類型,譬如:}
          //function MB(hWnd: LongWord; lpText, lpCaption: PChar; uType: LongWord): Integer;
          //  stdcall; external 'user32.dll' name 'MessageBoxA';
          
          {或者是:}
          //function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer;
          //  stdcall; external 'user32.dll' name 'MessageBoxA';
          
          {如果函數在此單元聲明后,需要給其他單元調用,需要先在 interface 區聲明:}
          //function MB(hWnd: Cardinal; lpText, lpCaption: PChar; uType: Cardinal): Integer;
          //  stdcall;
          {本例已經這樣做了,如果要測試其他幾種情況,需要先注釋掉它}
          {然后在 implementation 區,說明函數的來源:}
          function MB; external 'user32.dll' name 'MessageBoxA';
          
          //調用測試:
          procedure TForm1.Button1Click(Sender: TObject);
          var
            t,b:  PChar;
          begin
            t := '標題';
            b := '內容';
            MB(0,b,t,0);
          end;
          
          end.
          posted on 2010-05-10 10:09 小海 閱讀(191) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 金寨县| 策勒县| 绥阳县| 蒙自县| 政和县| 康定县| 眉山市| 老河口市| 清水河县| 白朗县| 元阳县| 庐江县| 宁晋县| 洛扎县| 余姚市| 南岸区| 克东县| 嘉禾县| 桃园县| 柏乡县| 丰镇市| 衡阳市| 都兰县| 临城县| 望都县| 财经| 东海县| 宁武县| 通道| 梧州市| 内乡县| 宝清县| 江口县| 沙坪坝区| 滕州市| 哈尔滨市| 城步| 乌鲁木齐县| 乌拉特中旗| 巨鹿县| 长葛市|