用Api创建窗体
2009-11-14 16:25 HYrz的菜园子 阅读(192) 评论(0) 编辑 收藏 举报![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
program WindowFromApi;
{ an example to how to create and design window with a button inside from API}
{ berzek aka alcap0rn }
// 01/07/09 : dd/mm/yy //
uses
Windows,
Messages,
dialogs,
sysutils;
const
mButton1 = 1;
mButton2 = 2;
mEdit = 3;
function WndProc(WindowHandler:HWND;Msj:UINT;Parameter1:WPARAM;Parameter2:LPARAM):LRESULT; stdcall;
Begin
Case Msj of // this are the events on the windows like keyboard,mouse etc..
WM_CLOSE: Begin
PostMessage(WindowHandler,WM_QUIT,0,0); // when u close the fuckin window
end;
WM_KEYDOWN : ShowMessage ('a key pressed');
WM_LBUTTONDOWN : ShowMessage ('mouse click pressed over the window');
WM_COMMAND : Begin
case Parameter1 of
mButton1 : ShowMessage ('hello Im the fucking button 1');
mButton2 : ShowMessage ('hello Im the fucking button 2');
end;
end;
else
Begin
wndProc:= DefWindowProc(WindowHandler,Msj,Parameter1,Parameter2);
end;
end;
End;
procedure DefineWinClass (var WindowClass:TWndClass);
Begin
WindowClass.style:=CS_HREDRAW or CS_VREDRAW or CS_OWNDC or CS_DROPSHADOW;
WindowClass.lpszClassName:='₧berzekakaalcaporn₧';
WindowClass.lpfnWndProc:=@WndProc;
WindowClass.cbClsExtra:=0;
WindowClass.cbWndExtra:=0;
WindowClass.hInstance:=0; // window instance.
WindowClass.hIcon:=LoadIcon(0,IDI_WINLOGO); // window icon
WindowClass.hCursor:=LoadCursor(0,IDC_ARROW); // cursor type
WindowClass.hbrBackground:=CreateSolidBrush(RGB(0,0,0)); //window color
WindowClass.lpszMenuName:=nil; // resourcename
End;
procedure CreateBtn(WindowHandler : hwnd; x,y,mlong,mheight : integer ; ButtonNum:Integer);
var
HandleButton : THandle;
HandleMenu : HMenu;
Begin
HandleMenu := ButtonNum;
HandleButton :=CreateWindowEX
(
0,
'BUTTON',
pchar('Dude '+intToStr(ButtonNum)), // Title
WS_VISIBLE or WS_CHILD or BS_PUSHBUTTON or BS_TEXT,
x,y,
mlong,mheight,
WindowHandler,
HandleMenu,
0,
nil);
end;
Procedure CreateEdit(WindowHandler: hwnd; x,y,Mlong,mheight:integer; EditNum:integer);
var
HandleEdit: THandle;
HandleMenu: HMenu;
begin
HandleMenu := EditNum;
HandleEdit :=CreateWindowEX
(
0,
'EDIT', // DWORD dwExStyle
//LPCTSTR lpClassName
pchar('write here little bitch'), // value
WS_CHILD or WS_VISIBLE or BS_FLAT, //DWORD dwStyle,
x,y, //int x,int y
mlong,mheight, // int nWidth,int nHeight
WindowHandler, // HWND hWndParent
HandleMenu, //HMENU hMenu
0, // HINSTANCE hInstance,
nil); // LPVOID lpParam
end;
procedure CreateWin(var WindowHandler:HWND);
var
WindowClass:TWndClass;
ExtendedWindowStyle,WindowStyle:Dword;
Begin
{we define the class window }
DefineWinClass(WindowClass);
if RegisterClass(WindowClass)=0 then
MEssageBox(0,'cant get the class ','WARNING',MB_OK);
ExtendedWindowStyle := WS_EX_APPWINDOW; //or WS_EX_WINDOWEDGE;
WindowStyle := WS_OVERLAPPEDWINDOW;// or WS_CLIPSIBLINGS or WS_CLIPCHILDREN;
WindowHandler:=CreateWindowEX
(
ExtendedWindowStyle,
'₧berzekakaalcaporn₧',
'Example of Window From API @ berzek A.K.A Alcap0rn', // Title
WindowStyle,
400,300,
379,274,
0,
0,
0,
nil);
If WindowHandler=0 then MessageBox(0,'something bad ?, find yourself!','WARNING ERROR',MB_OK);
ShowWindow(WindowHandler,SW_SHOW);
SetFocus(WindowHandler);
End;
var
Msj:TMsg;
TheResult:Boolean;
WindowHandler:HWND;
begin
CreateWin(WindowHandler);
CreateBtn(WindowHandler,100,100,80,40,mButton1);
CreateBtn(WindowHandler,200,100,80,40,mButton2);
CreateEdit(WindowHandler,100,150,250,30,mEdit);
TheResult:=Windows.GetMessage(Msj,WindowHandler,0,0);
while TheResult do
begin
TheResult:=Windows.GetMessage(Msj,WindowHandler,0,0);
TranslateMessage(Msj);
DispatchMessage(Msj);
end;
end.