今天心血来潮 看了下win32汇编的书籍 看到 汇编调用windows API 实现创建窗体 
本想用delphi 实现 结果失败了 求高人指点!
unit unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;const
  szClassName:PChar = 'MyClass';
  szCaption:PChar = 'Delphi CreateMainForm with Windows API!';
  szText:PChar = '嘿嘿嘿嘿嘿嘿嘿嘿';
var
  hInstance:LongWord;
  hWinMain:LongWord;
  stwndclass:WNDCLASSEXA;
  
function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
function  registerMyclass():Boolean;
function  ShowMyWindow():Boolean;
procedure CreateMyForm();
implementationprocedure CreateMyForm();
var
  stMsg:MSG;
begin
  hInstance := GetModuleHandle(szClassName);
  ZeroMemory(@stwndclass,SizeOf(stwndclass));
  if ((registerMyclass()) and (ShowMyWindow())) then
  begin
    while GetMessage(stMsg,0,0,0) do
    begin
      if not GetMessage(stMsg,0,0,0) then break;
      TranslateMessage(stMsg);
      DispatchMessage(stMsg)
    end;
  end;
end;function  ShowMyWindow():Boolean;
begin
  hWinMain := CreateWindowEx(WS_EX_CLIENTEDGE,szClassName,szCaption,WS_OVERLAPPEDWINDOW,100,
  100,600,400,0,0,hInstance,nil);
  if (hWinMain <> 0) then
  begin
    ShowWindow(hWinMain,SW_SHOWNORMAL);
    UpdateWindow(hWinMain);
    Result := True;
  end
  ELSE
  begin
    Result := False;
  end;
end;function  registerMyclass():Boolean; //注册窗口类
begin
  stwndclass.hCursor:=LoadCursor(0,IDC_ARROW);
  stwndclass.hInstance := hInstance;
  stwndclass.cbSize := SizeOf(WNDCLASSEXA);
  stwndclass.style := CS_HREDRAW or CS_VREDRAW;
  stwndclass.lpfnWndProc := @WindowProc;
  stwndclass.lpszClassName := szClassName;
  Result :=(RegisterClassEx(stwndclass)<>0);
end;function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;
var
  stps:PAINTSTRUCT;
  stRect:TRect;
  hdc:LongWord;
begin
  if umsg = WM_PAINT then
  begin
    BeginPaint(hwnd,stps);
    GetClientRect(hwnd,stRect);
    DrawText(hdc,szText,-1,stRect,DT_CENTER);
    EndPaint(hwnd,stps);
  end
  else if  umsg = WM_CLOSE then
  begin
    DestroyWindow(hwnd);
    PostQuitMessage(0);
  end;
end;
end.
工程文件program Project1;uses
  Forms,
  Windows,
  window in unit1.pas';{$R *.res}begin
  //Application.Initialize;
  CreateMyForm;
  //Application.Run;
end.
 

