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 小海 閱讀(189) 評論(0)  編輯  收藏

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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 海伦市| 宁阳县| 通道| 嘉禾县| 佳木斯市| 太仓市| 汝州市| 堆龙德庆县| 和龙市| 吴堡县| 聊城市| 兴文县| 湖北省| 新安县| 雷波县| 乐业县| 神农架林区| 尉犁县| 苏尼特右旗| 法库县| 枣庄市| 吉林市| 湟中县| 衡东县| 定兴县| 峨边| 休宁县| 宜君县| 东乌珠穆沁旗| 长海县| 类乌齐县| 繁昌县| 定安县| 无极县| 凤台县| 云梦县| 开鲁县| 金寨县| 长岛县| 枝江市| 鄂州市|