用Delphi编写一个Win32的窗体,C++的代码是:
#include <windows.h>int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
LRESULT WINAPI WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
MSG msg; WNDCLASS wc;
if(!hPrevInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = "Sdk 实例";
wc.lpszMenuName = NULL;
RegisterClass(&wc);
} hWnd = CreateWindow("Sdk 实例",
"Win32 SDK 应用程序实例",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
}LRESULT WINAPI WndProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
HDC hDC;
switch(msg)
{
case WM_PAINT:
hDC = GetDC(hWnd);
TextOut(hDC,10,100,"你好,SDK编程!",sizeof("你好,SDK编程!") - 1);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
在Delphi6、7下用什么可以便写完然后能编译?谢谢!
#include <windows.h>int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
LRESULT WINAPI WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
MSG msg; WNDCLASS wc;
if(!hPrevInstance)
{
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = "Sdk 实例";
wc.lpszMenuName = NULL;
RegisterClass(&wc);
} hWnd = CreateWindow("Sdk 实例",
"Win32 SDK 应用程序实例",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL); ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
}LRESULT WINAPI WndProc(HWND hWnd,
UINT msg,
WPARAM wParam,
LPARAM lParam)
{
HDC hDC;
switch(msg)
{
case WM_PAINT:
hDC = GetDC(hWnd);
TextOut(hDC,10,100,"你好,SDK编程!",sizeof("你好,SDK编程!") - 1);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc(hWnd,msg,wParam,lParam);
}
在Delphi6、7下用什么可以便写完然后能编译?谢谢!
解决方案 »
- delphi编程实现动态创建database link
- DBComboBox,如何才能实现,既可以输入中英文内容,又可以选择下拉内容呢
- 关于三层结构编程的DLL封装问题?
- 急求:EXCEL中,如何把内容直接写进文件,而不显示Excel这个软件?
- 用adoquery进行select查询,因为select太长,出现问题了,怎么办?
- 如何在ListView中添加子项?
- 那位大哥知道,TDBgridEh打印预览的时候为什么不显示Footer内容哪 ?
- dbgrideh关于bit字段的问题
- 数据库高手在等你们来啦!!!!!!在Delphi中怎样用代码向SQLServer中注册新建一个数据库!!!!
- 把delphi的数据库转换成outlook中的通讯簿
- 咨询根据数据库表名动态更新下拉框的函数的参数传送问题
- 菜鸟求助(代码改错)
unit Win32;interfaceuses
Windows;implementationfunction WinMain(hInstance:HINSTANCE,
hPrevInstance:HINSTANCE,
lpCmdLine:LPSTR,
nCmdShow:Integer):Integer;
begin
MessageBox(nil,
PChar('开始学习SDK编程',
PChar('消息对话框'),
MB_OK);
Result:= 0;
end;
end.[Error] Win32.pas(10): 'HINSTANCE' is not a type identifier
[Error] Win32.pas(11): 'HINSTANCE' is not a type identifier
[Error] Win32.pas(12): ';', ')' or '=' expected but ',' found
[Error] Win32.pas(15): Incompatible types: 'HWND' and 'Pointer'
为什么不知道HINSTANCE这个类型?不是在SysInit.pas文件里的吗?
program fontsexp;uses
Windows,
Messages,
FontStr,
SysUtils;const
AppName = 'FontsExp';
WM_STARTFONTS = (WM_USER + 0);
WM_NEWFONT = (WM_USER + 1);
ID_LISTBOX = 255;
CM_INFO = 256;
CM_EXIT = 257;
type
PEnumStruct = ^TEnumStruct;
TEnumStruct = Record
Count: Integer;
hWindow: HWnd;
end;var
TextMetrics: TTextMetric;
TextFace: string;
hFontList, hNumFonts: HWnd;
hFontName, hAlphaEdit: HWnd;
ButtonWindows: array[0..2] of HWnd;
TheFont: HFONT;function FontCallBack( lpnlf: PEnumLogFont;
lpntm: PNewTextMetric;
FontType: Integer;
Data: PEnumStruct ): Integer; stdcall;
var
s: string;
begin
s := (lpnlf.elfLogFont.lfFaceName);
SendMessage( Data.hWindow, LB_ADDSTRING, 0, LongInt( s ) );
Inc( Data.Count );
Result := 1;
end;function DescribeFontCallBack( lpnlf: PEnumLogFont;
lpntm: PNewTextMetric;
FontType: Integer;
Data: PEnumStruct ): Integer; stdcall;
begin
SendMessage( Data.hWindow, WM_NEWFONT, 0, LongInt( @lpnlf.elfLogFont ) );
Result := 1;
end;procedure HandleMetrics( TextMetrics: TTextMetric );
var
i: Integer;
flag: array[0..2] of integer;
begin
for i := 0 to 2 do flag[i] := 0;
if (TextMetrics.tmPitchAndFamily and TMPF_TRUETYPE) = TMPF_TRUETYPE then
flag[0] := 1;
if TextMetrics.tmWeight > 600 then flag[1] := 1;
if TextMetrics.tmItalic <> 0 then flag[2] := 1;
for i := 0 to 2 do
SendMessage( ButtonWindows[i], BM_SETCHECK, flag[i], 0 )
end;procedure On_Create( Window: HWnd; var Meg: TWMCreate );
const
Titles: array[0..2] of string = ('TrueType', 'Heavy', 'Italic' );
var
i: Integer;
begin
SetLength( TextFace, 250 );
hFontList := CreateWindow( 'ListBox', nil,
WS_CHILD or WS_VISIBLE or LBS_STANDARD,
9, 30, 201, 180, Window, HMENU( ID_LISTBOX ),
hInstance, nil );
hNumFonts := CreateWindow( 'Static', nil,
WS_CHILD or WS_VISIBLE or WS_BORDER,
10, 10, 200, 20, Window, 0, hInstance, nil );
hFontName := CreateWindow( 'Edit', nil, WS_CHILD or WS_VISIBLE or
WS_BORDER or ES_LEFT or ES_READONLY,
260, 10, 350, 70, Window, 0, hInstance, nil );
hAlphaEdit := CreateWindow( 'Edit', nil, WS_CHILD or WS_VISIBLE or WS_HSCROLL or
WS_BORDER or ES_LEFT or ES_AUTOHSCROLL or ES_MULTILINE,
260, 90, 350, 70, Window, -0, hInstance, nil );
for i := 0 to 2 do
ButtonWindows[i] := CreateWindow( 'Button', PChar( Titles[i] ),
WS_CHILD or WS_VISIBLE or BS_CHECKBOX,
260 + ( i * 125 ), 180, 100, 35,
Window, 0, hInstance, nil );
TheFont := 0;
PostMessage( Window, WM_STARTFONTS, 0, 0 );
end;procedure On_Destroy( hWindow: HWnd; var Meg: TWMDestroy );
begin
if TheFont <> 0 then DeleteObject( TheFont );
PostQuitMessage( 0 ); // 向应用程序发送退出消息 API
end;procedure ShowTheFont( hWindow: Hwnd );
var
Buffer: string;
EnumStruct: TEnumStruct;
Index: LResult;
DC: HDC;
Alpha: string;
SaveIt: HFont;
begin
SaveIt := 0;
SetLength( Buffer, 250 );
Alpha := '中国1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
DC := GetDC( hWindow );
Index := SendMessage( hFontList, LB_GETCURSEL, 0, 0 );
SendMessage( hFontList, LB_GETTEXT, Index, LPARAM( Buffer ) );
EnumStruct.Count := 0;
EnumStruct.hWindow := hWindow;
if not EnumFontFamilies( DC, PChar( Buffer ), @DescribeFontCallBack,
LongInt( @EnumStruct ) ) then Exit;
if TheFont <> 0 then
SaveIt := SelectObject( DC, TheFont );
GetTextMetrics( DC, TextMetrics );
HandleMetrics( TextMetrics );
GetTextFace( DC, 150, PChar( TextFace ) );
if TheFont <> 0 then
SelectObject( DC, SaveIt );
ReleaseDC( hWindow, DC );
SendMessage( hFontName, WM_SETTEXT, 0, LPARAM( Buffer ) );
SendMessage( hFontName, WM_SETFONT, WPARAM( TheFont ), 0 );
SendMessage( hAlphaEdit, WM_SETTEXT, 0, LPARAM( Alpha ) );
SendMessage( hAlphaEdit, WM_SETFONT, WPARAM( TheFont ), 0 );
end;procedure ShowFontString( hWindow: HWnd );
var
s: string;
begin
s := GetFontString( s, TextMetrics, TextFace );
MessageBox( hWindow, PChar( s ), 'Info', MB_OK );
end;procedure On_Command( hWindow: HWnd; var Meg: TWMCommand );
begin
case Meg.ItemID of
ID_LISTBOX:
case Meg.NotifyCode of
LBN_SELCHANGE: ShowTheFont( hWindow );
LBN_DBLCLK: ShowFontString( hWindow );
end;
CM_INFO: ShowFontString( hWindow );
CM_EXIT: PostQuitMessage( 0 );
end;
end;procedure NewFont( lParam: LongInt );
var
LogFont: PLogFont;
Font: TLogFont;
begin
if TheFont <> 0 then
DeleteObject( TheFont );
LogFont := PLogFont( lParam );
Font := LogFont^;
// Font.lfHeight := 12;
// Font.lfWidth := 6;
TheFont := CreateFontIndirect( Font );
end;procedure StartFont( hWindow: HWnd );
var
DC: HDC;
EnumStruct: TEnumStruct;
s: string;
begin
DC := GetDC( hWindow );
EnumStruct.Count := 0;
EnumStruct.hWindow := hFontList;
if not EnumFontFamilies( DC, nil, @FontCallBack, LongInt( @EnumStruct ) ) then
ReleaseDC( hWindow, DC );
s := Format( 'There are %d fonts available', [EnumStruct.Count] );
SetWindowText( hNumFonts, PChar( s ) );
SetFocus( hFontList );
SendMessage( hFontList, LB_SETCURSEL, 0, 0 );
ShowTheFont( hWindow );
end;function WindowProc( hWindow: HWND; aMessage: UINT; wParam,
lParam: longint ): longint; stdcall; export;
var
aMeg: TMessage;
begin
aMeg.Msg := aMessage;
aMeg.WParam := wParam;
aMeg.LParam := lParam;
aMeg.Result := 0;
case aMessage of
WM_CREATE: On_Create( hWindow, TWMCreate( aMeg ) );
WM_DESTROY: On_Destroy( hWindow, TWMDestroy( aMeg ) );
WM_COMMAND: On_Command( hWindow, TWMCommand( aMeg ) );
WM_NEWFONT: NewFont( lParam );
WM_STARTFONTS: StartFont( hWindow );
// 调用缺省的窗口函数 API
else aMeg.Result := DefWindowProc( hWindow, aMessage, wParam, lParam );
end;
Result := aMeg.Result;
end;function WinRegister: Boolean;
var
WCL: TWndClass;
begin
WCL.hInstance := hInstance;
with WCL do
begin
Style := CS_HREDRAW or CS_VREDRAW;
lpfnWndProc := @WindowProc;
hIcon := LoadIcon( hInstance, IDI_APPLICATION );
hCursor := LoadCursor( 0, IDC_ARROW );
hbrBackGround := hBrush( COLOR_WINDOW );
lpszMenuName := 'FontMenu';
lpszClassName := AppName;
cbClsExtra := 0;
cbWndExtra := 0;
end;
Result := RegisterClass( WCL ) <> 0; // 登记窗口类 API
end;function WinCreate: HWnd;
begin
Result := CreateWindow( AppName, 'Windows API 字体演示程序 for Delphi',
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
625, 275, HWND_DESKTOP, 0, hInstance, nil );
end;function ExecWin( hWindow: HWND ): longint;
var
aMessage: TMsg;
begin
ShowWindow( hWindow, CmdShow ); // 显示窗口 API
UpdateWindow( hWindow ); // 重新显示窗口 API
while GetMessage( aMessage, 0, 0, 0 ) do // 取消息 API
begin
TranslateMessage( aMessage ); // 转换字符消息 API
DispatchMessage( aMessage ); // 发送消息到指定窗口 API
end;
Result := aMessage.wParam;
end;var
hWindow: HWnd;
begin
if not WinRegister then
begin
MessageBox( 0, 'Register Failed', nil, mb_Ok );
Exit;
end;
hWindow := WinCreate;
if hWindow = 0 then
begin
MessageBox( 0, 'Create Failed', nil, mb_Ok );
Exit;
end;
Halt( ExecWin( hWindow ) );
end.
unit Win32;interfaceuses
Windows,Messages,SysUtils;const
AppName = 'HelloWorld';
type
//定义一个自己的类
TMyClass = class
public
//有一个窗体注册函数
function WinRegister:Boolean;
//创建窗体句柄函数
function WinCreate:HWND;
//执行窗体函数
function ExecWin(hWnd:HWND):LongInt;
//消息循环函数
function WindowProc(hWnd:HWND;
aMessage:UINT;
wParam,lParam:LongInt):LongInt;stdcall;export;
//消息销毁函数
procedure On_Destroy(hWnd: HWnd; var Meg: TWMDestroy);
end;implementationfunction TMyClass.WinRegister:Boolean;
var
WCL: TWndClass;
begin
WCL.hInstance := hInstance;
with WCL do
begin
Style := CS_HREDRAW or CS_VREDRAW;
lpfnWndProc := @WindowProc;
hIcon := LoadIcon( hInstance, IDI_APPLICATION );
hCursor := LoadCursor( 0, IDC_ARROW );
hbrBackGround := hBrush( COLOR_WINDOW );
lpszMenuName := 'FontMenu';
lpszClassName := AppName;
cbClsExtra := 0;
cbWndExtra := 0;
end;
Result := RegisterClass( WCL ) <> 0; // 登记窗口类 API
end;function TMyClass.WinCreate:HWND;
begin
Result := CreateWindow( AppName, 'Win32 SDK for Delphi',
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
625, 275, HWND_DESKTOP, 0, hInstance, nil );
end;function TMyClass.ExecWin(hWnd:HWND):LongInt;
var
aMessage: TMsg;
begin
ShowWindow( hWnd, CmdShow ); // 显示窗口 API
UpdateWindow( hWnd ); // 重新显示窗口 API
while GetMessage( aMessage, 0, 0, 0 ) do // 取消息 API
begin
TranslateMessage( aMessage ); // 转换字符消息 API
DispatchMessage( aMessage ); // 发送消息到指定窗口 API
end;
Result := aMessage.wParam;
end;function TMyClass.WindowProc(hWnd:HWND;
aMessage:UINT;
wParam,lParam:LongInt):LongInt;stdcall;export;
var
aMeg: TMessage;
begin
aMeg.Msg := aMessage;
aMeg.WParam := wParam;
aMeg.LParam := lParam;
aMeg.Result := 0;
case aMessage of
//WM_CREATE: On_Create( hWindow, TWMCreate( aMeg ) );
WM_DESTROY: On_Destroy( hWnd, TWMDestroy( aMeg ) );
//WM_COMMAND: On_Command( hWindow, TWMCommand( aMeg ) );
//WM_NEWFONT: NewFont( lParam );
//WM_STARTFONTS: StartFont( hWindow );
// 调用缺省的窗口函数 API
else aMeg.Result := DefWindowProc( hWnd, aMessage, wParam, lParam );
end;
Result := aMeg.Result;
end;procedure TMyClass.On_Destroy(hWnd: HWnd; var Meg: TWMDestroy);
begin
PostQuitMessage(0);
end;end.
我只想显示一个窗口,不要显示什么Font文字什么的,所以我注释了一些,一下是错误:
[Error] Win32.pas(38): Variable required
[Warning] Win32.pas(53): Constant expression violates subrange bounds
[Warning] Win32.pas(53): Constant expression violates subrange bounds
[Warning] Win32.pas(61): Symbol 'CmdShow' is specific to a platform
麻烦楼上看下。我快改1个月了