createwindow 创建一个窗口(它的许多参数实在难搞懂),请附实例。我找遍dfw和csdn也没搞明白,它就这么难吗?靠!  

解决方案 »

  1.   

    在Delphi中不使用VCL库建立窗口
        Borland的Delphi语言中提供了类似于VB中ActiveX的VCL控件库,利用它建立程序界面是十分
    方便的,但是这也带来了一个问题,就是即使建立一个非常简单的窗口,Delphi也会将所有的控件
    编译进去,使得程序十分庞大(通过常规方式建立一个仅包含一个窗口,不响应任何消息的程序的
    尺寸大概就有至少200K字节以上)。
        这里,我向大家介绍一个不使用VCL库建立窗口的方法。首先打开一个文本编辑器(例如NotePad)
    然后在其中输入以下的程序代码:program Project2;uses
      Windows;
    const
      AppName = 'Windows';
      WM_DESTROY = 2;
      WM_LBUTTONUP = 514;
    {$R *.RES}//窗口处理函数
    function WindowProc(Window:Hwnd;Amessage,wParam,
             lParam:LongInt):LongInt;stdcall;export;
    begin
      WindowProc:=0;  case AMessage of
        WM_DESTROY:
        begin
          PostQuitmessage(0);
          Exit;
        end;
        WM_LBUTTONUP:
        begin
          PostQuitmessage(0);
          Exit;
        end;
      end;  WindowProc := DefWindowProc(Window,AMessage,wParam,lParam);
    end;//注册窗口函数
    function WinRegister:Boolean;
    var
      WindowClass:TWndClass;
    begin
      WindowClass.style := cs_hredraw or cs_vRedraw or cs_NoClose;
      WindowClass.lpfnWndProc := @WindowProc;
      WindowClass.cbClsExtra := 0;
      WindowClass.cbWndExtra := 0;
      WindowClass.hInstance := Hinstance;
      WindowClass.hIcon := LoadIcon(0,idi_Application);
      WindowClass.hCursor := LoadCursor(0,idc_Arrow);
      WindowClass.hbrBackground := HBrush(COLOR_BTNFACE);
      WindowClass.lpszMenuName := nil;
      WindowClass.lpszClassName := AppName;  Result:=RegisterClass(WindowClass)<>0;
    end;//建立工具栏窗口函数
    function WinCreate:HWnd;
    var
      hWindow:Hwnd;
      pcharTemp:PChar;
    begin
      hWindow:=CreateWindowEx(WS_EX_RTLREADING or WS_EX_TOOLWINDOW,
          AppName,'Samples Window',ws_OverlappedWindow,
          cw_UseDefault,cw_UseDefault,cw_UseDefault,cw_UseDefault,0,0,
          Hinstance,nil);
      if hWindow<>0 then
      begin
        pcharTemp:='Samples';
        ShowWindow(hWindow,cmdShow);
        TextOut(GetWindowDC(hWindow),10,50,pcharTemp,7);
        UpdateWindow(hWindow);
      end;  Result:=hWindow;
    end;var
      AMessage:TMsg;
      hWindow:HWnd;
    begin //主程序部分
      if not WinRegister then
      begin
        MessageBox(0,'窗口注册失败',nil,mb_ok);
        Exit;
      end;
      hWindow:=WinCreate;
      if hWindow=0 then
      begin
        MessageBox(0,'建立窗口失败',nil,mb_ok);
        Exit;
      end;
      While GetMessage(AMessage,0,0,0)do
      begin
        TranslateMessage(AMessage);
        DispatchMessage(AMessage);
      end;
      Halt(AMessage.wParam);
    end.
        将上面的内容以 Project2.dpr 为文件名保存到Delphi的Bin目录中,然后使用Delphi中的DCC32来
    编译工程文件,具体的使用方法是:DCC32 Project2.dpr 。在编译过程中可能有一些警告(Warning)
    消息,不要理会。编译完成后,在Bin目录下会产生一个Project2.exe的文件,查看以下,这个文件是
    不是“缩水”了很多呢?在我的机器上编译出来的程序只有17K左右,这同使用C++编译的程序大小几乎
    没有什么两样。
        分析上面的程序,使用C++编写过Windows下程序的读者可能会觉得十分的熟悉,的确,同C++
    编写一样。在Delphi中不使用VCL编写窗口界面也需要三个函数:窗口建立函数、窗口注册函数和窗
    口消息处理函数。在上面的程序中,函数WinCreate是窗口建立函数,这个函数通过CreateWindowsEx
    函数建立一个工具栏窗口,窗口建立成功后显示窗口并在窗口中10,50的位置输出“Samples”。
    WindowProc函数是窗口消息处理函数这个函数只处理两个消息:WM_DESTROY和WM_LBUTTONUP。在得到
    这两个消息后退出。WinRegister是窗口注册函数,这个函数注册WinCreate建立的窗口并禁止窗口系
    统菜单的“关闭”项,同时将WindowProc定义为窗口消息处理函数。
        对于使用Pascal语言,对C++不熟悉的朋友来说,只要根据自己的需要对上面的程序稍微做一些修
    改,就可以编写同C++编写出来的一样的“苗条”的程序了。
        以上程序在Borland Delphi4.0,Win98下编译通过。
      

  2.   

    program Project1;uses
      SysUtils,Windows,Messages;function WindowProc(Handle:HWND;Msg:Cardinal;wParam:WParam;lParam:LParam):integer;stdcall
    begin
        case Msg of
        WM_CREATE:MessageBox(Handle,'A pure sdk window','demo',MB_OK);
        WM_DESTROY:PostQuitMessage(0);
        else
            result:=DefWindowProc(handle,msg,wparam,lparam);
        end;end;
    var wc:tagWNDCLASSEXA;
        msg:tmsg;
        handle:HWND;
        hInst:Cardinal;
    begin
        hInst:=GetModuleHandle(0);
        wc.cbSize :=sizeof(tagWNDCLASSEXA);
        wc.style :=CS_HREDRAW or CS_VREDRAW;
        wc.lpfnWndProc :=@WindowProc;
        wc.cbClsExtra :=0;
        wc.cbWndExtra :=0;
        wc.hInstance:=hInst;
        wc.hbrBackground :=COLOR_WINDOW;
        wc.lpszMenuName :=nil;
        wc.lpszClassName :='PureAPI';
        wc.hIcon :=LoadIcon(0,IDI_APPLICATION);
        wc.hIconSm:=wc.hIcon ;
        wc.hCursor :=LoadCursor(0,IDC_ARROW);
        RegisterClassEx(wc);
        handle:=CreateWindowEx(0,'PureAPI','pure api window',
            WS_OVERLAPPEDWINDOW or WS_SIZEBOX or WS_MAXIMIZEBOX,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            400,300,0,0,
            hInst,nil);
        ShowWindow(Handle,SW_SHOWNORMAL);
        UpdateWindow(Handle);
        while GetMessage(msg,0,0,0) do
        begin
            TranslateMessage(msg);
            DispatchMessage(msg);
        end;    // while
    end.
      

  3.   

    用MASM32编译,2560字节:.486
    .model flat, stdcall
    option casemap:noneincludelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\gdi32.libinclude \masm32\include\windows.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\user32.inc
    include \masm32\include\gdi32.inc
    WinMain PROTO STDCALL :DWORD, :DWORD, :DWORD, :DWORD
    WndProc PROTO STDCALL :DWORD, :DWORD, :DWORD, :DWORD
    .data?
    hInstance dd ?.data
    ClassName db "FirstWindowClass",0
    AppName db "FirstWindow",0.code
    start:invoke GetModuleHandle, NULL
    mov hInstance, eax
    invoke WinMain, hInstance, NULL, NULL, SW_SHOWNORMAL
    invoke ExitProcess, NULLWinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX
    LOCAL hwnd:DWORD
    LOCAL msg:MSG
    mov wc.cbSize,SIZEOF WNDCLASSEX
    mov wc.style, CS_HREDRAW or CS_VREDRAW
    mov wc.lpfnWndProc, OFFSET WndProc
    mov wc.cbClsExtra,NULL
    mov wc.cbWndExtra,NULL
    push hInst
    pop wc.hInstance
    mov wc.hbrBackground,COLOR_WINDOW
    mov wc.lpszMenuName,NULL
    mov wc.lpszClassName,OFFSET ClassName
    invoke LoadIcon,NULL,IDI_APPLICATION
    mov wc.hIcon, eax
    mov wc.hIconSm, eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov wc.hCursor,eax
    invoke RegisterClassEx, addr wcINVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
    WS_OVERLAPPEDWINDOW-WS_SIZEBOX-WS_MAXIMIZEBOX,CW_USEDEFAULT,\
    CW_USEDEFAULT,400,300,NULL,NULL,\
    hInst,NULL
    mov hwnd,eax
    invoke ShowWindow, hwnd,SW_SHOWNORMAL
    invoke UpdateWindow, hwnd
    .WHILE TRUE
    invoke GetMessage, ADDR msg,NULL,0,0
    .BREAK .IF (!eax)
    invoke TranslateMessage, ADDR msg
    invoke DispatchMessage, ADDR msg
    .ENDW
    mov eax,msg.wParam
    ret
    WinMain endpWndProc proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
    mov eax, uMsg
    .IF eax==WM_CREATE
    invoke MessageBox, NULL, ADDR AppName, ADDR AppName, NULL
    .ELSEIF eax==WM_DESTROY
    invoke PostQuitMessage, NULL
    .ELSE
    invoke DefWindowProc, hWnd, uMsg, wParam, lParam
    .ENDIF
    ret
    WndProc endp
    end start 
      

  4.   

    var
      hWindow: HWND;{ Register the extended Window class }
    function RegisterClassEx: Boolean;
    var  WindowClassEx: TWndClassEx;
    begin
      {setup our new window class}
      WindowClassEx.cbSize := SizeOf(TWndClassEx);      {the size of the structure}
      WindowClassEx.Style := CS_HREDRAW or CS_VREDRAW;  {set the class styles}
      WindowClassEx.lpfnWndProc := @DefWindowProc;      {point to the default window
                                                         procedure}
      WindowClassEx.cbClsExtra := 0;                    {no extra class memory}  WindowClassEx.cbWndExtra := 0;                    {no extra window memory}
      WindowClassEx.hInstance := hInstance;             {the application instance}
      WindowClassEx.hIcon := LoadIcon(0, IDI_APPLICATION); {load a predefined logo}
      WindowClassEx.hCursor := LoadCursor(0, IDC_WAIT); {load a predefined cursor}
      WindowClassEx.hbrBackground := COLOR_WINDOW;      {use a predefined color}
      WindowClassEx.lpszMenuName := nil;                {no menu}  WindowClassEx.lpszClassName := 'TestClass';       {the registered class name}
      WindowClassEx.hIconSm := 0;                       {no small icon}  {now that we have our class set up, register it with the system}
      Result := Windows.RegisterClassEx(WindowClassEx) <> 0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      {register our new class first}
      if not RegisterClassEx then  begin
        ShowMessage('RegisterClassEx failed');
        Exit;
      end;  {now, create a window based on our new class}
      hWindow := CreateWindowEx(WS_EX_CLIENTEDGE OR      {this window has a sunken 
                                                          edge}
                                WS_EX_CONTEXTHELP,       {and a context sensitive 
                                                          help button}
                                'TestClass',             {the registered class name}                            'API Window',            {the title bar text}
                                WS_OVERLAPPEDWINDOW AND  {a normal window}
                                NOT WS_MAXIMIZEBOX AND   {without a minimize or 
                                                          maximize button}
                                NOT WS_MINIMIZEBOX,      {so the help button is not 
                                                          obscured}
                                CW_USEDEFAULT,         {default horizontal position}                            CW_USEDEFAULT,         {default vertical position}
                                CW_USEDEFAULT,         {default width}
                                CW_USEDEFAULT,         {default height}
                                0,                     {no parent window}
                                0,                     {no menu}
                                hInstance,             {the application instance}                            nil                    {no additional information}
                                );  {if our window was created successfully, show it}
      if hWindow <> 0 then
      begin
        ShowWindow(hWindow, SW_SHOWNORMAL);
        UpdateWindow(hWindow);
      end
      else
      begin
        ShowMessage('CreateWindow failed');
        Exit;
      end;end;procedure TForm1.Button2Click(Sender: TObject);begin
      {first, destroy our window}
      DestroyWindow(hWindow);  {now we can unregister our new class}
      Windows.UnregisterClass('TestClass', hInstance);
    end;