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;
}
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;
}
}
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;
}
}
代碼展示從設備選擇到高版本介面獲取的完整流程,為後續顯示模式設置與圖形繪製奠基。