解决方案 »

  1.   

    罗云斌 的 Windows.环境下32位汇编语言程序设计(第2版)
    第4.2.2 章 教我们创建第一个窗体·
      

  2.   


    procedure CreateMyForm();
    var
      stMsg:MSG;
    begin
      hInstance := GetModuleHandle(szClassName);
      ZeroMemory(@stwndclass,SizeOf(stwndclass));
      if ((registerMyclass()) and (ShowMyWindow())) then
      begin
        while GetMessage(stMsg,0,0,0) do
        begin
             //去掉这里
          TranslateMessage(stMsg);
          DispatchMessage(stMsg)
        end;
      end;
    end;
      

  3.   


    TranslateMessage(stMsg);
    DispatchMessage(stMsg)
    这2个函数是提交  消息  用的啊!  我去掉了也没用 !  
      

  4.   

    procedure CreateMyForm();
    var
      stMsg:MSG;
    begin
      hInstance := GetModuleHandle(szClassName);
      ZeroMemory(@stwndclass,SizeOf(stwndclass));
      if ((registerMyclass()) and (ShowMyWindow())) then
      begin
        while GetMessage(stMsg,0,0,0) do
        begin      TranslateMessage(stMsg);
          DispatchMessage(stMsg)
        end;
      end;
    end;
    这样啊。   不是叫你去掉下面那2句!而是去掉if not GetMessage(stMsg,0,0,0) then break;
    这句。
      

  5.   

    我的情况不是想lz那样,我是僵住不懂(没结束,窗口没显示出来)。我在"function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;"
    里添加了 
    "else
        Result:=DefWindowProc(hwnd,umsg,wParam,lParam);"
    窗口出来了,但是感觉也很怪。
      

  6.   


    function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;
    var
      stps:PAINTSTRUCT;
      stRect:TRect;
      hdc:LongWord;
    begin
      if umsg = WM_PAINT then
      begin
        BeginPaint(hwnd,stps);
        GetClientRect(hwnd,stRect);
        DrawText(hdc,szText,-1,stRect,DT_CENTER);
        EndPaint(hwnd,stps);
      end
      else if  umsg = WM_CLOSE then
      begin
        DestroyWindow(hwnd);
        PostQuitMessage(0);
      end
      else
        Result:=DefWindowProc(hwnd,umsg,wParam,lParam);
    end;
      

  7.   


    去掉那个 //if not GetMessage(stMsg,0,0,0) then break; 
    我断点跟踪了下 程序永久处于 循环状态~~~不行的·
      

  8.   

    okmnji79513  继续加油 好像有点苗头了
      

  9.   


    function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;
    var
      stps:PAINTSTRUCT;
      stRect:TRect;
      hdc:LongWord;
    begin
      if umsg = WM_PAINT then
      begin
        BeginPaint(hwnd,stps);
        GetClientRect(hwnd,stRect);
        DrawText(hdc,szText,-1,stRect,DT_CENTER);
        EndPaint(hwnd,stps);
      end
      else if  umsg = WM_CLOSE then
      begin
        DestroyWindow(hwnd);
        PostQuitMessage(0);
      end
      else
        Result:=DefWindowProc(hwnd,umsg,wParam,lParam);//这里也要加上啊,windows默认的处理,不然你没处理的消息就得不到处理,显示出来的窗口就会不对地!
    end;
      

  10.   

    "if not GetMessage(stMsg,0,0,0) then break;"应该去掉,不然GetMessage重复了。第一次GetMessage取掉了消息,第二次GetMessage就取不到消息了。
      

  11.   

    对,把if not GetMessage(stMsg,0,0,0) then break去掉
      

  12.   


    搞定 unit window;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;const
      szClassName:PChar = 'MyClass';
      szCaption:PChar = 'Delphi CreateMainForm with Windows API!';
      szText:PChar = '嘿嘿嘿嘿嘿嘿';
    var
      hInstance:LongWord;
      hWinMain:LongWord;
      stwndclass:WNDCLASSEXA;function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;export;
    function  registerMyclass():Boolean;
    function  ShowMyWindow():Boolean;
    procedure CreateMyForm();
    implementationprocedure CreateMyForm();
    var
      stMsg:MSG;
    begin
      hInstance := GetModuleHandle(szClassName);
      ZeroMemory(@stwndclass,SizeOf(stwndclass));
      if ((registerMyclass()) and (ShowMyWindow())) then
      begin
        while GetMessage(stMsg,0,0,0) do
        begin
          TranslateMessage(stMsg);  //修改
          DispatchMessage(stMsg)
        end;
      end;
    end;function  ShowMyWindow():Boolean;
    begin
      hWinMain := CreateWindowEx(WS_EX_CLIENTEDGE,szClassName,szCaption,WS_OVERLAPPEDWINDOW,100,
      100,600,400,0,0,hInstance,nil);
      if (hWinMain <> 0) then
      begin
        ShowWindow(hWinMain,SW_SHOWNORMAL);
        UpdateWindow(hWinMain);
        Result := True;
      end
      ELSE
      begin
        Result := False;
      end;
    end;function  registerMyclass():Boolean; //注册窗口类
    begin
      stwndclass.hCursor:=LoadCursor(0,IDC_ARROW);
      stwndclass.hInstance := hInstance;
      stwndclass.cbSize := SizeOf(WNDCLASSEXA);
      stwndclass.style := CS_HREDRAW or CS_VREDRAW;
      stwndclass.lpfnWndProc := @WindowProc;
      stwndclass.lpszClassName := szClassName;
      stwndclass.hbrBackground := COLOR_WINDOW + 1;
      Result :=(RegisterClassEx(stwndclass)<>0);
    end;function  WindowProc(hwnd:HWND;umsg:UINT;wParam:WPARAM;lParam:LPARAM):LRESULT;
    var
      stps:PAINTSTRUCT;
      stRect:TRect;
      ahdc:HDC;
    begin
      if umsg = WM_PAINT then
      begin
        ahdc := BeginPaint(hwnd,stps);
        GetClientRect(hwnd,stRect);
        DrawText(ahdc,szText,Length(szText),stRect,DT_SINGLELINE or DT_CENTER or DT_VCENTER );//修改
        EndPaint(hwnd,stps);
      end
      else if  umsg = WM_CLOSE then
      begin
        DestroyWindow(hwnd);
        PostQuitMessage(0);
      end
      else
      begin
        Result:=DefWindowProc(hwnd,umsg,wParam,lParam);   //看书看到漏掉了
      end;
    end;
    end.
      

  13.   


    不过生成的exe 比汇编大多了
      

  14.   

    SQLDebug_Fan okmnji79513 kfcoffe 留下Q 交流交流