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,(LPVOID *)&lplpDD2)))
Edit1->Text="Get IDriectDraw2 Interface Failed!";
else
{
Edit1->Text="Get IDriectDraw2 Interface OK!";
lplpDD->Release(); /* 釋放IdirectDraw接口 */
Button3->Enabled=false;
Button4->Enabled=true;
}
}