vs2008用c++编写程序调用一幅图片,并在用户窗口区上显示。 表示窗口能出来图片出不来!
#include<windows.h>#include<string.h>#include<stdio.h>LRESULTCALLBACKWndProc(HWND,UIN...
#include <windows.h>
#include <string.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//定义变量
HDC hdc;//设备环境句柄
HDC hdcmem;//内存设备环境句柄
HBITMAP hBm;//位图句柄
BITMAP bm;//BITMAP结构
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst, LPSTR lpszCmdLine,int nCmdShow)
{HWND hwnd;
MSG Msg ;
WNDCLASS wndclass ;
char lpszClassName[] ="位图";
char lpszTitle[]="Example_For_BitMap";
wndclass.style= 0;
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( BLACK_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL) ;
//加载位图
hBm=LoadBitmap(hInstance,"yang.bmp");
//获取位图尺寸
GetObject(hBm, sizeof( BITMAP), (LPVOID)&bm) ;
ShowWindow(hwnd, nCmdShow) ;
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage( &Msg) ;
DispatchMessage( &Msg) ;
}
case WM_PAINT://显示图片
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdcmem,hBm); //将位图选入内存设备环境
//调用函数BitBlt将内存设备环境中的位图拷贝到设备环境中
BitBlt(hdc,180,80,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
EndPaint(hwnd,&ps); //释放设备环境句柄
break;
case WM_DESTROY:
DeleteObject(hBm); //释放位图
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
} 展开
#include <string.h>
#include <stdio.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
//定义变量
HDC hdc;//设备环境句柄
HDC hdcmem;//内存设备环境句柄
HBITMAP hBm;//位图句柄
BITMAP bm;//BITMAP结构
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst, LPSTR lpszCmdLine,int nCmdShow)
{HWND hwnd;
MSG Msg ;
WNDCLASS wndclass ;
char lpszClassName[] ="位图";
char lpszTitle[]="Example_For_BitMap";
wndclass.style= 0;
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( BLACK_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = lpszClassName ;
if( !RegisterClass( &wndclass))
{
MessageBeep(0) ;
return FALSE ;
}
hwnd = CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL) ;
//加载位图
hBm=LoadBitmap(hInstance,"yang.bmp");
//获取位图尺寸
GetObject(hBm, sizeof( BITMAP), (LPVOID)&bm) ;
ShowWindow(hwnd, nCmdShow) ;
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage( &Msg) ;
DispatchMessage( &Msg) ;
}
case WM_PAINT://显示图片
hdc=BeginPaint(hwnd,&ps);
SelectObject(hdcmem,hBm); //将位图选入内存设备环境
//调用函数BitBlt将内存设备环境中的位图拷贝到设备环境中
BitBlt(hdc,180,80,bm.bmWidth,bm.bmHeight,hdcmem,0,0,SRCCOPY);
EndPaint(hwnd,&ps); //释放设备环境句柄
break;
case WM_DESTROY:
DeleteObject(hBm); //释放位图
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
} 展开
展开全部
既然是vs,为啥不用Easyx?Easyx也有GetImageHDC
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我只想说你这是正儿八经的C语言....windowsSDK........
不是C++...............
不是C++...............
追问
时隔五年~我都不晓得我还提过这个问题~哈哈哈~
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询