一个用delphi写的整合汇编与api的简单的窗口程序

program Project1;


{ Types and Structures Definition }
type
  WNDCLASSEX = packed record
    cbSize: LongWord;
    style: LongWord;
    lpfnWndProc: Pointer;
    cbClsExtra: Integer;
    cbWndExtra: Integer;
    hInstance: LongWord;
    hIcon: LongWord;
    hCursor: LongWord;
    hbrBackground: LongWord;
    lpszMenuName: PAnsiChar;
    lpszClassName: PAnsiChar;
    hIconSm: LongWord;
  end;


  POINT = packed record
    X: Longint;
    Y: Longint;
  end;


  MSG = packed record
    hwnd: LongWord;
    message: LongWord;
    wParam: Longint;
    lParam: Longint;
    time: LongWord;
    pt: POINT;
  end;


{ Application Specific Variables and constants }
const
  szBuf = 255;                               // Size of buffer used to handle strings
  WndClsSize = SizeOf(WNDCLASSEX);           // Size of WNDCLASSEX Structure
  wnd_class: PChar = 'TMainWindow';          // Main window class name.
  wnd_title: PChar = 'Closer to the Metal';  // Main window caption.
var
  wcx      : WNDCLASSEX;                     // Main window structure
  msgbuf   : MSG;                            // Message structure
  hwnd     : LongWord;                       // A window handle


{ Windows Specific Constants }
const
  { Class styles }
  CS_VREDRAW = LongWord(1);
  CS_HREDRAW = LongWord(2);
  CS_GLOBALCLASS = $4000;


  { Color Types }
  COLOR_WINDOW = 5;


  { Window Styles }
  WS_OVERLAPPED  = 0;
  WS_CAPTION     = $C00000;      { WS_BORDER or WS_DLGFRAME  }
  WS_SYSMENU     = $80000;
  WS_THICKFRAME  = $40000;
  WS_MINIMIZEBOX = $20000;
  WS_MAXIMIZEBOX = $10000;
  WS_VISIBLE     = $10000000;


  { Common Window Styles }
  WS_OVERLAPPEDWINDOW = (WS_VISIBLE or WS_OVERLAPPED or WS_CAPTION
    or WS_SYSMENU or WS_THICKFRAME or WS_MINIMIZEBOX or WS_MAXIMIZEBOX);


  { Messages }
  WM_DESTROY  = $0002;


