创建表单而不使用VCL

创建表单而不使用VCL

本文介绍了创建表单而不使用VCL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个表单(使用 CreateWindow 函数),而不需要任何来自 VCL (或任何视觉控制)只使用Windows API。

I need to create a form (using CreateWindow functions) without any help from the VCL (or any visual control) only using the Windows API.

此表单将有一个 InputBox ,一个按钮 BitMap (如 TImage )。

This form will have an InputBox, a Button and a BitMap (like TImage does).

我无法在互联网上找到任何样本。有没有人知道一个可以下载除MSDN之外的示例的好地方?

I was not able to find any sample on internet. Does anyone know a good place where I can download a sample besides MSDN?

推荐答案

这是一个仅使用WinApi的示例应用程序使用静态,编辑和按钮创建Windows,我可以让您显示BitMap显示为您的运动。

This is a sample App which uses only the WinApi to create Windows with an static, edit and button, I let you the display of the BitMap display as exercise for you.

uses
  Windows,
  Messages,
  SysUtils;

var
  Msg        : TMSG;
  LWndClass  : TWndClass;
  hMainHandle: HWND;
  hButton    : HWND;
  hStatic    : HWND;
  hEdit      : HWND;
  hFontText  : HWND;
  hFontButton: HWND;

procedure ReleaseResources;
begin
  DestroyWindow(hButton);
  DestroyWindow(hStatic);
  DestroyWindow(hEdit);
  DeleteObject(hFontText);
  DeleteObject(hFontButton);
  PostQuitMessage(0);
end;

function WindowProc(hWnd,Msg:Longint; wParam : WPARAM; lParam: LPARAM):Longint; stdcall;
begin
  case Msg of
      WM_COMMAND: if lParam = hButton then
                    MessageBox(hMainHandle,'You pressed the button Hello', 'Hello',MB_OK or MB_ICONINFORMATION);
      WM_DESTROY: ReleaseResources;
  end;
  Result:=DefWindowProc(hWnd,Msg,wParam,lParam);
end;

begin
  //create the window
  LWndClass.hInstance := hInstance;
  with LWndClass do
    begin
      lpszClassName := 'MyWinApiWnd';
      Style         := CS_PARENTDC or CS_BYTEALIGNCLIENT;
      hIcon         := LoadIcon(hInstance,'MAINICON');
      lpfnWndProc   := @WindowProc;
      hbrBackground := COLOR_BTNFACE+1;
      hCursor       := LoadCursor(0,IDC_ARROW);
    end;

  RegisterClass(LWndClass);
  hMainHandle := CreateWindow(LWndClass.lpszClassName,'Window Title', WS_CAPTION or WS_MINIMIZEBOX or WS_SYSMENU or WS_VISIBLE, (GetSystemMetrics(SM_CXSCREEN) div 2)-190,
      (GetSystemMetrics(SM_CYSCREEN) div 2)-170, 386,200,0,0,hInstance,nil);

  //Create the fonts to use
  hFontText := CreateFont(-14,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH or FF_SWISS,'Tahoma');
  hFontButton := CreateFont(-14,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH or FF_SWISS,'Tahoma');

  //create the static
  hStatic:=CreateWindow('Static','This is static text, like a TLabel',WS_VISIBLE or WS_CHILD or SS_LEFT, 10,10,360,44,hMainHandle,0,hInstance,nil);
  SendMessage(hStatic,WM_SETFONT,hFontText,0);

  //create the edit
  hEdit:=CreateWindowEx(WS_EX_CLIENTEDGE,'Edit','This a Edit like and TEdit', WS_VISIBLE or WS_CHILD or ES_LEFT or ES_AUTOHSCROLL,10,35,360,23,hMainHandle,0,hInstance,nil);
  SendMessage(hEdit,WM_SETFONT,hFontText,0);

  //create the button
  hButton:=CreateWindow('Button','Hello', WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON or BS_TEXT, 10,130,100,28,hMainHandle,0,hInstance,nil);
  SendMessage(hButton,WM_SETFONT,hFontButton,0);

  //message loop
  while GetMessage(Msg,0,0,0) do
  begin
    TranslateMessage(Msg);
    DispatchMessage(Msg);
  end;

end.

这篇关于创建表单而不使用VCL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:12