1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
unit bsd_api;
interface
uses
Windows,
Messages;
procedure InitApplication;
procedure ExecuteApplication;
procedure FinalizeApplication;
implementation
var
WndClass: TWndClass;
HAppInstance: integer;
Msg: TMsg;
MainForm, Button1, Edit1: HWND;
//
Bitmap: TBitmap;
procedure Button1Click;
var
EditText: array[0..255] of char;
begin
GetWindowText(Edit1, EditText, 256);
// Display message box
MessageBox(MainForm,
PChar('You typed: ' + EditText),
'Message',
MB_APPLMODAL OR MB_ICONINFORMATION OR MB_OK);
end;
procedure CreateControls;
var
hControlFont: HFONT;
lfControl: TLogFont;
begin
// Create all controls (Edit1, Button1)
// See the help file for a description of the style flags
Edit1 := CreateWindowEx(WS_EX_CLIENTEDGE, // Extended style
'EDIT', // EDIT creates an edit box
'Edit1', // Name of window - also the text that will be in it
WS_CHILD OR WS_VISIBLE OR ES_AUTOHSCROLL OR ES_NOHIDESEL, // style flags
8, 16, 160, 21, // Position and size
MainForm, // Parent window
0, // Menu - none because it's an edit box(!)
HAppInstance, // Application instance
nil); // No creation data
Button1 := CreateWindow('BUTTON', // BUTTON creates an button, obviously
'Show Message', // Name of window - also the text that will be in it
WS_CHILD OR WS_VISIBLE OR BS_PUSHBUTTON OR BS_TEXT, // style flags
8, 40, 96, 25, // Position and size
MainForm, // Parent window
0, // Menu - none because it's a button
HAppInstance, // Application instance
nil); // No creation data
// Set up the font
{ Calculate font height from point size - they are not the same thing!
The first parameter of MulDiv is the point size. }
// lfControl.lfHeight := -MulDiv(8, GetDeviceCaps(GetDC(0), LOGPIXELSY), 96);
lfControl.lfFaceName :='Tahoma';
lfControl.lfHeight :=13;
lfControl.lfWeight :=0;
lfControl.lfItalic :=0;
lfControl.lfUnderline :=0;
lfControl.lfPitchAndFamily :=0;
lfControl.lfStrikeOut :=0;
lfControl.lfOutPrecision :=0;
lfControl.lfEscapement :=0;
lfControl.lfOrientation :=0;
lfControl.lfCharSet :=0;
lfControl.lfWidth :=0;
lfControl.lfClipPrecision :=0;
lfControl.lfQuality :=0;
// Create the font
hControlFont := CreateFontIndirect(lfControl);
// Tell controls to set their fonts
SendMessage(Edit1, WM_SETFONT, hControlFont, 1);
SendMessage(Button1, WM_SETFONT, hControlFont, 1);
end;
function WindowProc(hwnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
begin
// This is the function Windows calls when a message is sent to the application
case uMsg of // Check which message was sent
WM_DESTROY: PostQuitMessage(0); // Otherwise app will continue to run
// ...
// Handle any other messages here
WM_ACTIVATE: SetFocus(Edit1);
WM_COMMAND:
begin
Result := 0; // Default return value for this message
if lParam = Button1 then
case wParam of
BN_CLICKED: Button1Click; // Button was clicked
else Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
end; // case wNotifyCode of
end; // case: WM_COMMAND
// ...
// Use default message processing
else Result := DefWindowProc(hwnd, uMsg, wParam, lParam);
end;
end;
procedure InitApplication;
begin
HAppInstance := HInstance;
// Set up window class
with WndClass do begin
Style := 0;
lpfnWndProc := @WindowProc; // See function above
cbClsExtra := 0; // no extra class memory
cbWndExtra := 0; // no extra window memory
hInstance := HAppInstance; // application instance
hIcon := 0; // use default icon
hCursor := LoadCursor(0, IDC_ARROW); // use arrow cursor
hbrBackground := COLOR_WINDOW; // standard window colour
lpszMenuName := nil; // no menu resource
lpszClassName := 'TMainForm';
end; // with WndClass
Windows.RegisterClass(WndClass); // Don't use Delphi's version of RegisterClass
// Create the window
MainForm := CreateWindow('TMainForm',
'Delphi API Demo', // window caption
WS_OVERLAPPEDWINDOW, // standard window style
CW_USEDEFAULT, CW_USEDEFAULT, // default position
320, 200, // size
0, // no owner window
0, // no menu
hInstance, // application instance
nil);
CreateControls; // See above
end;
procedure ExecuteApplication;//(Const Form:HWND)
begin
ShowWindow(MainForm, SW_SHOWMAXIMIZED); // make window visible
// Set up message loop
while GetMessage(Msg, 0, 0, 0) <> BOOL(FALSE) do begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
procedure FinalizeApplication;
begin
// Clean up code...
// I'm not sure these are actually needed
DestroyWindow(Edit1);
DestroyWindow(Button1);
end;
initialization
InitApplication;
finalization
FinalizeApplication;
end. |
Partager