首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

C++ Builder DirectX 程式設計入門 (2):DirectDraw 設備枚舉與物件建立實作

     編輯:VC 教學

專案結構

  • dx1.bpr 專案檔
  • main.cpp 視窗實作
  • main.h 頭文件

主視窗類別定義

class TForm1 : public TForm {
__published: // IDE 管理元件
private:
    LPDIRECTDRAW FAR lplpDD;      // IDirectDraw 介面指標
    LPDIRECTDRAW2 FAR lplpDD2;    // IDirectDraw2 介面指標
public:
    __fastcall TForm1(TComponent* Owner);
};

關鍵實作代碼

全域變數

LPGUID FAR lpDevices[10];                 // 設備 GUID 陣列
struct DisplayModes { DWORD Width,Height,Depth,Rate; } DisplayMode[100];

設備枚舉回調

BOOL WINAPI EnumDeviceCallBack(GUID FAR *lpGUID, LPSTR lpDevice,
    LPSTR lpDeviceName, LPVOID lpContext) {
    static char i = 0;
    lpDevices[i] = lpGUID;
    Form1->ComboBox1->Items->Add((AnsiString)lpDevice + "--" + (AnsiString)lpDeviceName);
    return (i++ < 10) ? DDENUMRET_OK : 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 IDirectDraw2 Interface Failed!";
    else {
        Edit1->Text = "Get IDirectDraw2 Interface OK!";
        lplpDD->Release(); // 釋放基礎介面
        Button3->Enabled = false; Button4->Enabled = true;
    }
}

代碼展示從設備選擇到高版本介面獲取的完整流程,為後續顯示模式設置與圖形繪製奠基。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved