{*************************************************************************}
{                                                                         }
{ MicroWin.pas - functions to create windows within API only              }
{                                                                         }
{ This file is a part of the project:                                     }
{   Micro Tools - modules to keep size of applications small              }
{                                                                         }
{ © Parfenov Alexey Andreevich, 2007                                      }
{                                                                         }
{ e-mail: AlkatrazStudio@yandex.ru                                        }
{                                                                         }
{ This library is free software; you can redistribute it and/or           }
{ modify it under the terms of the GNU General Public License             }
{ as published by the Free Software Foundation; either version 2.1 of     }
{ the License, or (at your option) any later version.                     }
{                                                                         }
{ This library is distributed in the hope that it will be useful,         }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of          }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU        }
{ General Public License for more details.                                }
{                                                                         }
{ You may read GNU General Public License at:                             }
{   http://www.gnu.org/copyleft/gpl.html                                  }
{                                                                         }
{ last modified: 25.04.2009                                               }
{*************************************************************************}
unit MicroWin;     

interface

uses windows,microutils;

function MakeWindow(WndClass:string;WndProc:Pointer):HWND;
function DefaultWindowProc(Hwn,mesg,wpr,lpr:longint):longint;stdcall;
procedure InfiniteLoop;
procedure PopupMenuAtCursor(menu:HMENU;Handle:HWND=0);

implementation

function DefaultWindowProc(Hwn,mesg,wpr,lpr:longint):longint;stdcall;
begin
  result:=DefWindowProc(Hwn,mesg,wpr,lpr);
end;

function MakeWindow(WndClass:string;WndProc:Pointer):HWND;
var
  WindowClass:TWndClass;
  Instance:HWND;
begin
  Instance:=GetModuleHandle(nil);
  ZeroMemory(@WindowClass,sizeof(WindowClass));
  with WindowClass do
  begin
    style:=cs_hredraw or cs_vredraw;
    if WndProc=nil then wndproc:=@DefaultWindowProc;
    lpfnWndProc:=WndProc;
    hInstance:=instance;
    hbrBackground:=color_btnface;
    lpszClassName:=PAnsiChar(WndClass);
    hCursor:=loadcursor(0,idc_arrow);
  end;
  RegisterClass(WindowClass);
  result:=CreateWindowEx(
    0,PAnsiChar(WndClass),'',WS_POPUP,1,1,200,200,0,0,Instance,nil);
end;

procedure InfiniteLoop;
var
  msg:TMsg;
begin
  while GetMessage(msg,0,0,0) do
  begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
end;

procedure PopupMenuAtCursor(menu:HMENU;Handle:HWND=0);
var
  p:TPoint;
begin
  GetCursorPos(p);
  TrackPopupMenu(Menu,TPM_RIGHTALIGN or TPM_RIGHTBUTTON,p.x,p.y,0,Handle,nil);
end;

end.
