|
|
发表于 2006-8-5 21:48:00
|
显示全部楼层
Re:关于图像处理问题?
你好,我也是初学者,我知道两种方法,WIN32 GDI和D3DSprite(DDraw没学)
WIN32的方法很简单,就是用BitBlt
他的原形是:
BOOL BitBlt(
HDC hdcDest, // handle to destination DC (目的DC)
int nXDest, // x-coord of destination upper-left corner (目的DC X坐标)
int nYDest, // y-coord of destination upper-left corner (目的DC Y坐标)
int nWidth, // width of destination rectangle (贴到目的DC的宽度)
int nHeight, // height of destination rectangle (贴到目的DC的高度)
HDC hdcSrc, // handle to source DC (来源DC)
int nXSrc, // x-coordinate of source upper-left corner (来源DC X 坐标)
int nYSrc, // y-coordinate of source upper-left corner (来源DC Y 坐标)
DWORD dwRop // raster operation code (贴图方式)
);
比如BitBlt(hdc,50,50,350,250,mdc,200,100,SRCCOPY) ;
意思就是把MDC上的图片以(200,100)作为左上角坐标,350,250作为宽和高的一个矩形区域贴到目的DC上。而贴到目的DC的位置是以(50,50)作为其左上点坐标的。
D3DSprite的方法这里有介绍http://www.dingge.com/forum/dispbbs.asp?boardID=46&ID=6940&page=4
以前练习时写的源码,不过没注释:
#include<iostream>
#include<windows.h>
HINSTANCE hInst;
HBITMAP boy,bg;
HDC mdc,hdc,bufdc;
DWORD tPre,tNow;
int x=0,y=0,num=0,dir=0;
int delay=2,onum=0;
int WalkRun=0;
const int w=33,h=49;
HWND hWnd;
void MyPaint(HDC hdc);
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow);
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine,int iCmdShow)
{
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);
MSG msg;
WNDCLASSEX wndclass ;
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = NULL ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = "mouse" ;
wndclass.hIconSm = NULL;
if (!RegisterClassEx (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
TEXT ("This program requires Windows NT!"), MB_ICONERROR) ;
return 0 ;
}
if(!InitInstance(hInstance,iCmdShow))
{
MessageBox ( NULL, TEXT ("!!!!!!"),
TEXT ("This program can't init instance!"), MB_ICONERROR) ;
return FALSE;
}
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
else
{
tNow=GetTickCount();
if(tNow-tPre>=40)
MyPaint(hdc);
}
}
return msg.wParam;
}
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
HBITMAP bmp;
hInst=hInstance;
hWnd=CreateWindow("mouse","人物窗口",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hWnd)
{
return FALSE;
}
MoveWindow(hWnd,10,10,640,480,true);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
POINT pt;
RECT rc;
pt.x=0;
pt.y=0;
ClientToScreen(hWnd,&pt);
rc.left=pt.x;
rc.top=pt.y;
rc.bottom=pt.y+446;
rc.right=pt.x+632;
ClipCursor(&rc);
hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc);
bmp=CreateCompatibleBitmap(hdc,640,480);
SelectObject(mdc,bmp);
x=300;
y=250;
boy=(HBITMAP)LoadImage(NULL,"行走2.bmp",IMAGE_BITMAP,131,196,LR_LOADFROMFILE);
bg=(HBITMAP)LoadImage(NULL,"base.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
MyPaint(hdc);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
x=LOWORD(lParam)-17;
y=HIWORD(lParam)-20;
break;
case WM_RBUTTONDOWN:
WalkRun=2-WalkRun;
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
case VK_UP:
if(num==0||num==2) y-=2*(WalkRun+1);
else
y-=3*(WalkRun+1);
if(y<0) y=0;
dir=3;
if(onum++>delay-WalkRun/2)
{
num++;
onum=0;
}
if(num==4) num=0;
break;
case VK_DOWN:
if(num==0||num==2) y+=2*(WalkRun+1);
else
y+=3*(WalkRun+1);
if(y>402) y=402;
dir=0;
if(onum++>delay-WalkRun/2)
{
num++;
onum=0;
}
if(num==4) num=0;
break;
case VK_LEFT:
if(num==0||num==2) x-=2*(WalkRun+1);
else
x-=3*(WalkRun+1);
if(x<0) x=0;
dir=1;
if(onum++>delay-WalkRun/2)
{
num++;
onum=0;
}
if(num==4) num=0;
break;
case VK_RIGHT:
if(num==0||num==2) x+=2*(WalkRun+1);
else
x+=3*(WalkRun+1);
if(x>607) x=607;
dir=2;
if(onum++>delay-WalkRun/2)
{
num++;
onum=0;
}
if(num==4) num=0;
break;
}
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(boy);
DeleteObject(bg);
ReleaseDC(hWnd,hdc);
ClipCursor(NULL);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}
void MyPaint(HDC hdc)
{
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);
SelectObject(bufdc,boy);
BitBlt(mdc,x,y,w,h,bufdc,num*w,dir*h,SRCAND);
BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);
tPre=GetTickCount();
}
不好意思,我这里用的是我自己的图片,但我不知道怎么上传上来
|
|