Delphi下用WindowsAPI创建窗体

//   Delphi 下调用Windows API 创建窗体.       //
//  模板-------BY Hottey 2004-4-13-0:18       //
//  作者网站: http://asp.itdrp.com/hottey      // 
program delphi;

uses
  windows,
  messages;

const
  hellostr='Hello World!';

{$R delphi.res}


//窗口消息处理函数.
function MyWinProc(hWnd:THandle;uMsg:UINT;wParam,lParam:Cardinal):Cardinal;exp
ort;stdcall;

var 
  hdca,hdcb:THandle;         //设备描述表句柄.
  rect:TRect;                //矩形结构.
  font:HFont;
  ps:TPaintStruct;           //绘图结构.
begin
  result:=0;
  case uMsg of
    WM_PAINT:
      begin
        hdca:=BeginPaint(hWnd,ps);
        SetBkMode(hdca, Transparent);
        SetBkColor(hdca,GetBkColor(hdca));
        GetClientRect(hWnd,rect);      //获取窗口客户区的尺寸.
        DrawText(hdca,Pchar(hellostr),-1,rect,DT_SINGLELINE or DT_CENTER or DT
_VCENTER);
//      TextOut(hdc,100,40,hellostr,Length(hellostr));
        EndPaint(hWnd,ps);
      end;
    WM_CREATE:
      begin
        hdcb  := GetDC(hWnd);
        font  := CreateFont(45, 0, 0, 0, FW_normal, 0, 0, 0, ansi_charset, out
_default_precis, clip_default_precis,
        default_quality, 34, PChar('Arial'));
        SelectObject(hdcb, font);
        ReleaseDC(hWnd, hdcb);
      end;
    WM_DESTROY:
      PostQuitMessage(0)
    else
//使用缺省的窗口消息处理函数.
      result:=DefWindowProc(hWnd,uMsg,wParam,lParam);
    end;
end;

//主程序开始.

var
  Msg        :TMsg;          //消息结构.
  hWnd,hInst :THandle;       //Windows 窗口句柄.
  WinClass   :TWndClassEx;   //Windows 窗口类结构.
begin
  hInst:=GetModuleHandle(nil); // get the application instance
  WinClass.cbSize:=SizeOf(TWndClassEx);
  WinClass.lpszClassName:='MyWindow';         //类名.
  WinClass.style:=CS_HREDRAW or CS_VREDRAW or CS_OWNDC;
  WinClass.hInstance:=hInst;              //程序的实例句柄.
//设置窗口消息处理函数.
  WinClass.lpfnWndProc:=@MyWinProc;           //窗口过程.
  WinClass.cbClsExtra:=0;                     //以下两个域用于在类结构和Window
s内部保存的窗口结构
  WinClass.cbWndExtra:=0;                     //中预留一些额外空间.
  WinClass.hIcon:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
  WinClass.hIconsm:=LoadIcon(hInstance,MakeIntResource('MAINICON'));
  WinClass.hCursor:=LoadCursor(0,IDC_Arrow);
//GetStockObject 获取一个图形对象,在这里是获取绘制窗口背景的刷子,返回一个白色刷
子的句柄.
  WinClass.hbrBackground:=HBRUSH(GetStockObject(white_Brush));
  WinClass.lpszMenuName:=nil;                 //指定窗口类菜单.

//向Windows 注册窗口类.
  if RegisterClassEx(WinClass)=0 then
  begin
    MessageBox(0,'Registeration Error!','SDK/API',MB_OK);
    Exit;
  end;

//建立窗口对象.
  hWnd:=CreateWindowEx(
                 WS_EX_OVERLAPPEDWINDOW,                 //扩展的窗口风格.
                 WinClass.lpszClassName,                 //类名.
                 'Hello Window',                         //窗口标题.
                 WS_OVERLAPPEDWINDOW,                    //窗口风格.
                 CW_USEDEFAULT,                          //窗口左上角相对于屏幕
左上角的初始位置x.
                 0,                                      //....右y.
                 CW_USEDEFAULT,                          //窗口宽度x.
                 0,                                      //窗口高度y.
                 0,                                      //父窗口句柄.
                 0,                                      //窗口菜单句柄.
                 hInst,                                  //程序实例句柄.
                 nil);                                   //创建参数指针.
  if hWnd<>0 then
     begin
       ShowWindow(hWnd,SW_SHOWNORMAL);        //显示窗口.
       UpdateWindow(hWnd);                    //指示窗口刷新自己.
       SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);

     end
  else
     MessageBox(0,'Window not Created!','SDK/API',MB_OK);

