chenybin和老人家进来拿分吧! ( 积分: 100 )

  • 主题发起人 主题发起人 czw6296
  • 开始时间 开始时间
C

czw6296

Unregistered / Unconfirmed
GUEST, unregistred user!
上次分配分数错误,弥补一下。能提供一个C++Builder窗体下使用DirectDraw的例子吗?
或者看看我下面的程序,为什么显示的内容不在我的窗体中?当然,不回答也有份哦
//---------------------------------------------------------------------------

#include <vcl.h>
#include <ddraw.h>
#pragma hdrstop

#include &quot;Direct.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
//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 = &quot;Front buffer &quot;;
BackMsg = &quot;Back buffer &quot;;
}
//---------------------------------------------------------------------------
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, &amp;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(&amp;ddsd, &amp;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(&amp;ddsd, &amp;lpDDSBack, NULL);
if(ddrval == DD_OK)
{
ddrval = lpDD->CreateClipper(0, &amp;pClipper, NULL);
// myWnd = AfxGetMainWnd()->GetSafeHwnd();
ddrval = pClipper->SetHWnd(0,this->Handle );

ddrval = lpDDSPrimary->SetClipper(pClipper);
if (lpDDSPrimary->GetDC(&amp;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(&amp;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, &quot;Direct Draw Init Failed (%08lx)/n&quot;, ddrval);
MessageBox(Handle, buf, &quot;ERROR&quot;, MB_OK);
}

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Start();
Timer1->Enabled = True;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Draw();
Display();
}
void TForm1::Draw(void)
{
HDC DC;
if (lpDDSPrimary->IsLost() == DDERR_SURFACELOST) lpDDSPrimary->Restore();
if (lpDDSBack->GetDC(&amp;DC) == DD_OK)
{
if(phase)
{
this->Caption =&quot;前页!&quot;;
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 =&quot;后页!&quot;;
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::Display(void)
{
while(1)
{
HRESULT ddrval;
TRect r;
// ddrval = lpDDSPrimary->Flip(NULL, 0);
r=GetClientRect();
// ddrval = lpDDSPrimary->BltFast( 0, 0, lpDDSBack, &amp;r , DDBLTFAST_SRCCOLORKEY);
ddrval = lpDDSPrimary->Blt( &amp;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[] = &quot;My First DirectDraw !&quot;;
if (!bActive)
{
HDC DC = GetDC(Handle);
rc = GetClientRect();
GetTextExtentPoint(DC, szMsg, lstrlen(szMsg), &amp;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);
}
}
//---------------------------------------------------------------------------
 
上次分配分数错误,弥补一下。能提供一个C++Builder窗体下使用DirectDraw的例子吗?
或者看看我下面的程序,为什么显示的内容不在我的窗体中?当然,不回答也有份哦
//---------------------------------------------------------------------------

#include <vcl.h>
#include <ddraw.h>
#pragma hdrstop

#include &quot;Direct.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
//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 = &quot;Front buffer &quot;;
BackMsg = &quot;Back buffer &quot;;
}
//---------------------------------------------------------------------------
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, &amp;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(&amp;ddsd, &amp;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(&amp;ddsd, &amp;lpDDSBack, NULL);
if(ddrval == DD_OK)
{
ddrval = lpDD->CreateClipper(0, &amp;pClipper, NULL);
// myWnd = AfxGetMainWnd()->GetSafeHwnd();
ddrval = pClipper->SetHWnd(0,this->Handle );

ddrval = lpDDSPrimary->SetClipper(pClipper);
if (lpDDSPrimary->GetDC(&amp;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(&amp;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, &quot;Direct Draw Init Failed (%08lx)/n&quot;, ddrval);
MessageBox(Handle, buf, &quot;ERROR&quot;, MB_OK);
}

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Start();
Timer1->Enabled = True;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Draw();
Display();
}
void TForm1::Draw(void)
{
HDC DC;
if (lpDDSPrimary->IsLost() == DDERR_SURFACELOST) lpDDSPrimary->Restore();
if (lpDDSBack->GetDC(&amp;DC) == DD_OK)
{
if(phase)
{
this->Caption =&quot;前页!&quot;;
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 =&quot;后页!&quot;;
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::Display(void)
{
while(1)
{
HRESULT ddrval;
TRect r;
// ddrval = lpDDSPrimary->Flip(NULL, 0);
r=GetClientRect();
// ddrval = lpDDSPrimary->BltFast( 0, 0, lpDDSBack, &amp;r , DDBLTFAST_SRCCOLORKEY);
ddrval = lpDDSPrimary->Blt( &amp;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[] = &quot;My First DirectDraw !&quot;;
if (!bActive)
{
HDC DC = GetDC(Handle);
rc = GetClientRect();
GetTextExtentPoint(DC, szMsg, lstrlen(szMsg), &amp;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);
}
}
//---------------------------------------------------------------------------
 
其他人也可以回答,分数好说呀
 
看了几天的SDK,又参考了一个VB程序,今天终于可以把显示窗口定位到窗体中了。累!
 
留个位置,问题就不回答了,免得心理失落,看你给我多少分,最好能帮我顶到这个月满3W,呵呵(找个借口,其实是不会)
 
接受答案了.
 
后退
顶部