candc32 wrote:Samuel wrote:For Bitmaps, you could use the windows function LoadImage(); or you could use the glaux library. I recommend LoadImage() because glaux has memory leaks.
so I do this?
- Code: Select all
#include <windows>
using namespace std;
int main()
{
loadimage("test.bmp");
}

Heck no! Nothing is that simple with the WinAPI.
I put something together a while back that loads/displays more than just bitmap file formats. The following code loads and displays the file "pic.bmp" from the same directory as the executable. You can display most common file formats (gif, etc.), by simply changing the `const char* picture_file="...";' line in WndProc().
main.cpp
[syntax="cpp"]#include "picture.h"
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
static LPPICTURE pic=0;
static HDC hdc;
static PAINTSTRUCT ps;
static const char* picture_file="pic.bmp";
switch(msg){
case WM_CREATE:
pic=LoadPictureFile(picture_file);
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
if(pic){
long hmWidth,hmHeight;
pic->get_Width(&hmWidth);
pic->get_Height(&hmHeight);
int nWidth=MulDiv(hmWidth,GetDeviceCaps(hdc,LOGPIXELSX),2540),
nHeight=MulDiv(hmHeight,GetDeviceCaps(hdc,LOGPIXELSY),2540);
RECT rc;
GetClientRect(hwnd,&rc);
pic->Render(hdc,0,0,nWidth,nHeight,0,hmHeight,hmWidth,-hmHeight,&rc);
}
EndPaint(hwnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
} return 0;
}
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR args,int nShow){
MSG msg;
WNDCLASS wc={0};
wc.lpszClassName="Win32App";
wc.hInstance=hInst;
wc.hbrBackground=GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc=WndProc;
wc.hCursor=LoadCursor(0,IDC_ARROW);
RegisterClass(&wc);
CreateWindow(wc.lpszClassName,"Image Loader",WS_OVERLAPPEDWINDOW|
WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT,400,400,0,0,hInst,0);
while(GetMessage(&msg,0,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}[/syntax]
picture.h
[syntax="cpp"]#ifndef _PICTURE_H_
#define _PICTURE_H_
#include <windows.h>
#include <olectl.h>
#include <crtdbg.h>
LPPICTURE LoadPictureFile(LPCTSTR);
#endif[/syntax]
picture.cpp
[syntax="cpp"]#include "picture.h"
LPPICTURE LoadPictureFile(LPCTSTR szFile){
HANDLE hFile=CreateFile(szFile,GENERIC_READ,0,0,OPEN_EXISTING,0,0);
DWORD dwFileSize=GetFileSize(hFile,0);
LPVOID pvData=0;
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
pvData=GlobalLock(hGlobal);
DWORD dwBytesRead=0;
BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwBytesRead,0);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
LPSTREAM pstm=0;
HRESULT hr=CreateStreamOnHGlobal(hGlobal,1,&pstm);
LPPICTURE pic;
hr=OleLoadPicture(pstm,dwFileSize,0,IID_IPicture,(LPVOID*)&pic);
pstm->Release();
return pic;
}[/syntax]
I havn't tested this for a while, but I think it worked the last time I checked. If you have any problems compiling the code, then post back here. Good luck!