主题:跪球各位高手指点,我都快哭了, 球各位高手门给我指条活路
小弟我最近学习DX编程,按照书上教的,写了个窗口程序,把一个图片加到程序里头去,编译的所有错误都解决了,但是,就是无法运行程序...我都快疯了,我仔细看了代码,就是不知道错在那里,希望各位高手,给我指明活路,我给你们跪下了[em10][em10][em10]
以下是代码,希望各位大哥们帮帮我,救救我啊.!!
主程序 MAIN.CPP
#include <windows.h>
#include "Wnd.h"
#include "MyDDraw.h"
#include <ddraw.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
Win w;
CDDraw d;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
HWND hWnd;
LPDIRECTDRAWSURFACE7 lpSurf;
hWnd=w.InitWin(WndProc,hInstance,nCmdShow);
d.InitDDraw(hWnd);
d.BlitBack(lpSurf);
d.FLIP();
w.RunMsg();
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT nmsg,WPARAM wParam,LPARAM lParam)
{
return w.MsgProc(hWnd,nmsg,wParam,lParam);
}
我写的DirectDraw类 MyCDDraw.h
#include <windows.h>
#include <ddraw.h>
#pragma comment(lib,"ddraw.lib")
#pragma comment(lib,"dxguid.lib")
class CDDraw
{
protected:
LPDIRECTDRAW7 lpDD;
LPDIRECTDRAWSURFACE7 lpDDSPrimary;
LPDIRECTDRAWSURFACE7 lpDDSBack;
public:
BOOL InitDDraw(HWND hWnd);
LPDIRECTDRAWSURFACE7 CreateBMPSurface(LPCSTR BitmapFile,int X,int Y);
BOOL BlitBack(LPDIRECTDRAWSURFACE7 lpSurf);
void FLIP();
};
BOOL CDDraw::InitDDraw(HWND hWnd)
{
if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpDD,IID_IDirectDraw7,NULL)))
{
return FALSE;
}
if(FAILED(lpDD->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN)))
{
return FALSE;
}
if(FAILED(lpDD->SetDisplayMode(800,640,16,NULL,NULL)))
{
return FALSE;
}
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount=1;
if(FAILED(lpDD->CreateSurface(&ddsd,&lpDDSPrimary,NULL)))
{
return FALSE;
}
ddsd.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
if(FAILED(lpDDSPrimary->GetAttachedSurface(&ddsd.ddsCaps,&lpDDSBack)))
{
return FALSE;
}
return TRUE;
}
LPDIRECTDRAWSURFACE7 CDDraw::CreateBMPSurface(LPCSTR BitmapFile,int X,int Y)
{
HDC hdc;
HDC hdcImage;
HBITMAP hbm;
LPDIRECTDRAWSURFACE7 lpSurf=NULL;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth=X;
ddsd.dwHeight=Y;
if(FAILED(lpDD->CreateSurface(&ddsd,&lpSurf,NULL)))
{
return FALSE;
}
hbm=(HBITMAP)LoadImage(NULL,BitmapFile,IMAGE_BITMAP,X,Y,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
hdcImage=CreateCompatibleDC(NULL);
SelectObject(hdcImage,hbm);
lpSurf->GetDC(&hdc);
BitBlt(hdc,0,0,X,Y,hdcImage,0,0,SRCCOPY);
DeleteDC(hdcImage);
DeleteObject(hbm);
lpSurf->ReleaseDC(hdc);
return lpSurf;
}
BOOL CDDraw::BlitBack(LPDIRECTDRAWSURFACE7 lpSurf)
{
if(FAILED(lpDDSBack->Blt(NULL,lpSurf,NULL,DDBLT_WAIT,NULL)))
{
return FALSE;
}
return TRUE;
}
void CDDraw::FLIP()
{
HRESULT ddrval=1;
if( lpDDSPrimary && lpDDSBack )
{
if (lpDDSPrimary->IsLost()==DDERR_SURFACELOST)
lpDDSPrimary->Restore();
if (lpDDSBack->IsLost()==DDERR_SURFACELOST)
lpDDSBack->Restore();
do{
ddrval = lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
if( ddrval == DD_OK )
break;
else if( ddrval == DDERR_SURFACELOST ){
if( FAILED(lpDDSBack->Restore()) )
break;
if( FAILED(lpDDSPrimary->Restore()) )
break;
}
else if( ddrval != DDERR_WASSTILLDRAWING )
break;
}while( 1 );
}
}
最最基本的窗口类 Wnd.h
#include <windows.h>
class Win
{
protected:
HWND hWnd;
public:
HWND InitWin(WNDPROC WndProc,HINSTANCE hInstance,int nCmdShow);
int RunMsg();
LRESULT MsgProc(HWND hWnd,UINT nmsg,WPARAM wParam,LPARAM lParam);
};
HWND Win::InitWin(WNDPROC WndProc,HINSTANCE hInstance,int nCmdShow)
{
static char app[] = "Ex1";
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = app;
if(!RegisterClass(&wc))
{
MessageBox(NULL,"注册失败","Error!",MB_OK);
}
hWnd = CreateWindow(app,"练习1",WS_POPUP,0,0,1027,760,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return hWnd;
}
int Win::RunMsg()
{
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT Win::MsgProc(HWND hWnd,UINT nmsg,WPARAM wParam,LPARAM lParam)
{
switch(nmsg)
{
case WM_DESTROY:
PostQuitMessage(0);
}
return DefWindowProc(hWnd,nmsg,wParam,lParam);
}
求求各位大哥们帮帮小弟我了,我都快崩溃了,就是不知道怎么回事,写了N次,都是这样,程序可以生成,就是没办法顺利运行,一运行就错误终止,我给你们下跪了,求你们帮我....当然不是没放图片在DEBUG下面的那个错误,所以这个回答,可以省略了...
[em10][em10][em10][em10][em10]
以下是代码,希望各位大哥们帮帮我,救救我啊.!!
主程序 MAIN.CPP
#include <windows.h>
#include "Wnd.h"
#include "MyDDraw.h"
#include <ddraw.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
Win w;
CDDraw d;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
HWND hWnd;
LPDIRECTDRAWSURFACE7 lpSurf;
hWnd=w.InitWin(WndProc,hInstance,nCmdShow);
d.InitDDraw(hWnd);
d.BlitBack(lpSurf);
d.FLIP();
w.RunMsg();
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT nmsg,WPARAM wParam,LPARAM lParam)
{
return w.MsgProc(hWnd,nmsg,wParam,lParam);
}
我写的DirectDraw类 MyCDDraw.h
#include <windows.h>
#include <ddraw.h>
#pragma comment(lib,"ddraw.lib")
#pragma comment(lib,"dxguid.lib")
class CDDraw
{
protected:
LPDIRECTDRAW7 lpDD;
LPDIRECTDRAWSURFACE7 lpDDSPrimary;
LPDIRECTDRAWSURFACE7 lpDDSBack;
public:
BOOL InitDDraw(HWND hWnd);
LPDIRECTDRAWSURFACE7 CreateBMPSurface(LPCSTR BitmapFile,int X,int Y);
BOOL BlitBack(LPDIRECTDRAWSURFACE7 lpSurf);
void FLIP();
};
BOOL CDDraw::InitDDraw(HWND hWnd)
{
if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpDD,IID_IDirectDraw7,NULL)))
{
return FALSE;
}
if(FAILED(lpDD->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN)))
{
return FALSE;
}
if(FAILED(lpDD->SetDisplayMode(800,640,16,NULL,NULL)))
{
return FALSE;
}
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount=1;
if(FAILED(lpDD->CreateSurface(&ddsd,&lpDDSPrimary,NULL)))
{
return FALSE;
}
ddsd.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
if(FAILED(lpDDSPrimary->GetAttachedSurface(&ddsd.ddsCaps,&lpDDSBack)))
{
return FALSE;
}
return TRUE;
}
LPDIRECTDRAWSURFACE7 CDDraw::CreateBMPSurface(LPCSTR BitmapFile,int X,int Y)
{
HDC hdc;
HDC hdcImage;
HBITMAP hbm;
LPDIRECTDRAWSURFACE7 lpSurf=NULL;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY;
ddsd.dwWidth=X;
ddsd.dwHeight=Y;
if(FAILED(lpDD->CreateSurface(&ddsd,&lpSurf,NULL)))
{
return FALSE;
}
hbm=(HBITMAP)LoadImage(NULL,BitmapFile,IMAGE_BITMAP,X,Y,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
hdcImage=CreateCompatibleDC(NULL);
SelectObject(hdcImage,hbm);
lpSurf->GetDC(&hdc);
BitBlt(hdc,0,0,X,Y,hdcImage,0,0,SRCCOPY);
DeleteDC(hdcImage);
DeleteObject(hbm);
lpSurf->ReleaseDC(hdc);
return lpSurf;
}
BOOL CDDraw::BlitBack(LPDIRECTDRAWSURFACE7 lpSurf)
{
if(FAILED(lpDDSBack->Blt(NULL,lpSurf,NULL,DDBLT_WAIT,NULL)))
{
return FALSE;
}
return TRUE;
}
void CDDraw::FLIP()
{
HRESULT ddrval=1;
if( lpDDSPrimary && lpDDSBack )
{
if (lpDDSPrimary->IsLost()==DDERR_SURFACELOST)
lpDDSPrimary->Restore();
if (lpDDSBack->IsLost()==DDERR_SURFACELOST)
lpDDSBack->Restore();
do{
ddrval = lpDDSPrimary->Flip( NULL, DDFLIP_WAIT );
if( ddrval == DD_OK )
break;
else if( ddrval == DDERR_SURFACELOST ){
if( FAILED(lpDDSBack->Restore()) )
break;
if( FAILED(lpDDSPrimary->Restore()) )
break;
}
else if( ddrval != DDERR_WASSTILLDRAWING )
break;
}while( 1 );
}
}
最最基本的窗口类 Wnd.h
#include <windows.h>
class Win
{
protected:
HWND hWnd;
public:
HWND InitWin(WNDPROC WndProc,HINSTANCE hInstance,int nCmdShow);
int RunMsg();
LRESULT MsgProc(HWND hWnd,UINT nmsg,WPARAM wParam,LPARAM lParam);
};
HWND Win::InitWin(WNDPROC WndProc,HINSTANCE hInstance,int nCmdShow)
{
static char app[] = "Ex1";
WNDCLASS wc;
wc.style = CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = app;
if(!RegisterClass(&wc))
{
MessageBox(NULL,"注册失败","Error!",MB_OK);
}
hWnd = CreateWindow(app,"练习1",WS_POPUP,0,0,1027,760,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
return hWnd;
}
int Win::RunMsg()
{
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT Win::MsgProc(HWND hWnd,UINT nmsg,WPARAM wParam,LPARAM lParam)
{
switch(nmsg)
{
case WM_DESTROY:
PostQuitMessage(0);
}
return DefWindowProc(hWnd,nmsg,wParam,lParam);
}
求求各位大哥们帮帮小弟我了,我都快崩溃了,就是不知道怎么回事,写了N次,都是这样,程序可以生成,就是没办法顺利运行,一运行就错误终止,我给你们下跪了,求你们帮我....当然不是没放图片在DEBUG下面的那个错误,所以这个回答,可以省略了...
[em10][em10][em10][em10][em10]