分类: C/C++
2008-05-31 14:07:05
2.2.3.1 dx1主要文件的组成为:工程文件(dx1.bpr)、窗口文件(main.cpp)、头文件(main.h)。
2.2.3.2 头文件main.h
#ifndef mainH
#define mainH
//---------------------------------------------------------------------------
#include
#include
#include
#include
//---------------------------------------------------------------------------
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
#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;
}
}
//设置协作级别---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
DWORD dwFlags=0;
if(CheckBox1->Checked) dwFlags=dwFlags|DDSCL_ALLOWMODEX;
if(CheckBox2->Checked) dwFlags=dwFlags|DDSCL_ALLOWREBOOT;
if(CheckBox3->Checked) dwFlags=dwFlags|DDSCL_EXCLUSIVE;
if(CheckBox4->Checked) dwFlags=dwFlags|DDSCL_FULLSCREEN;
if(CheckBox5->Checked) dwFlags=dwFlags|DDSCL_NORMAL;
if(CheckBox6->Checked) dwFlags=dwFlags|DDSCL_NOWINDOWCHANGES;
if(FAILED(lplpDD2->SetCooperativeLevel(Handle,dwFlags)))
Edit1->Text=\"Set set cooperative level Failed!\";
else
{
Edit1->Text=\"Set cooperative level OK!\";
GroupBox2->Enabled=false;
Button4->Enabled=false;
Button5->Enabled=true;
}
}//枚举显示模式的回调函数---------------------------------------------------------------------------
BOOL WINAPI EnumDisplayModesCallBack(LPDDSURFACEDESC lpDesc,
LPVOID lpContext)
{
static int i=0;
char buff[256];
wsprintf(buff,\"%dx%dx%dx%d\",
lpDesc->dwWidth,
lpDesc->dwHeight,
lpDesc->ddpfPixelFormat.dwRGBBitCount,
lpDesc->dwRefreshRate);
Form1->ComboBox2->Items->Add(buff);
DisplayMode[i].Width=lpDesc->dwWidth;
DisplayMode[i].Height=lpDesc->dwHeight;
DisplayMode[i].Depth=lpDesc->ddpfPixelFormat.dwRGBBitCount;
DisplayMode[i].Rate=lpDesc->dwRefreshRate;
i++;
if(i<100)
return(DDENUMRET_OK);
else
return(DDENUMRET_CANCEL);
}
//枚举显示模式---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
ComboBox2->Clear();
if(FAILED(lplpDD2->EnumDisplayModes(DDEDM_STANDARDVGAMODES,
NULL,
NULL,
(LPDDENUMMODESCALLBACK)EnumDisplayModesCallBack)))
Edit1->Text=\"Enumerate Display Modes failed!\";
else
{
Edit1->Text=\"Enumerate Display Modes OK!\";
ComboBox2->ItemIndex=0;
Button5->Enabled=false;
Button6->Enabled=true;
}
}
//设置显示模式---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
if(ComboBox2->ItemIndex<0)
{
ShowMessage(\"请先在DDraw2下拉框中选择一种显示方式\");
return;
}
if(FAILED(lplpDD2->SetDisplayMode(
DisplayMode[ComboBox2->ItemIndex].Width,
DisplayMode[ComboBox2->ItemIndex].Height,
DisplayMode[ComboBox2->ItemIndex].Depth,
DisplayMode[ComboBox2->ItemIndex].Rate,
0)))
Edit1->Text=\"Set DisplayMode failed!\";
}
//退出dx1---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(lplpDD2) lplpDD2->Release();
}