• 全部
  • VC综合技术
  • 互联网技术
  • MFC AppLauncher
  • .NET 技术
  • 界面
  • 进程
  • 算法
  • 硬件/系统
  • 数据库
  • VC++技术资源

DX高手快来啊!小弟请教!

lyzcom 2001-07-19 08:58:24
我按照你说的,先ReleaseDC(..)后再lpDDPrimary->Flip(NULL, 0);可是屏幕全部是花的,人那些花屏的图案中可以看出,好像是WINDOWS当前的桌面被“花”上来了。这又是为什么啊?还是我原来的那些程序。但是同样的程序,在WIN2000下一切正常,也有什么花屏。而且,就算在WIN98下,只要不用缓冲,直接画到屏幕上,也没有问题啊?这是怎么回事?
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>

LPDIRECTDRAW4 lpDD;
LPDIRECTDRAWSURFACE4 lpDDPrimary;
LPDIRECTDRAWSURFACE4 lpDDBack;
BOOL bActive = TRUE;
HWND hwnd;
HDC hdc;

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL InitWindow(HINSTANCE, int);
HRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
BOOL InitFail(char *);
BOOL InitDDraw(void);
void FreeDDraw(void);
HRESULT FindLostSurface();
void ScreenFlip();
void Draw();



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;

if(!InitWindow(hInstance, nShowCmd)) return FALSE;
if(!InitDDraw()) return InitFail("初始化 DirectX 出错");

Draw();

while(1)
{
if(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if(bActive)
{
}
else WaitMessage();
}

return msg.wParam;
}

static BOOL InitWindow(HINSTANCE hInstance, int nShowCmd)
{
WNDCLASS wc;

wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.lpfnWndProc = WinProc;
wc.lpszClassName = "天才王国";
wc.lpszMenuName = "天才王国";
wc.style = CS_HREDRAW ¦ CS_VREDRAW;
wc.hInstance = hInstance;

RegisterClass(&wc);

hwnd = CreateWindowEx(NULL, "天才王国", "天才王国", WS_POPUP, 0, 0,
GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
NULL, NULL, hInstance, NULL);
if(!hwnd) return FALSE;

ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);

return TRUE;
}

HRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_ACTIVATEAPP:
bActive = TRUE;
break;

case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostMessage(hWnd, WM_CLOSE, NULL, NULL);
break;
}
break;

case WM_DESTROY:
FreeDDraw();
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

BOOL InitFail(char *str)
{
FreeDDraw();
MessageBox(hwnd, str, "Error!", MB_OK);
DestroyWindow(hwnd);
return FALSE;
}

void FreeDDraw()
{
if(lpDD != NULL)
{
if(lpDDPrimary != NULL)
{
lpDDPrimary->Release();
lpDDPrimary = NULL;
lpDDBack=NULL;
}

lpDD->Release();
lpDD = NULL;
}
}

BOOL InitDDraw()
{
LPDIRECTDRAW _lpDD;
DDSCAPS2 ddscaps;
DDSURFACEDESC2 ddsdesc;

//Create Draw Surface , Set Mode
if(FAILED(DirectDrawCreate(NULL, &_lpDD, NULL))) return FALSE;
if(FAILED(_lpDD->QueryInterface(IID_IDirectDraw4, (LPVOID *)&lpDD))) return FALSE;
if(FAILED(lpDD->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE ¦ DDSCL_FULLSCREEN))) return FALSE;
if(FAILED(lpDD->SetDisplayMode(800, 600, 16, 0, 0))) return FALSE;
UpdateWindow(hwnd);

//Release Primitive DD
_lpDD->Release(); _lpDD = NULL;

//Create Primary Surface
ZeroMemory(&ddsdesc, sizeof(ddsdesc));
ddsdesc.dwSize = sizeof(ddsdesc);
ddsdesc.dwFlags = DDSD_CAPS ¦ DDSD_BACKBUFFERCOUNT;
ddsdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE ¦ DDSCAPS_FLIP ¦ DDSCAPS_COMPLEX;
ddsdesc.dwBackBufferCount = 1;
if(FAILED(lpDD->CreateSurface(&ddsdesc, &lpDDPrimary, NULL))) return FALSE;

//Create a back buffer
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
if(FAILED(lpDDPrimary->GetAttachedSurface(&ddscaps, &lpDDBack))) return FALSE;

return TRUE;
}

void Draw()
{
lpDDBack->GetDC(&hdc);

SetBkColor(hdc, RGB(255, 0, 0));
SetTextColor(hdc, RGB(255, 255, 255));
TextOut(hdc, 100, 100, "abcdefg", lstrlen("abcdefg"));
ScreenFlip();

lpDDBack->ReleaseDC(hdc);
}

void ScreenFlip()
{
HRESULT ddrval;
while(1)
{
ddrval = lpDDPrimary->Flip(NULL, 0);

if(ddrval == DD_OK) break;
if(ddrval == DDERR_SURFACELOST)
{
ddrval = FindLostSurface();
if(ddrval != DD_OK) InitFail("内存错误");
}
if(ddrval != DDERR_WASSTILLDRAWING) break;
}
}

HRESULT FindLostSurface()
{
HRESULT ddrval;

ddrval = lpDDPrimary->Restore();
ddrval = lpDDBack->Restore();

return ddrval;
}
...全文
20 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
Kevin_qing 2001-07-19
花屏的原因是因为你没有再画东西前clear背景啊.
这样
再GetDC前面加上
DDBLTFX bltfx;
bltfx.dwSize=sizeof(bltfx);
bltfx.dwFillColor =0;//(或者其他你想要的颜色)
lpDDBack->Blt(NULL,NULL,NULL,DDBLT_COLORFILL |DDBLT_WAIT,&bltfx);
回复
相关推荐
发帖
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
帖子事件
创建了帖子
2001-07-19 08:58
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……