{ Windows API's }
function GetModuleHandle(lpModuleName: PChar): HMODULE;
  stdcall; external 'kernel32.dll' name 'GetModuleHandleA';


procedure ExitProcess(uExitCode: LongWord);
  stdcall; external 'kernel32.dll' name 'ExitProcess';


function DefWindowProc(hWnd: LongWord; Msg: LongWord;
  wParam: Longint; lParam: Longint): Longint;
  stdcall; external 'user32.dll' name 'DefWindowProcA';


function RegisterClassEx(const WndClass: WNDCLASSEX): Word;
  stdcall; external 'user32.dll' name 'RegisterClassExA';


function CreateWindowEx(dwExStyle: LongWord; lpClassName: PChar;
  lpWindowName: PChar; dwStyle: LongWord;
  X, Y, nWidth, nHeight: Integer; hWndParent: LongWord;
  hMenu: LongWord; hInstance: LongWord; lpParam: Pointer): LongWord;
  stdcall; external 'user32.dll' name 'CreateWindowExA';


function GetMessage(var lpMsg: MSG; hWnd: LongWord;
  wMsgFilterMin, wMsgFilterMax: LongWord): LongBool;
  stdcall; external 'user32.dll' name 'GetMessageA';


function DispatchMessage(const lpMsg: MSG): Longint;
  stdcall; external 'user32.dll' name 'DispatchMessageA';


procedure PostQuitMessage(nExitCode: Integer);
  stdcall; external 'user32.dll' name 'PostQuitMessage';


{ Windows Procedure }
function WindowProc(hWnd, Msg: LongWord; wParam, lParam: Longint): Longint; stdcall;
asm
    // The inline assembler will take care of setting the stack,
    // preserving the registers and returning.
    mov    EAX, [Msg]
    // WM_DESTROY:
    cmp     EAX, WM_DESTROY
    je      @@m_destroy
    // All Other Messages:
    jmp     @@defwndproc
@@m_destroy:
    push    0
    call    PostQuitMessage // Quit.
    jmp     @@return
@@defwndproc:
    push    [lParam]
    push    [wParam]
    push    [Msg]
    push    [hWnd]
    call    DefWindowProc
    jmp     @@finish
@@return:
    xor     eax,eax
@@finish:
end;


{ Main Program Block }
asm
  // GetModuleHandle with a NULL pointer gives us the instance handle
  // of the EXE file. This is the module that will "own" the window class.
    push    0
    call    GetModuleHandle


    // Define our window properties:
    mov     [wcx.cbSize], WndClsSize;
    mov     [wcx.style], CS_VREDRAW or CS_HREDRAW or CS_GLOBALCLASS
    mov     [wcx.lpfnWndProc], offset WindowProc
    mov     [wcx.cbClsExtra], 0
    mov     [wcx.cbWndExtra], 0
    mov     eax, hInstance
    mov     [wcx.hInstance], EAX
    mov     [wcx.hIcon], 0
    mov     [wcx.hCursor], 0
    mov     [wcx.hbrBackground], COLOR_WINDOW + 1
    mov     dword ptr [wcx.lpszMenuName], 0
    mov     dword ptr [wcx.lpszClassName], offset wnd_class
    mov     [wcx.hIconSm], 0


    mov     EAX, wnd_class
    mov     [wcx.lpszClassName], EAX


    // Register window class:
    push    offset wcx
    call    RegisterClassEx


    // Create window:
    push    0                       // lpParam
    push    [wcx.hInstance]         // hInstance
    push    0                       // hMenu
    push    0                       // hWndParent
    push    200                     // nHeight
    push    200                     // nWidth
    push    100                     // y (top)
    push    100                     // x (left)
    push    WS_OVERLAPPEDWINDOW     // dwStyle
    mov     EAX, wnd_title          // lpWindowName
    push    EAX
    mov     EAX, wnd_class          // lpClassName
    push    EAX
    push    0                       // dwExStyle
    call    CreateWindowEx
    mov     hwnd, EAX


    // Message Loop/Pump:
@@msg_loop:
    push    0                       // wMsgFileterMax
    push    0                       // wMsgFilterMin
    push    0                       // hWnd  (0 = all windows)
    push    offset msgbuf           // lpMsg
    call    GetMessage
    cmp     eax, 0                  // Returns 0 (zero) if WM_QUIT
    jz      @@end_loop
    push    offset msgbuf
    call    DispatchMessage
    jmp     @@msg_loop
@@end_loop:


    // Terminating the program:
    push    0                       // Error return code.
    call    ExitProcess
end.
初学 Delphi 嵌入汇编[1] - 汇编语言与机器语言 初学 Delphi 嵌入汇编[2] - 汇编语言关键字 初学 Delphi 嵌入汇编[3] - 第一个 Delphi汇编的例子 初学 Delphi 嵌入汇编[4] - 寄存器在过程与函数中的使用 初学 Delphi 嵌入汇编[5] - 寄存器在过程与函数中的使用 - 续 初学 Delphi 嵌入汇编[6] - & 操作符 初学 Delphi 嵌入汇编[7] - 使用常量 初学 Delphi 嵌入汇编[8] - 8 位寄存器、16 位寄存器与 32 位寄存器 初学 Delphi 嵌入汇编[9] - asm 可以代替 begin 初学 Delphi 嵌入汇编[10] - 函数返回值与寄存器 初学 Delphi 嵌入汇编[11] - 用汇编一个 Delphi 函数 初学 Delphi 嵌入汇编[12] - 在汇编代码中可以直接使用 Result 初学 Delphi 嵌入汇编[13] - 地址参数用 [] 取值 初学 Delphi 嵌入汇编[14] - 常量与变量在汇编中的一个区别 初学 Delphi 嵌入汇编[15] - 需要保护的寄存器 初学 Delphi 嵌入汇编[16] - 进制的表示方法 初学 Delphi 嵌入汇编[17] - 逻辑运算 初学 Delphi 嵌入汇编[18] - SHL 与 SHR 初学 Delphi 嵌入汇编[19] - Delphi 的无符号整数类型 初学 Delphi 嵌入汇编[20] - Delphi 的 Integer 类型 初学 Delphi 嵌入汇编[21] - Delphi 的其他整数类型 初学 Delphi 嵌入汇编[22] - 在汇编代码中使用记录 初学 Delphi 嵌入汇编[23] - LOOP 循环 初学 Delphi 嵌入汇编[24] - 汇编语言的简单数据类型 初学 Delphi 嵌入汇编[25] - 在汇编中调用函数 初学 Delphi 嵌入汇编[26] - 大小字母转换 初学 Delphi 嵌入汇编[27] - XCHG 指令: 交换寄存器的内容 初学 Delphi 嵌入汇编[28] - 把 EAX 的值置为 0 的三种方法与效率 初学 Delphi 嵌入汇编[29] - 寄存器所能接受的数值范围 初学 Delphi 嵌入汇编[30] - 寄存器表
好的,下面给您提供一个简单Delphi程序示例,用于调用Open API获取天气信息: ```delphi uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls, System.Net.HttpClientComponent, System.Net.URLClient, System.Net.HttpClient, System.JSON; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.fmx} procedure TForm1.Button1Click(Sender: TObject); var HttpClient: THttpClient; URL: string; Response: IHTTPResponse; JSONValue: TJSONValue; begin URL := 'https://api.openweathermap.org/data/2.5/weather?q=London&appid=your-api-key'; HttpClient := THttpClient.Create; try Response := HttpClient.Get(URL); if Response.StatusCode = 200 then begin JSONValue := TJSONObject.ParseJSONValue(Response.ContentAsString); Memo1.Lines.Add(JSONValue.GetValue<string>('weather[0].description')); end else Memo1.Lines.Add('Error: ' + Response.StatusCode.ToString); finally HttpClient.Free; end; end; end. ``` 在这个示例中,我们使用了THttpClient组件来创建HTTP客户端,然后使用Get方法向Open API发送请求。如果请求成功,我们将会得到一个JSON字符串,通过解析JSON字符串,我们可以获取到我们需要的信息。在这个例子中,我们获取了伦敦的天气信息,将天气描述显示在Memo控件中。 您需要将示例中的“your-api-key”替换为您自己的API密钥。此外,您还需要在项目文件中添加“System.Net.HttpClientComponent”单元。 这只是一个简单的示例,您可以根据自己的需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值