|
各位好!!
我刚学directx,想显示一幅图,可是遇到了几个问题想请各位高手帮忙。
1。我为什么不能定义lpdirectdraw7的对象,一定义就说参数不对或是不能转换。我只好都改成lpdirectdraw
了。是不是得用QueryInterface函数才行?书上都没有用啊。
2。我的图显示出来比原来的图像大,屏幕放不下。也不是从屏幕0,0位置开始的。
3。还有rect结构在程式中要怎么初始化才好呢??
4。谢谢各位看看我这还有什么错误什么的请指点一下,费心了。
#include <windows.h>
#include <ddraw.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
LPDIRECTDRAW lpDD7;
LPDIRECTDRAWSURFACE lpPrimary;
LPDIRECTDRAWSURFACE lpOffscreenplain;
LPDIRECTDRAWSURFACE lpBackbuffer;
DDSURFACEDESC desc;
DDSCAPS caps;
HBITMAP h_bitmap;
RECT rect;
BOOL InitDraw(HWND hWnd);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
rect.bottom=600;
rect.left=0;
rect.right=800;
rect.top=0;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
InitDraw( hwnd );
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
//PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
BOOL InitDraw(HWND hWd)
{
HRESULT h_judge;
HDC hdc;
HDC hdc1;
HWND hWnd = hWd;
if( (h_judge=DirectDrawCreate( NULL, &lpDD7, NULL))!=DD_OK )
return FALSE;
if( (h_judge=lpDD7->SetCooperativeLevel(hWnd, DDSCL_EXCLUSIVE |
DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT))!=DD_OK )
return FALSE;
if( (h_judge=lpDD7->SetDisplayMode(800, 600, 16))!=DD_OK )
return FALSE;
ZeroMemory( &desc, sizeof(desc) );
desc.dwSize=sizeof(desc);
desc.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
desc.dwBackBufferCount=1;
desc.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX |
DDSCAPS_FLIP;
if( (h_judge=lpDD7->CreateSurface(&desc, &lpPrimary, NULL))!=DD_OK )
return FALSE;
caps.dwCaps=DDSCAPS_BACKBUFFER;
if( (h_judge=lpPrimary->GetAttachedSurface(&caps, &lpBackbuffer))!=DD_OK )
return FALSE;
ZeroMemory( &desc, sizeof(desc) );
desc.dwSize=sizeof(desc);
desc.dwFlags=DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
desc.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;
desc.dwWidth=800;
desc.dwHeight=600;
if( (h_judge=lpDD7->CreateSurface(&desc, &lpOffscreenplain, NULL))!=DD_OK )
return FALSE;
hdc1=CreateCompatibleDC( NULL );
lpOffscreenplain->GetDC( &hdc );
h_bitmap=(HBITMAP)LoadImage( NULL, "i:\\baby.bmp", IMAGE_BITMAP, 800,
600, LR_LOADFROMFILE );
SelectObject( hdc1, h_bitmap );
BitBlt( hdc, 0, 0, 800, 600, hdc1, 0, 0, SRCCOPY );
lpOffscreenplain->ReleaseDC( hdc );
lpBackbuffer->BltFast( 0, 0, lpOffscreenplain, &rect, DDBLTFAST_WAIT );
lpPrimary->Flip( NULL, DDFLIP_WAIT );
return 0;
}
[em7] [em7] [em7] [em7] [em10] |
|