用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧C++ Builder

在CB中进行DirectX编程(2)

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:41:21

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:oolsdx5sdksdkincddraw.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=dwFlagsDDSCL_ALLOWMODEX;

if(CheckBox2->Checked) dwFlags=dwFlagsDDSCL_ALLOWREBOOT;

if(CheckBox3->Checked) dwFlags=dwFlagsDDSCL_EXCLUSIVE;

if(CheckBox4->Checked) dwFlags=dwFlagsDDSCL_FULLSCREEN;

if(CheckBox5->Checked) dwFlags=dwFlagsDDSCL_NORMAL;

if(CheckBox6->Checked) dwFlags=dwFlagsDDSCL_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();

}

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:15,531.25000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号