We help IT Professionals succeed at work.

create small application

ZafodBiblbrox
on
Medium Priority
221 Views
Last Modified: 2010-04-06
hox can i create small exe file in Delphi my smallest exe file have 200kB but i need to create application about 20kB with form and 2 editbox and 2 buttons.

Zafod
Comment
Watch Question

You must use enterily the API, but I think you cant go less than 40 or 30 Kb.

See this example, its not from me.

 {Copyright ? MrRidk 2000-2001 All rights reserved}
 {ridk@hotmail.com -- http://www.mrridk.cjb.net}


program Ejemplo_Api;

uses
  Windows,
  Messages,
  SysUtils;

var
 Clase: TWndClassA; // Definimos Variables.
 Mensaje : tmsg; // Definimos la variable del mensaje de windows
 Label1, Label2, Label3, Edit1, Edit2, Edit3 : Integer; // Definimos variables
 Buton1, Buton2, Fuente, Handle, Instancia : Integer; // Definimos variables.
 Sumando1, Sumando2 : Pchar; // Definimos variables.
 Valor, Longitud : Integer; // Definimos Variables

function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall; // Definimos la variable encargada de procesar los mensajes.
begin
 Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
 If (uMsg = WM_DESTROY) then Halt; // Si damos al boton de la X, la aplicacion termina

 If (lParam = Buton2) And (uMsg = WM_COMMAND) then begin // Si el mensaje viene del boton y es la pulsacion de este . . .
  Halt; // Cerramos la Aplicacion.
 end;
 If (lParam = Buton1) And (uMsg = WM_COMMAND) then begin // Si el mensaje viene del boton y es la pulsacion de este . . .
  Longitud := GetWindowTextLength(Edit1); // Averiguamos cual es la longitud del texto de la primera edit
  GetMem(Sumando1,Longitud+1); // Cogemos memoria para el texto.
  GetWindowText(Edit1,Sumando1,Longitud+1); // COgemos el texto que hay en el edit y lo almacenamos en la variable Sumando1
  Longitud := GetWindowTextLength(Edit2); // Averiguamos cual es la longitud del texto de la segunda edit
  GetMem(Sumando2,Longitud+1); // Cogemos memoria para el texto.
  GetWindowText(Edit2,Sumando2,Longitud+1); // Cogemos el texto que hay en el edit y lo almacenamos en la variable Sumando2
  Valor := StrToInt(Sumando1) + StrToInt(Sumando2); // Realizamos la suma.
  SetWindowText(Edit3,Pchar(IntToStr(Valor))); // Ponemos el texto en el edit3
 end;
end;

begin
 Instancia := hInstance;
 { Damos a la variable instance, el handle que aplicara windows
   a nuestro programa }

 Fuente := CreateFont (-1, 0, 0, 0, 400, 0, 0, 0, DEFAULT_CHARSET,
                       OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
                       DEFAULT_QUALITY, DEFAULT_PITCH + FF_DONTCARE,
                       'MS Sans Serif');
     { Creamos la fuente que usaremos en nuestra aplicacion }

 with Clase do begin // Comenzamos a definir la clase de nuestro programa
   Style := CS_CLASSDC or CS_PARENTDC; // Definimos el estilo
   lpfnWndProc := @WindowProc; // La funcion que manejara los mensajes
   hInstance := Instancia; // La handle de nuestra aplicacion
   hbrBackGround := Color_btnFace+1; // El color de fondo
   lpszClassName := 'MrRidk'; // El nombre de la class
   hCursor := LoadCursor(0, IDC_ARROW); // El cursor que usara nuestra aplicacion
 end; // Fin

 RegisterClassA(Clase); // Registramos la clase en windows.

 Handle := CreateWindowEx(0,      'MrRidk','Ejemplo de Programa Con Api', WS_SYSMENU + WS_VISIBLE, 150,   150,   280,    140,   0,  0,  Instancia, Nil);
      {Creamos la ventana, Estilo, Clase,  Titulo,                        Estilo,                  Posicion,    Tama?o,      , El Padre de la ventana, El menu,  La hInstance, El lParam.}
{ ************************************************************************************************************ }

 Edit1 := CreateWindowEx(WS_EX_CLIENTEDGE,'Edit','3',WS_VISIBLE+WS_CHILD+BS_TEXT,90,10,175,20,Handle,0,Instancia,Nil);
      {Creamos la ventana, Estilo,         Tipo, Texto, Estilo,                  Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 Edit2 := CreateWindowEx(WS_EX_CLIENTEDGE,'Edit','2',WS_VISIBLE+WS_CHILD+BS_TEXT,90,31,175,20,Handle,0,Instancia,Nil);
      {Creamos la ventana, Estilo,         Tipo, Texto, Estilo,                  Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 Edit3 := CreateWindowEx(WS_EX_CLIENTEDGE,'Edit','5',WS_VISIBLE+WS_CHILD+BS_TEXT,90,52,175,20,Handle,0,Instancia,Nil);
      {Creamos la ventana, Estilo,         Tipo, Texto, Estilo,                  Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }
 
 Label1 := CreateWindow('Static','Sumando 1:',WS_VISIBLE + WS_CHILD, 10, 16,75,20,Handle,0,Instancia,Nil);
      {Creamos la ventana, Tipo, Texto,    Estilo,               Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 Label2 := CreateWindow('Static','Sumando 2:',WS_VISIBLE + WS_CHILD, 10, 37,75,20,Handle,0,Instancia,Nil);
      {Creamos la ventana, Tipo, Texto,    Estilo,               Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 Label3 := CreateWindow('Static','Resultado:',WS_VISIBLE + WS_CHILD, 10, 57,75,20,Handle,0,Instancia,Nil);
      {Creamos la ventana, Tipo, Texto,    Estilo,               Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 Buton1 := CreateWindow('Button','Sumar', WS_VISIBLE + WS_CHILD, 65, 85, 60, 20, Handle,0,Instancia,Nil);
      {Creamos la ventana, Tipo, Texto,    Estilo,               Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 Buton2 := CreateWindow('Button','Salir', WS_VISIBLE + WS_CHILD, 150, 85, 60, 20, Handle,0,Instancia,Nil);
     {Creamos la ventana, Tipo, Texto,    Estilo,               Posicion,Tama?o,Padre,Menu,hInstance,El lparam}
{ ************************************************************************************************************ }

 SendMessage(Label1,WM_SETFONT,Fuente,Label1); // Ponemos la fuente que antes hemos definido
 SendMessage(Label2,WM_SETFONT,Fuente,Label1); // Ponemos la fuente que antes hemos definido
 SendMessage(Label3,WM_SETFONT,Fuente,Label1); // Ponemos la fuente que antes hemos definido
 SendMessage(Buton1,WM_SETFONT,Fuente,Buton1); // Ponemos la fuente que antes hemos definido
 SendMessage(Buton2,WM_SETFONT,Fuente,Buton2); // Ponemos la fuente que antes hemos definido
 SendMessage(Edit1,WM_SETFONT,Fuente,Edit1); // Ponemos la fuente que antes hemos definido
 SendMessage(Edit2,WM_SETFONT,Fuente,Edit2); // Ponemos la fuente que antes hemos definido
 SendMessage(Edit3,WM_SETFONT,Fuente,Edit3); // Ponemos la fuente que antes hemos definido

 while (GetMessage(Mensaje, Handle, 0, 0)) do // Vamos tratando los mensajes.
  begin
   TranslateMessage(Mensaje);
   DispatchMessage(Mensaje);
  end;

Commented:
You have to be console based with NO visual class/components. The overheqad on the tform,tedit, and tbuttons you are using is going to kill you.

Commented:
There are ways of doing small applications, but all of them require you to get "dirty", KOL has ways of creating very small binaries with Delphi, by saying bye bye to your standard units like Forms, SysUtils and replacing them with their KOL substitutes, and creating all controls runtime.

http://bonanzas0.tripod.com/

I Don't know if this is the Autors homepage so you might want to search the net a little bit first, before downloading from there.

Regards
 //raidos

Commented:
KOL works... I have built some small exe's of 20kb or less using it. I recommend using KOL to create your small exe and them using the free exe compressor UPX to compress it even further :-)

I can send you an exeample KOL exe if you'd like to see a really small delphi app :-)

Commented:
Listenning,

Commented:
For really simple, GUI dependant apps, you might want to consider .HTA's

GL
Mike
Commented:
hello ZafodBiblbrox, since you want such a small size application you will have to use pure windows API. I have been doing it with Delphi 5, but it takes alot of time and frustration (all of the windows programming instruction for this type o thing is in C, just look at the windows API help) here is an App with 2 edit boxes and 2 buttons. I put in a few comments but you should look up each function like "CreateWindow()" in your windows API help. . ask me questions if you are interested in this code. Take out the main form in a newly created application so there are no forms. You will only be coding in your .dpr for this . . now remove the lines
  Application.Initialize
  Application.CreateForm(TForm1, Form1);
  Application.Run;

and  Forms form the uses, then use the following code

program SmallApp;

uses
  Windows,
  Messages;

{$R *.RES}

var
WinClass: TWndClass;
hAppHandle,hExitBut,hShowBut,hLabel1,hEdit1,
hEdit2,hBrush1,hFont1,hFont2,hFont3: HWND;
Msg: TMSG;
Rect1,Rect2,Rect3: TRect;

procedure ShutDown;
begin
  DestroyWindow(hShowBut);
  DestroyWindow(hExitBut);
  DestroyWindow(hLabel1);
  DestroyWindow(hEdit1);
  DestroyWindow(hEdit2);
{DestroyWindow is NOT really nessary
but I do it for insurance to clean up the OS}

  DeleteObject(hFont1);
  DeleteObject(hFont2);
  DeleteObject(hFont3);
  DeleteObject(hBrush1);
  PostQuitMessage(0);
  {do NOT exit your process here - Do NOT call Halt
  or ExitProcess(hInstance). .   Just call
  PostQuitMessage(0) and Let it fall through the
GetMessage Loop to destroy the windows System handles you
 created. Windows OS does things to terminate
  an App as the GetMessage loop ends with
  PostQuitMessage(0)}
end;

function WindowProc(hWnd,Msg,wParam,lParam:Longint):Longint; stdcall;
var
DC: HDC;
PS: TPaintStruct;
begin
{This is where the app comunicates with the windows OS, delphi
hides this from you but it is SO VERY IMPORTANT for a programmer
to know whats happening with these messages}
case Msg of
    WM_PAINT: begin
              DC := BeginPaint(hWnd, PS);
              FillRect(DC,Rect1,hBrush1);
              FillRect(DC,Rect2,hBrush1);
              FillRect(DC,Rect3,hBrush1);
              EndPaint(hWnd,PS);
              end;
    WM_COMMAND: if lParam = abs(hShowBut) then
                  MessageBox(hAppHandle,'This takes some time to learn'#13'But you will understand how windows uses Handles, messages and other important stuff',
                    'Monkey Wrench',MB_OK or MB_ICONEXCLAMATION)
                else if LParam=abs(hExitBut) then ShutDown;

    WM_DESTROY: ShutDown;
end; // case
Result:=DefWindowProc(hWnd,Msg,wParam,lParam);
{windows will auto handle most messages if you just call DefWindowProc(hWnd,Msg,wParam,lParam)}
end;

begin  // / / / / / / / / / / Main Begin
hBrush1 := CreateSolidBrush(GetSysColor(COLOR_ACTIVECAPTION));
hFont1:=CreateFont(
    -16,                           // Height
    0,                             // Width
    0,                             // Angle of Rotation
    0,                             // Orientation
    FW_BOLD,                       // Weight
    0,                             // Italic
    0,                             // Underline
    0,                             // Strike Out
    ANSI_CHARSET,               // Char Set
    OUT_TT_ONLY_PRECIS,            // Precision
    CLIP_DEFAULT_PRECIS,           // Clipping
    ANTIALIASED_QUALITY,               // Render Quality
    VARIABLE_PITCH or FF_SWISS,  // Pitch & Family
    'Times New Roman');              // Font Name

hFont2 := CreateFont(-14,0,0,0,0,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH or FF_SWISS,'MS Sans Serif');

hFont3 := CreateFont(-14,0,0,0,FW_BOLD,0,0,0,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,
   CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,VARIABLE_PITCH or FF_SWISS,'MS Sans Serif');

WinClass.hInstance := hInstance;
with WinClass do
  begin
    Style:=         CS_PARENTDC or CS_BYTEALIGNCLIENT;
    hIcon:=         LoadIcon(hInstance,'MAINICON');
    lpfnWndProc:=   @WindowProc;
    hbrBackground:= COLOR_BTNFACE+1;
    lpszClassName:= 'SmallApp Class';
    hCursor:=       LoadCursor(0,IDC_ARROW);
  end;

windows.RegisterClass(WinClass);

hAppHandle := CreateWindow(WinClass.lpszClassName,'A windows API program in Delphi',
    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);

GetClientRect(hAppHandle,Rect1);
SetRect(Rect2,6,Rect1.Bottom - 6,Rect1.Right - 6,Rect1.Bottom);
SetRect(Rect3,Rect1.Right - 6,0,Rect1.Right,Rect1.Bottom);
SetRect(Rect1,0,0,6,Rect1.Bottom);

hLabel1:=CreateWindow('Static','This is a program that has no Forms unit and uses only windows API to make this application function',WS_VISIBLE or WS_CHILD or SS_CENTER,
    8,6,360,44,hAppHandle,0,hInstance,nil);

SendMessage(hLabel1,WM_SETFONT,hFont1,0);

hEdit1:=CreateWindowEx(WS_EX_CLIENTEDGE,'Edit','Text in Edit1',
    WS_VISIBLE or WS_CHILD or ES_LEFT or ES_AUTOHSCROLL,
    10,55,360,23,hAppHandle,0,hInstance,nil);

SendMessage(hEdit1,WM_SETFONT,hFont2,0);

hEdit2:=CreateWindowEx(WS_EX_CLIENTEDGE,'Edit','more Text in Edit2',
    WS_VISIBLE or WS_CHILD or ES_LEFT or ES_AUTOHSCROLL,
    10,82,360,23,hAppHandle,0,hInstance,nil);

SendMessage(hEdit2,WM_SETFONT,hFont2,0);

hExitBut:=CreateWindow('Button','E X I T',
    WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON or BS_TEXT,
    275,130,64,28,hAppHandle,0,hInstance,nil);

SendMessage(hExitBut,WM_SETFONT,hFont3,0);

hShowBut:=CreateWindow('Button','Show a Message',
    WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON or BS_TEXT,
    53,130,138,28,hAppHandle,0,hInstance,nil);

SendMessage(hShowBut,WM_SETFONT,hFont3,0);

while GetMessage(Msg,0,0,0) do
  begin
    TranslateMessage(Msg);  // Translate any keyboard Msg's
    DispatchMessage(Msg);   // Send it to our WindowProc
  end;
{until you do it without the forms unit you may not realize how much delphi does behind the scenes}
end.

Author

Commented:
Thanx, it is what i needed


Zafod

Explore More ContentExplore courses, solutions, and other research materials related to this topic.