小弟在编辑位图是,程序老是出错,我请教了好多高手,都不知道哪里错误,请各位指点一下。特将原代码如下:#include<windows.h>
HWND hwndmain;
long WINAPI Wndproc(HWND hwnd,UINT imessage,UINT wparam,LONG lparam);
BOOL InitWindowsClass(HINSTANCE hinstance);
BOOL InitWindows(HINSTANCE hinstance,int ncmdshow);
HDC hdc;
HDC hdcmem;
HBITMAP hbm;
BITMAP bm;
int iy=60;
int iwindowswidth;
int iwindowsheight;
char cupwarn[]="已经到达顶部";
char cdownwarn[]="已经到达底部";
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevInstance,
LPSTR lpcmdLine,
int ncmdshow)
{
MSG message;
iwindowswidth=2*bm.bmWidth;
iwindowsheight=2*bm.bmHeight;
if(!InitWindowsClass(hinstance))
return FALSE;
if(!InitWindows(hinstance,ncmdshow))
return FALSE;
while(GetMessage(&message,0,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
long WINAPI Wndproc(HWND hwnd,UINT imessage,UINT wparam,LONG lparam)
{
PAINTSTRUCT ps;
switch(imessage)
{
case WM_LBUTTONDOWN:
iy=iy-10;
InvalidateRect(hwnd,NULL,1);
break;
case WM_RBUTTONDOWN:
IY=IY+10;
InvalidateRect(hwnd,NULL,1);
break;
case WM_KEYDOWN:
switch(wparam)
{
case VK_UP:
iy=iy-10;
break;
case VK_DOWN:
iy=iy+10;
break;
}
InvalidateRect(hwnd,NULL,1);
break;
case WM_CREATE:
hdc=GetDC(hwnd);
hbm=LoadBitmap(hinstance,"pic");
GetObject(hbm,sizeof(BITMAP),(LPVOID)&bm);
ReleaseDC(hwnd,hdc);
case WM_PAINT:
hdc=BeginPain(hwnd,&ps);
hdcmem=CreateCompatibleDC(hdc);
if(iy>0&&iy<iwindowsheight/2)
{
SelectObject(hdcmem,hbm);
BitBlt(hdc,60,iy,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
}
else if(iy<=0)
{
TextOut(hdc,0,0,cupwarn,sizeof(cupwarn));
}
else
{
TextOut(hdc,0,0,cdownwarn,sizeof(cdownwarn));
}
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
DeleteObject(hbm);
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hwnd,imessage,wparam,lparam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hinstance,int ncmdshow)
{
HWND hwnd;
hwnd=CreateWindow("bitmap",
"位图示例",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
iwindowswidth;
iwindowsheight;
NULL,
NULL,
hinstance,NULL);
if(!hwnd)
return FALSE;
hwndmain=hwnd;
ShowWindow(hwnd,ncmdshow);
UpdateWindow(hwnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hinstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hinstance;
wndclass.lpfnWndProc=Wndproc;
wndclass.lpszClassName="bitmap";
wndclass.lpszMenuName=NULL;
wndclass.style=0;
return RegisterClass(&wndclass);
}
资源文件为:
pic BITMAP "pic.bmp"
HWND hwndmain;
long WINAPI Wndproc(HWND hwnd,UINT imessage,UINT wparam,LONG lparam);
BOOL InitWindowsClass(HINSTANCE hinstance);
BOOL InitWindows(HINSTANCE hinstance,int ncmdshow);
HDC hdc;
HDC hdcmem;
HBITMAP hbm;
BITMAP bm;
int iy=60;
int iwindowswidth;
int iwindowsheight;
char cupwarn[]="已经到达顶部";
char cdownwarn[]="已经到达底部";
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevInstance,
LPSTR lpcmdLine,
int ncmdshow)
{
MSG message;
iwindowswidth=2*bm.bmWidth;
iwindowsheight=2*bm.bmHeight;
if(!InitWindowsClass(hinstance))
return FALSE;
if(!InitWindows(hinstance,ncmdshow))
return FALSE;
while(GetMessage(&message,0,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
long WINAPI Wndproc(HWND hwnd,UINT imessage,UINT wparam,LONG lparam)
{
PAINTSTRUCT ps;
switch(imessage)
{
case WM_LBUTTONDOWN:
iy=iy-10;
InvalidateRect(hwnd,NULL,1);
break;
case WM_RBUTTONDOWN:
IY=IY+10;
InvalidateRect(hwnd,NULL,1);
break;
case WM_KEYDOWN:
switch(wparam)
{
case VK_UP:
iy=iy-10;
break;
case VK_DOWN:
iy=iy+10;
break;
}
InvalidateRect(hwnd,NULL,1);
break;
case WM_CREATE:
hdc=GetDC(hwnd);
hbm=LoadBitmap(hinstance,"pic");
GetObject(hbm,sizeof(BITMAP),(LPVOID)&bm);
ReleaseDC(hwnd,hdc);
case WM_PAINT:
hdc=BeginPain(hwnd,&ps);
hdcmem=CreateCompatibleDC(hdc);
if(iy>0&&iy<iwindowsheight/2)
{
SelectObject(hdcmem,hbm);
BitBlt(hdc,60,iy,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
}
else if(iy<=0)
{
TextOut(hdc,0,0,cupwarn,sizeof(cupwarn));
}
else
{
TextOut(hdc,0,0,cdownwarn,sizeof(cdownwarn));
}
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
DeleteObject(hbm);
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hwnd,imessage,wparam,lparam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hinstance,int ncmdshow)
{
HWND hwnd;
hwnd=CreateWindow("bitmap",
"位图示例",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
iwindowswidth;
iwindowsheight;
NULL,
NULL,
hinstance,NULL);
if(!hwnd)
return FALSE;
hwndmain=hwnd;
ShowWindow(hwnd,ncmdshow);
UpdateWindow(hwnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hinstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hinstance;
wndclass.lpfnWndProc=Wndproc;
wndclass.lpszClassName="bitmap";
wndclass.lpszMenuName=NULL;
wndclass.style=0;
return RegisterClass(&wndclass);
}
资源文件为:
pic BITMAP "pic.bmp"
#include <windows.h>
#include "stdafx.h"
HWND hwndmain;
long WINAPI Wndproc(HWND hwnd,UINT imessage,UINT wparam,LONG lparam);
BOOL InitWindowsClass(HINSTANCE hinstance);
BOOL InitWindows(HINSTANCE hinstance,int ncmdshow);
HDC hdc;
HDC hdcmem;
HBITMAP hbm;
BITMAP bm;
HINSTANCE hInstance;
int iy=60,ix=60;
int iwindowswidth;
int iwindowsheight;
char cupwarn[]="已经到达顶部";
char cdownwarn[]="已经到达底部";
char cleftwarn[]="已经到达左部";
char crightwarn[]="已经到达右部";
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevInstance,
LPSTR lpcmdLine,
int ncmdshow)
{
MSG message;
iwindowswidth=2*bm.bmWidth;
iwindowsheight=2*bm.bmHeight;
if(!InitWindowsClass(hinstance))
return FALSE;
hInstance=hinstance;
if(!InitWindows(hinstance,ncmdshow))
return FALSE;
while(GetMessage(&message,0,0,0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return message.wParam;
}
long WINAPI Wndproc(HWND hwnd,UINT imessage,UINT wparam,LONG lparam)
{
PAINTSTRUCT ps;
switch(imessage)
{
case WM_SIZE:
iwindowsheight=HIWORD(lparam);
iwindowswidth=LOWORD(lparam);
break;
case WM_LBUTTONDOWN:
iy=iy-10;
InvalidateRect(hwnd,NULL,1);
break;
case WM_RBUTTONDOWN:
iy=iy+10;
InvalidateRect(hwnd,NULL,1);
break;
case WM_KEYDOWN:
switch(wparam)
{
case VK_UP:
iy=iy-10;
break;
case VK_DOWN:
iy=iy+10;
break;
case VK_LEFT:
ix-=10;
break;
case VK_RIGHT:
ix+=10;
break;
}
InvalidateRect(hwnd,NULL,1);
break;
case WM_CREATE:
hdc=GetDC(hwnd);
hbm=LoadBitmap(hInstance,"pic");
GetObject(hbm,sizeof(BITMAP),(LPVOID)&bm);
ReleaseDC(hwnd,hdc);
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
if((iy>0 && iy<iwindowsheight) && (ix>0 && ix<iwindowswidth))
{
hdcmem=CreateCompatibleDC(hdc);
SelectObject(hdcmem,hbm);
BitBlt(hdc,ix,iy,bm.bmWidth,bm.bmHeight,
hdcmem,0,0,SRCCOPY);
DeleteObject(hdcmem);//应删除之
}
if(iy<=0)
{
TextOut(hdc,0,0,cupwarn,sizeof(cupwarn)-1);
}
else if(iy>=iwindowsheight)
{
TextOut(hdc,0,0,cdownwarn,sizeof(cdownwarn)-1);
}
if(ix<=0)
{
TextOut(hdc,100,0,cleftwarn,sizeof(cleftwarn)-1);
}
else if(ix>=iwindowswidth)
{
TextOut(hdc,100,0,crightwarn,sizeof(crightwarn)-1);
}
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
DeleteObject(hbm);
PostQuitMessage(0);
return 0;
default:
return(DefWindowProc(hwnd,imessage,wparam,lparam));
}
return 0;
}
BOOL InitWindows(HINSTANCE hinstance,int ncmdshow)
{
HWND hwnd;
hwnd=CreateWindow("bitmap",
TEXT("位图示例"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hinstance,NULL);
if(!hwnd) return FALSE;
hwndmain=hwnd;
ShowWindow(hwnd,ncmdshow);
UpdateWindow(hwnd);
return TRUE;
}
BOOL InitWindowsClass(HINSTANCE hinstance)
{
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hinstance;
wndclass.lpfnWndProc=Wndproc;
wndclass.lpszClassName="bitmap";
wndclass.lpszMenuName=NULL;
wndclass.style=0;
return RegisterClass(&wndclass);
}
"位图示例",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
0,
iwindowswidth;
iwindowsheight;
NULL,
NULL,
hinstance,NULL);
下面两个参数后面怎么是分号???
iwindowswidth;
iwindowsheight;
1.普通的输入错误:大小写问题,标点问题;
2.生成窗口时 iwindowswidth,iwindowsheight未曾初始化,就是可通过编译(无第一项错误),因iwindowswidth,iwindowsheight值未知,也无法显示位图,应响应WM_SIZE信息,以改变iwindowswidth,iwindowsheight值.以正确显示!
3.在TextOut中sizeof(字符串)时会多一,故会显示一个汉字的乱码(这个我也奇怪)!
4.最好生成一个静态的memhdc,就可以不用象我改的那样每次生成和释放HDC资源!
上文如不注销会出错.同样,位图对象等资源应在WM_DESTROY信息中释放!
5.其实你的程序没在实现所有功能,试试加上MOUSE的拖拉位图的功能,并解决图像运动中的闪烁(我早已知道解决方法);
6.以上是早上改的代码,因昨天一夜没睡,不保证完全正确,不过我的VC6+windows2000下是可能正常通过的!