Okay jeg har prøvet det med at loade begge billeder:
// Her inkluderes nogle header filer
#include <windows.h>
#include <string>
// Program variabler
HINSTANCE InstanceHandle;
HWND MainWindow;
HBITMAP BackGroundBitmap, TueBitmap, ManBitmap, ManTueBitmap;
UINT ManTueWidth, ManTueHeight;
UINT TueWidth, TueHeight;
UINT ManWidth, ManHeight;
UINT BackGroundWidth, BackGroundHeight;
int DownTimes = 0;
int LeftTimes = 0;
int DownPos = 0;
int SitePos = 5;
std::string Man = "ManBitmap";
// Her checkes om billedet kan læses
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
HBITMAP LoadBMP(const char *FileName, UINT *Width, UINT *Height)
{
HBITMAP BitMap = (HBITMAP )LoadImage(0, FileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(!BitMap)
{
std::string Msg = "Failed to load image" + std::string(FileName);
MessageBox(0, Msg.c_str(), "BitMap", MB_OK);
}
else
{
BITMAP BitmapInfo;
GetObject(BitMap, sizeof(BITMAP), &BitmapInfo);
*Width = BitmapInfo.bmWidth;
*Height = BitmapInfo.bmHeight;
}
return BitMap;
}
// Her sættes nogle indstillinger til vinduet
HWND CreateMainWindow()
{
WNDCLASS wc;
memset(&wc, 0, sizeof(WNDCLASS));
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
wc.lpfnWndProc = (WNDPROC )MainWndProc;
wc.hInstance = InstanceHandle;
wc.hbrBackground = (HBRUSH )(COLOR_BTNFACE + 1);
wc.lpszClassName = "SimpleWinWndClass";
wc.lpszMenuName = 0;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClass(&wc))
return 0;
// Her skal billedstierne skrives
BackGroundBitmap = LoadBMP("baggrund.bmp", &BackGroundWidth, &BackGroundHeight);
ManTueBitmap = LoadBMP("mandtue.bmp", &ManWidth, &ManHeight);
TueBitmap = LoadBMP("tue.bmp", &TueWidth, &TueHeight);
ManBitmap = LoadBMP("mandland.bmp", &ManWidth, &ManHeight);
// Her laves vinduet
return CreateWindow("SimpleWinWndClass",
"NumberGame",
WS_MINIMIZEBOX | WS_VISIBLE |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MAXIMIZEBOX |
WS_CAPTION | WS_BORDER | WS_SYSMENU | WS_THICKFRAME,
100, 100, 788, 400,
0,
0,
InstanceHandle,
0);
}
// Her er en funktion der tegner billedet på skærmen
void OnPaint(HWND hwnd)
{
PAINTSTRUCT PaintStruct;
HDC dc = BeginPaint(hwnd, &PaintStruct);
HDC MemDC = CreateCompatibleDC(dc);
HBITMAP OldBitmap = (HBITMAP )SelectObject(MemDC, BackGroundBitmap);
BitBlt(dc, 0, 0, BackGroundWidth, BackGroundHeight, MemDC, 0, 0, SRCCOPY);
SelectObject(MemDC, TueBitmap);
BitBlt(dc, 10, 150, TueWidth, TueHeight, MemDC, 0, 0, SRCCOPY);
SetBkMode(dc, TRANSPARENT);
TextOut(dc, 35, 170, "9", 1);
SelectObject(MemDC, TueBitmap);
BitBlt(dc, 80, 150, TueWidth, TueHeight, MemDC, 0, 0, SRCCOPY);
SetBkMode(dc, TRANSPARENT);
TextOut(dc, 105, 170, "0", 1);
SelectObject(MemDC, TueBitmap);
BitBlt(dc, 150, 150, TueWidth, TueHeight, MemDC, 0, 0, SRCCOPY);
SetBkMode(dc, TRANSPARENT);
TextOut(dc, 175, 170, "7", 1);
SelectObject(MemDC, TueBitmap);
BitBlt(dc, 220, 150, TueWidth, TueHeight, MemDC, 0, 0, SRCCOPY);
SetBkMode(dc, TRANSPARENT);
TextOut(dc, 245, 170, "2", 1);
SelectObject(MemDC, Man);
BitBlt(dc, SitePos, DownPos, ManWidth, ManHeight, MemDC, 0, 0, SRCCOPY);
SelectObject(MemDC, OldBitmap);
DeleteObject(MemDC);
EndPaint(hwnd, &PaintStruct);
}
// MessageLoop her styres hele programmet
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
switch (wParam)
{
case VK_LEFT:
if(LeftTimes == 0) {
}
else
{
LeftTimes = LeftTimes - 1;
SitePos = SitePos - 70;
InvalidateRect(hwnd, 0, TRUE);
}
break;
case VK_RIGHT:
if(LeftTimes == 9) {
}
else {
LeftTimes = LeftTimes + 1;
SitePos = SitePos + 70;
InvalidateRect(hwnd, 0, TRUE);
}
break;
case VK_UP:
switch(DownTimes)
{
case 0:
break;
case 1:
DownTimes = DownTimes - 1;
Man = "ManBitmap";
DownPos = DownPos - 170;
InvalidateRect(hwnd, 0, TRUE);
break;
case 2:
DownTimes = DownTimes - 1;
Man = "ManTueBitmap";
DownPos = DownPos - 150;
InvalidateRect(hwnd, 0, TRUE);
break;
}
break;
case VK_DOWN:
switch(DownTimes)
{
case 0:
DownTimes = DownTimes + 1;
Man = "ManTueBitmap";
DownPos = DownPos + 170;
InvalidateRect(hwnd, 0, TRUE);
break;
case 1:
DownTimes = DownTimes + 1;
Man = "ManBitmap";
DownPos = DownPos + 150;
InvalidateRect(hwnd, 0, TRUE);
break;
}
break;
}
break;
case WM_PAINT:
OnPaint(hwnd); // Her kaldes funktionen OnPaint
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
// Her checkes om der sker fejl i oprettelsen af vinduet
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
INT nCmdShow)
{
InstanceHandle = hInstance;
if((MainWindow = CreateMainWindow()) == (HWND )0)
{
MessageBox(0, "Failed to create MainWindow!", "Warning", MB_OK);
return 0;
}
ShowWindow(MainWindow, SW_SHOW);
MSG Msg;
while(GetMessage(&Msg, 0, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Men jeg får disse fejl:
C:\\BORLAND\\BCC55\\BIN>makeobj game/game
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
game/Game.cpp:
Error E2034 game/Game.cpp 125: Cannot convert 'std::string' to 'void *' in funct
ion OnPaint(HWND__ *)
Error E2340 game/Game.cpp 125: Type mismatch in parameter 2 (wanted 'void *', go
t 'std::string') in function OnPaint(HWND__ *)
*** 2 errors in Compile ***
Jeg ved godt det er fordi jeg ikke kan bruge en string inde i den void men hvordan skal jeg ellers gøre??