如何定位DirectDraw的画图区域,比如PaintBox中 ( 积分: 50 )

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

czw6296

Unregistered / Unconfirmed
GUEST, unregistred user!
以下程序来自C++Builder6的示例程序。
ddrval = DirectDrawCreate(NULL, &lpDD, NULL);
if(ddrval == DD_OK)
{
ddrval = lpDD->SetCooperativeLevel(Handle,DDSCL_NORMAL);
//Handle改为PaintBox1->Canvas->Handle就出错,为什么
if(ddrval == DD_OK)
{
ddrval = lpDD->SetDisplayMode(1024, 768, 24);
if(ddrval == DD_OK)
{
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
if(ddrval == DD_OK)
{
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps,
&lpDDSBack);
if(ddrval == DD_OK)
{
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);
}
Timer1->Enabled = True;
bActive = True;
return;
}
}
}
}
}
wsprintf(buf, "Direct Draw Init Failed (%08lx)/n", ddrval);
MessageBox(Handle, buf, "ERROR", MB_OK);
}
 
以下程序来自C++Builder6的示例程序。
ddrval = DirectDrawCreate(NULL, &lpDD, NULL);
if(ddrval == DD_OK)
{
ddrval = lpDD->SetCooperativeLevel(Handle,DDSCL_NORMAL);
//Handle改为PaintBox1->Canvas->Handle就出错,为什么
if(ddrval == DD_OK)
{
ddrval = lpDD->SetDisplayMode(1024, 768, 24);
if(ddrval == DD_OK)
{
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
DDSCAPS_FLIP |
DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
if(ddrval == DD_OK)
{
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps,
&lpDDSBack);
if(ddrval == DD_OK)
{
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);
}
Timer1->Enabled = True;
bActive = True;
return;
}
}
}
}
}
wsprintf(buf, "Direct Draw Init Failed (%08lx)/n", ddrval);
MessageBox(Handle, buf, "ERROR", MB_OK);
}
 
我需要在程序的某个窗口中实现动画,C++Builder中的例子全是FullScreen模式下。哪为朋友有在窗口中实现的例子,请贴出来共参考,多谢了
 
我想在动画时,窗口菜单等仍能够显示,SetCooperativeLevel的参数如何设置哦
 
高手看看程序错在哪里,为什么显示的内容不在我的窗体中
Blt(THIS_ LPRECT,LPDIRECTDRAWSURFACE, LPRECT,DWORD, LPDDBLTFX);中两个LPRECT含义是???
//---------------------------------------------------------------------------

#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);
}
}
//---------------------------------------------------------------------------
 
将Canvas赋值给directdraw的surface即可
 
接受答案了.
 
后退
顶部