2.2.3 dx1源程序
2.2.3.1 dx1主要文件的组成为:工程文件(dx1.bpr)、窗口文件(main.cpp)、头文件(main.h)。
2.2.3.2 头文件main.h
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
(略)
private: // User declarations
LPDIRECTDRAW FAR lplpDD; /* 获得的 DirectDraw 接口指针 */
LPDIRECTDRAW2 FAR lplpDD2; /* 获得的 DirectDraw2 接口指针 */
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
2.2.3.3 程序文件main.cpp
#include <vcl.h>
#include "d:\tools\dx5sdk\sdk\inc\ddraw.h"
#pragma hdrstop
#include "main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
LPGUID FAR lpDevices[10]; /* 保存枚举设备的标识指针 */
struct DisplayModes { DWORD Width,Height,Depth,Rate; } DisplayMode[100];
/* 保存显示设备支持的显示方式 */
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//设备枚举回调函数---------------------------------------------------------------------------
BOOL WINAPI EnumDeviceCallBack(GUID FAR *lpGUID,LPSTR lpDevice,
LPSTR lpDeviceName,
LPVOID lpContext)
{ static char i=0;
lpDevices[i]=lpGUID;
i++;
Form1->ComboBox1->Items->Add((AnsiString)lpDevice+"--"+(AnsiString)lpDeviceName);
if(i<10)
return(DDENUMRET_OK);
else
return(DDENUMRET_CANCEL);
}
//枚举设备---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ComboBox1->Clear();
if(FAILED(DirectDrawEnumerate(EnumDeviceCallBack,NULL)))
Edit1->Text="Enumerate Devices failed!";
else
Edit1->Text="Enumerate Devices OK!";
ComboBox1->ItemIndex=0;
Button1->Enabled=false;
Button2->Enabled=true;
}
//创建DirectDraw对象---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
LPGUID FAR lpD=lpDevices[ComboBox1->ItemIndex];
if(FAILED(DirectDrawCreate(lpD,&lplpDD,NULL)))
Edit1->Text="DirectDraw Create failed!";
else
{
Edit1->Text="DirectDraw Create OK!";
ComboBox1->Enabled=false;
Button2->Enabled=false;
Button3->Enabled=true;
}
}
//获得IDirectDraw2接口---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(FAILED(lplpDD->QueryInterface(IID_IDirectDraw2,(LPVOI |