//主消息循环程序.  
  while GetMessage(Msg,0,0,0) do
  begin
    TranslateMessage(Msg);                   //转换某些键盘消息.
    DispatchMessage(Msg);                    //将消息发送给窗口过程.
  end;
end.

>其实Windows 编程是每个学写程序的人都要掌握的,学Delphi时也最好能先学习Windos编
程(最少要知道).以上代码虽说不如在Delphi中直接来个New->Form来的快,但它能告诉你本
质的东西.能让你更好的了解消息循环以及其他.而这些正是让New出来的窗体给掩盖的部分
.
>注:以上代码是我从Windows 程序设计上通过C++语法直译过来的(),测试后没有问题.若我
的注解有什么错误的地方,请各位指正!^_^
 
hottey 于2004-5-19
作者网站: http://asp.itdrp.com/hottey  (附例程)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
program Hackdiy; uses Windows, Messages; var TheMessage: TMsg; const ClassName = 'MainForm_FOrm1'; // 窗体过程回调函数 function FormProc(hForm, MsgID, WParam, LParam: LongWord): LongWord; stdcall; const {$J+} TempFont: DWORD = 0; {$J-} ControlID1 = 1; ControlID2 = 2; ControlID3 = 3; var EditText: array[0..30] of Char; begin Result := DefWindowProc(hForm, MsgID, WParam, LParam); // 标准处理 case MsgID of WM_CREATE: begin TempFont := CreateFont(12, 6, 0, 0, FW_EXTRALIGHT, Byte(FALSE), Byte(FALSE), Byte(FALSE), GB2312_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, '宋体'); CreateWindowEx(0, 'BUTTON', '退出程序', WS_CHILD or WS_VISIBLE, 60, 20, 70, 25, hForm, ControlID2, HInstance, nil); SendMessage(GetDlgItem(hForm, ControlID2), WM_SETFONT, TempFont, 1); end; WM_COMMAND: begin if (HIWORD(wParam) = BN_CLICKED) then // 单击按钮 begin case LOWORD(wParam) of // 控件ID ControlID1: begin end; ControlID2: SendMessage(hForm, WM_CLOSE, 0, 0); end; end; end; WM_DESTROY: begin PostQuitMessage(0); DeleteObject(TempFont); end; end; end; // 注册窗体类 procedure Register_MainForm; var FormClass: TWndClass; begin FormClass.Style := CS_HREDRAW or CS_VREDRAW; FormClass.lpfnWndProc := @FormProc; FormClass.cbClsExtra := 0; FormClass.cbWndExtra := 0; FormClass.hInstance := SysInit.HInstance; FormClass.hIcon := LoadIcon(HInstance, 'Cool'); FormClass.hCursor := LoadCursor(0, IDC_ARROW); FormClass.hbrBackground := COLOR_WINDOW; FormClass.lpszMenuName := nil; FormClass.lpszClassName := ClassName; RegisterClass(FormClass); end; // 注销窗体类 procedure Unregister_MainForm; begin UnregisterClass(ClassName, HInstance); end; // 建立主窗体 procedure Create_MainForm; begin CreateWindowEx(WS_EX_TOPMOST, ClassName, 'Form1', WS_VISIBLE or WS_TILED or WS_SYSMENU or WS_MINIMIZEBOX or WS_SIZEBOX, 200, 200, 200, 100, 0, 0, HInstance, nil); end; begin Register_MainForm; Create_MainForm; while GetMessage(TheMessage, 0, 0, 0) do begin TranslateMessage(TheMessage); DisPatchMessage(TheMessage); end; end.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值