游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1999|回复: 0

Delpih 中的windows API编程初步(3)

[复制链接]

152

主题

155

帖子

180

积分

注册会员

Rank: 2

积分
180
发表于 2004-12-13 11:39:00 | 显示全部楼层 |阅读模式
下面我们用大家最常见的一个例子对前面的知识加以总结。在这个例子中,我们将在窗体中显示“ hello , world ! ” 。下面是程序及其运行效果:



program Project1;



{ $ APPTYPE CONSOLE }



uses

  Windows ,

  Messages ;

{ uses

  SysUtils ;  }



var

wClass :   TWndClass;      // 主窗口类

hInst ,                     //应用程序句柄

Handle : HWnd ;              //  主窗口

aMsg :     TMsg ;       //消息

RCT : TRect ;             //区域

ps : TPaintStruct ;     //显示

dc : hdc ;                 //设备上下文





//函数:WindowProc

//作用:处理主窗口的消息

function WindowProc ( hWnd , Msg , wParam , lParam : Longint ) : Longint ; stdcall ;

begin

   WindowProc : = 0 ;

   case Msg of

  WM_PAINT :

     begin

         dc : =BeginPaint ( hWnd , ps ) ;

         GetClientRect ( hWnd , RCT ) ;

         DrawText ( dc , ' hello , world ! ' , -1 , RCT , Dt_SINGLELINE or DT_CENTER or DT_VCENTER ) ;

         EndPaint ( hWnd , ps ) ;

         Exit ;

     end ;

  WM_DESTROY : //结束应用程序

       Begin

                 PostQuitMessage ( 0 ) ;

                 Exit ;

        end ;

  end ;

  Result : = DefWindowProc ( hWnd , Msg , wParam , lParam ) ; //消息默认处理

end ;



//主窗口

begin

  // hInst : = GetModuleHandle ( nil ) ; // 获得应用程序句柄

  with wClass do //初始化窗口类

  begin

    hInstance : =     system.MainInstance ;

    Style : =         CS_HREDRAW or CS_VREDRAW ;

    HIcon : =         LoadIcon ( 0 , IDI_APPLICATION ) ;

    LpfnWndProc : =   @WindowProc ;

    HbrBackground : = GetStockObject ( WHITE_BRUSH ) ;

    lpszClassName : =  ' Sample Class ' ;

    hCursor : =       LoadCursor ( 0 , IDC_ARROW ) ;

  end ;

  RegisterClass ( wClass ) ; // 注册窗口类

  //创建主窗口

  Handle : = CreateWindow (

    ' Sample Class ' ,          // 窗口类名

    ' Windows API在Delphi中的应用 ' ,  //窗口标题

    WS_OVERLAPPEDWINDOW or WS_VISIBLE ,  // 窗口风格

    10 ,                      //左边界坐标

    10 ,                      //上边界坐标

    400 ,                     // 宽度

    300 ,                     // 高度

    0 ,                       // 父窗口句柄

    0 ,                       //菜单句柄

    system . MainInstance ,                    // 应用程序实例

    nil                     //创建窗口的附加参数

  ) ;



  if Handle <> 0 then

  begin

      ShowWindow ( Handle , SW_SHOW ) ;

      UpdateWindow ( Handle ) ;

  end ;

  while ( GetMessage ( aMsg , Handle , 0 , 0 ) ) do //消息循环

  begin

    TranslateMessage ( aMsg ) ;            //翻译消息

    DispatchMessage ( aMsg ) ;             //发送消息

  end ;

end .



  效果如下图:

sf_20041213113927.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-12-23 16:33

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表