X
xslkim
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾,小弟正在学习 Direct for Delphi 编程,无赖碰到的第一个教学实例就出了问题,想了好几天也找不出原因来。还望各位大虾不吝赐教,小弟万分感谢! 问题是这样的一个Direct主表面和后表面翻转的程序,每个表面显示不同的文字。 但程序运行时,就只有后表面显示出来,而主表面就是Windows下面的窗口,而不是direct创建的表面。 不管怎么设置Tform1的属性都不能解决问题。以下是程序的源代码。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,DirectDraw, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure start;
procedure Timer1Timer(Sender: TObject);
private
FrontMsg,BackMsg: string;
//定义显示的字符串
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
FDirectDraw:IDirectDraw;
FPrimarySurface,FBackSurface:IDirectDrawSurface;
implementation
{$R *.dfm}
Procedure TForm1.start;
var
SurfaceDesc:TDDSURFACEDESC;
//定义表面数据结构
DDSCapS:TDDSCAPS;
DC:HDC;
begin
DirectDrawCreate(nil,FDirectDraw,nil);
//创建DirectDraw对象
//设置全屏和独占模式,屏幕大小和颜色数。
FDirectDraw.SetCooperativelevel(Handle,DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN);
FDirectDraw.SetDisplayMode(640,480,8);
//初始化表面数据结构
SurfaceDesc.dwSize:=sizeof(SurfaceDesc);
SurfaceDesc.dwFlags:=DDSD_CAPS or DDSD_BACKBUFFERCOUNT;
SurfaceDesc.ddsCaps.dwCaps:=DDSCAPS_PRIMARYSURFACE or
ddSCAPS_FLIP or DDSCAPS_COMPLEX ;
SurfaceDesc.dwBackBufferCount:=1;
//创建主面表
FDirectDraw.CreateSurface(SurfaceDesc,FPrimarySurface,nil);
//定义从属后表面
DDSCapS.dwCaps:=DDSCAPS_BACKBUFFER;
//创建后表面
FPrimarySurface.GetAttachedSurface(DDSCapS,FBackSurface);
//设置主表面,颜色和输出文字
FPrimarySurface.GetDC(DC);
SetBkColor(DC,RGB(0,0,255));
SetTextColor(DC,RGB(255,255,0));
Textout(DC,50,50,Pchar(FrontMsg),Length(FrontMsg));
FPrimarySurface.ReleaseDC(DC);
//设置后表面,颜色和输出文字
FBackSurface.GetDC(DC);
SetBkColor(DC,RGB(0,255,255));
SetTextColor(DC,RGB(255,255,255));
Textout(DC,0,0,Pchar(BackMsg),Length(BackMsg));
FBackSurface.ReleaseDC(DC);
Timer1.Enabled:=True;
//起动定时器
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Width:=640;
Height:=480;
FDirectDraw:=nil;
FrontMsg:='Front 1 buffer(F12 or Esc to quit)';
//主表面要显示的文字
BackMsg:='Back 2 buffer(F12 or Esc to quit)';
//后表面要显示的文字
Start;
//调用起动过程
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
FPrimarySurface.Flip(nil,DDFLIP_WAIT);
//定时翻转
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,DirectDraw, ExtCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure start;
procedure Timer1Timer(Sender: TObject);
private
FrontMsg,BackMsg: string;
//定义显示的字符串
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
FDirectDraw:IDirectDraw;
FPrimarySurface,FBackSurface:IDirectDrawSurface;
implementation
{$R *.dfm}
Procedure TForm1.start;
var
SurfaceDesc:TDDSURFACEDESC;
//定义表面数据结构
DDSCapS:TDDSCAPS;
DC:HDC;
begin
DirectDrawCreate(nil,FDirectDraw,nil);
//创建DirectDraw对象
//设置全屏和独占模式,屏幕大小和颜色数。
FDirectDraw.SetCooperativelevel(Handle,DDSCL_EXCLUSIVE or DDSCL_FULLSCREEN);
FDirectDraw.SetDisplayMode(640,480,8);
//初始化表面数据结构
SurfaceDesc.dwSize:=sizeof(SurfaceDesc);
SurfaceDesc.dwFlags:=DDSD_CAPS or DDSD_BACKBUFFERCOUNT;
SurfaceDesc.ddsCaps.dwCaps:=DDSCAPS_PRIMARYSURFACE or
ddSCAPS_FLIP or DDSCAPS_COMPLEX ;
SurfaceDesc.dwBackBufferCount:=1;
//创建主面表
FDirectDraw.CreateSurface(SurfaceDesc,FPrimarySurface,nil);
//定义从属后表面
DDSCapS.dwCaps:=DDSCAPS_BACKBUFFER;
//创建后表面
FPrimarySurface.GetAttachedSurface(DDSCapS,FBackSurface);
//设置主表面,颜色和输出文字
FPrimarySurface.GetDC(DC);
SetBkColor(DC,RGB(0,0,255));
SetTextColor(DC,RGB(255,255,0));
Textout(DC,50,50,Pchar(FrontMsg),Length(FrontMsg));
FPrimarySurface.ReleaseDC(DC);
//设置后表面,颜色和输出文字
FBackSurface.GetDC(DC);
SetBkColor(DC,RGB(0,255,255));
SetTextColor(DC,RGB(255,255,255));
Textout(DC,0,0,Pchar(BackMsg),Length(BackMsg));
FBackSurface.ReleaseDC(DC);
Timer1.Enabled:=True;
//起动定时器
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Width:=640;
Height:=480;
FDirectDraw:=nil;
FrontMsg:='Front 1 buffer(F12 or Esc to quit)';
//主表面要显示的文字
BackMsg:='Back 2 buffer(F12 or Esc to quit)';
//后表面要显示的文字
Start;
//调用起动过程
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
FPrimarySurface.Flip(nil,DDFLIP_WAIT);
//定时翻转
end;
end.