C
czw6296
Unregistered / Unconfirmed
GUEST, unregistred user!
上次分配分数错误,弥补一下。能提供一个C++Builder窗体下使用DirectDraw的例子吗?
或者看看我下面的程序,为什么显示的内容不在我的窗体中?当然,不回答也有份哦
//---------------------------------------------------------------------------
#include <vcl.h>
#include <ddraw.h>
#pragma hdrstop
#include "Direct.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//Const
#define TIMER_ID 1
#define TIMER_RATE 500
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
lpDD = NULL;
phase = 0;
bActive = False;
FrontMsg = "Front buffer ";
BackMsg = "Back buffer ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
if(lpDD != NULL)
{
if(lpDDSPrimary != NULL)
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
//---------------------------------------------------------------------------
///////////////////////////////////////
// Initialize DirectDraw
///////////////////////////////////////
void __fastcall TForm1::Start()
{
HRESULT ddrval;
DDSURFACEDESC ddsd;
HDC DC;
char buf[256];
ddrval = DirectDrawCreate(NULL, &lpDD, NULL);
if(ddrval == DD_OK)
{
ddrval =lpDD->SetCooperativeLevel(Handle,DDSCL_NORMAL);
if(ddrval == DD_OK)
{
ddrval = lpDD->SetDisplayMode(1024, 768, 16);
if(ddrval == DD_OK)
{
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS ;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
if(ddrval == DD_OK)
{
ddsd.dwFlags = DDSD_CAPS |DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;//|DDSCAPS_3DDEVICE;
ddsd.dwWidth =ClientWidth ;
ddsd.dwHeight=ClientHeight;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSBack, NULL);
if(ddrval == DD_OK)
{
ddrval = lpDD->CreateClipper(0, &pClipper, NULL);
// myWnd = AfxGetMainWnd()->GetSafeHwnd();
ddrval = pClipper->SetHWnd(0,this->Handle );
ddrval = lpDDSPrimary->SetClipper(pClipper);
if (lpDDSPrimary->GetDC(&DC) == DD_OK)
{
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, 0, 0, FrontMsg.c_str(), FrontMsg.Length());
lpDDSPrimary->ReleaseDC(DC);
}
if (lpDDSBack->GetDC(&DC) == DD_OK)
{
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, 0, 0, BackMsg.c_str(), BackMsg.Length());
lpDDSBack->ReleaseDC(DC);
}
bActive = True;
return;
}
}
}
}
}
wsprintf(buf, "Direct Draw Init Failed (%08lx)/n", ddrval);
MessageBox(Handle, buf, "ERROR", MB_OK);
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Start();
Timer1->Enabled = True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Draw();
Display();
}
void TForm1:raw(void)
{
HDC DC;
if (lpDDSPrimary->IsLost() == DDERR_SURFACELOST) lpDDSPrimary->Restore();
if (lpDDSBack->GetDC(&DC) == DD_OK)
{
if(phase)
{
this->Caption ="前页!";
this->Update() ;
Application->ProcessMessages();
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, 100, 100, FrontMsg.c_str(), FrontMsg.Length());
phase = 0;
}
else
{
this->Caption ="后页!";
this->Update() ;
Application->ProcessMessages();
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(0, 255, 255));
TextOut(DC, 100, 100, BackMsg.c_str(), BackMsg.Length());
phase = 1;
}
lpDDSBack->ReleaseDC(DC);
}
}
//---------------------------------------------------------------------------
void TForm1:isplay(void)
{
while(1)
{
HRESULT ddrval;
TRect r;
// ddrval = lpDDSPrimary->Flip(NULL, 0);
r=GetClientRect();
// ddrval = lpDDSPrimary->BltFast( 0, 0, lpDDSBack, &r , DDBLTFAST_SRCCOLORKEY);
ddrval = lpDDSPrimary->Blt( &r, lpDDSBack, NULL , DDBLT_WAIT,NULL);
if(ddrval == DD_OK) break;
if(ddrval == DDERR_SURFACELOST)
{
ddrval = lpDDSPrimary->Restore();
if(ddrval != DD_OK)
break;
}
if(ddrval != DDERR_WASSTILLDRAWING)
break;
}
}
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Timer1->Enabled =false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
RECT rc;
SIZE size;
char szMsg[] = "My First DirectDraw !";
if (!bActive)
{
HDC DC = GetDC(Handle);
rc = GetClientRect();
GetTextExtentPoint(DC, szMsg, lstrlen(szMsg), &size);
SetBkColor(DC, RGB(0, 0, 0));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, (rc.right - size.cx)/2, (rc.bottom - size.cy)/2,szMsg, sizeof(szMsg)-1);
ReleaseDC(Handle, DC);
}
}
//---------------------------------------------------------------------------
或者看看我下面的程序,为什么显示的内容不在我的窗体中?当然,不回答也有份哦
//---------------------------------------------------------------------------
#include <vcl.h>
#include <ddraw.h>
#pragma hdrstop
#include "Direct.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//Const
#define TIMER_ID 1
#define TIMER_RATE 500
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
lpDD = NULL;
phase = 0;
bActive = False;
FrontMsg = "Front buffer ";
BackMsg = "Back buffer ";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
if(lpDD != NULL)
{
if(lpDDSPrimary != NULL)
{
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
lpDD->Release();
lpDD = NULL;
}
}
//---------------------------------------------------------------------------
///////////////////////////////////////
// Initialize DirectDraw
///////////////////////////////////////
void __fastcall TForm1::Start()
{
HRESULT ddrval;
DDSURFACEDESC ddsd;
HDC DC;
char buf[256];
ddrval = DirectDrawCreate(NULL, &lpDD, NULL);
if(ddrval == DD_OK)
{
ddrval =lpDD->SetCooperativeLevel(Handle,DDSCL_NORMAL);
if(ddrval == DD_OK)
{
ddrval = lpDD->SetDisplayMode(1024, 768, 16);
if(ddrval == DD_OK)
{
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS ;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
if(ddrval == DD_OK)
{
ddsd.dwFlags = DDSD_CAPS |DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;//|DDSCAPS_3DDEVICE;
ddsd.dwWidth =ClientWidth ;
ddsd.dwHeight=ClientHeight;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSBack, NULL);
if(ddrval == DD_OK)
{
ddrval = lpDD->CreateClipper(0, &pClipper, NULL);
// myWnd = AfxGetMainWnd()->GetSafeHwnd();
ddrval = pClipper->SetHWnd(0,this->Handle );
ddrval = lpDDSPrimary->SetClipper(pClipper);
if (lpDDSPrimary->GetDC(&DC) == DD_OK)
{
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, 0, 0, FrontMsg.c_str(), FrontMsg.Length());
lpDDSPrimary->ReleaseDC(DC);
}
if (lpDDSBack->GetDC(&DC) == DD_OK)
{
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, 0, 0, BackMsg.c_str(), BackMsg.Length());
lpDDSBack->ReleaseDC(DC);
}
bActive = True;
return;
}
}
}
}
}
wsprintf(buf, "Direct Draw Init Failed (%08lx)/n", ddrval);
MessageBox(Handle, buf, "ERROR", MB_OK);
}
void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Start();
Timer1->Enabled = True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Draw();
Display();
}
void TForm1:raw(void)
{
HDC DC;
if (lpDDSPrimary->IsLost() == DDERR_SURFACELOST) lpDDSPrimary->Restore();
if (lpDDSBack->GetDC(&DC) == DD_OK)
{
if(phase)
{
this->Caption ="前页!";
this->Update() ;
Application->ProcessMessages();
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, 100, 100, FrontMsg.c_str(), FrontMsg.Length());
phase = 0;
}
else
{
this->Caption ="后页!";
this->Update() ;
Application->ProcessMessages();
SetBkColor(DC, RGB(0, 0, 255));
SetTextColor(DC, RGB(0, 255, 255));
TextOut(DC, 100, 100, BackMsg.c_str(), BackMsg.Length());
phase = 1;
}
lpDDSBack->ReleaseDC(DC);
}
}
//---------------------------------------------------------------------------
void TForm1:isplay(void)
{
while(1)
{
HRESULT ddrval;
TRect r;
// ddrval = lpDDSPrimary->Flip(NULL, 0);
r=GetClientRect();
// ddrval = lpDDSPrimary->BltFast( 0, 0, lpDDSBack, &r , DDBLTFAST_SRCCOLORKEY);
ddrval = lpDDSPrimary->Blt( &r, lpDDSBack, NULL , DDBLT_WAIT,NULL);
if(ddrval == DD_OK) break;
if(ddrval == DDERR_SURFACELOST)
{
ddrval = lpDDSPrimary->Restore();
if(ddrval != DD_OK)
break;
}
if(ddrval != DDERR_WASSTILLDRAWING)
break;
}
}
void __fastcall TForm1::BitBtn2Click(TObject *Sender)
{
Timer1->Enabled =false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
RECT rc;
SIZE size;
char szMsg[] = "My First DirectDraw !";
if (!bActive)
{
HDC DC = GetDC(Handle);
rc = GetClientRect();
GetTextExtentPoint(DC, szMsg, lstrlen(szMsg), &size);
SetBkColor(DC, RGB(0, 0, 0));
SetTextColor(DC, RGB(255, 255, 0));
TextOut(DC, (rc.right - size.cx)/2, (rc.bottom - size.cy)/2,szMsg, sizeof(szMsg)-1);
ReleaseDC(Handle, DC);
}
}
//---------------------------------------------------------------------------