被Delphi惯坏了,发现写一个原生的Form这么麻烦
vc版本
<!---->
#include
<
windows.h
>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int
WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine,
int
iCmdShow)
{
static
TCHAR szAppName[]
=
TEXT (
"
HelloWin
"
) ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style
=
CS_HREDRAW
|
CS_VREDRAW ;
wndclass.lpfnWndProc
=
WndProc ;
wndclass.cbClsExtra
=
0
;
wndclass.cbWndExtra
=
0
;
wndclass.hInstance
=
hInstance ;
wndclass.hIcon
=
LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor
=
LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground
=
(HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName
=
NULL ;
wndclass.lpszClassName
=
szAppName ;
if
(
!
RegisterClass (
&
wndclass))
{
MessageBox (NULL, TEXT (
"
This program requires Windows NT!
"
),
szAppName, MB_ICONERROR) ;
return
0
;
}
hwnd
=
CreateWindow (szAppName,
//
window class name
TEXT (
"
The Hello Program
"
),
//
window caption
WS_OVERLAPPEDWINDOW,
//
window style
CW_USEDEFAULT,
//
initial x position
CW_USEDEFAULT,
//
initial y position
CW_USEDEFAULT,
//
initial x size
CW_USEDEFAULT,
//
initial y size
NULL,
//
parent window handle
NULL,
//
window menu handle
hInstance,
//
program instance handle
NULL) ;
//
creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while
(GetMessage (
&
msg, NULL,
0
,
0
))
{
TranslateMessage (
&
msg) ;
DispatchMessage (
&
msg) ;
}
return
msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch
(message)
{
case
WM_CREATE:
PlaySound (TEXT (
"
hellowin.wav
"
), NULL, SND_FILENAME
|
SND_ASYNC) ;
return
0
;
case
WM_PAINT:
hdc
=
BeginPaint (hwnd,
&
ps) ;
GetClientRect (hwnd,
&
rect) ;
DrawText (hdc, TEXT (
"
Hello, Windows 98! By ZhangSK
"
),
-
1
,
&
rect,
DT_SINGLELINE
|
DT_CENTER
|
DT_VCENTER) ;
EndPaint (hwnd,
&
ps) ;
return
0
;
case
WM_DESTROY:
PostQuitMessage (
0
) ;
return
0
;
}
return
DefWindowProc (hwnd, message, wParam, lParam) ;
}
Delphi版本
<!---->
program HelloWin;
uses
Windows,
Messages,
MMSystem,
SysUtils;
Const
AppName:String
=
'
HelloWin
'
;
Null:Integer
=
0
;
function WndProc(WindowHwnd:HWND;TheMessage:UINT;WPARAMS:wParam;LPARAMS:lParam):Integer;stdcall;
var
ClientDC:HDC;
ps:TPaintStruct;
ClientRect:TRect;
sUser, sPower: string;
begin
case
TheMessage of
WM_CREATE: begin
PlaySound(
'
hellowin.wav
'
,
null
,SND_FILENAME or SND_ASYNC);
Result:
=
0
;
end;
WM_PAINT: begin
ClientDc:
=
BeginPaint(WindowHwnd,ps);
GetClientRect(WindowHwnd,ClientRect);
DrawText(ClientDc,PChar(
'
Hello,Window98!
'
),
-
1
,ClientRect,DT_SINGLELINE or
DT_CENTER OR DT_VCENTER);
sUser :
=
'
ZhangSK
''
Testing
'
;
sPower :
=
'
POWERD BY DELPHI
'
;
TextOut(ClientDC,
5
,
5
, PChar(sUser), Length(sUser));
TextOut(ClientDC, ClientRect.Right
-
200
, ClientRect.Bottom
-
30
, PChar(sPower), Length(sPower));
Endpaint(Windowhwnd,ps);
Result:
=
0
;
end;
WM_DESTROY: begin
PostQuitMessage(
0
);
Result:
=
0
;
end;
else
Result:
=
DefWindowProc(WindowHwnd,TheMessage,WPARAMS,LPARAMS);
end;
end;
var
WinHwnd:HWND;
WinMsg:MSG;
WinClass:WNDCLASS;
ECode:DWORD;
EString:PChar;
begin
WinClass.style:
=
CS_HREDRAW OR CS_VREDRAW;
WinClass.lpfnWndProc:
=
@WndProc;
WinClass.cbClsExtra:
=
0
;
WinClass.cbWndExtra:
=
0
;
WinClass.hInstance:
=
hInstance;
WinClass.hIcon:
=
LoadIcon(NULL,IDI_APPLICATION);
WinClass.hCursor:
=
LoadCursor(Null,IDC_ARROW);
WinClass.hbrBackground:
=
HBRUSH(GetStockObject(WHITE_BRUSH));
WinClass.lpszMenuName:
=
nil;
WinClass.lpszClassName:
=
PChar(AppName);
if
(RegisterClass(WinClass)
=
0
) then
begin
MessageBox(
null
,
'
This application need WINDOWS platform
'
,
'
message
'
,MB_ICONERROR);
exit;
end;
WinHwnd:
=
CreateWindow(PChar(AppName),
'
First SDK Application
'
,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
0
,
0
,hInstance,nil);
if
Iswindow(WinHwnd)then
begin
ShowWindow(WinHwnd,SW_SHOWNORMAL);
updateWindow(WinHwnd);
end
else
begin
ECode:
=
GetLastError;
EString:
=
PChar(Inttostr(LoWord(ECode)));
Messagebox(
null
,EString,
'
Error
'
,MB_ICONERROR);
exit;
end;
while
(Getmessage(WinMsg,
null
,
0
,
0
))
do
begin
TranslateMessage(WinMsg);
DispatchMessage(WinMsg);
end;
UnregisterClass(PChar(AppName),hInstance);
end.