Okay nu har jeg så fået tegnet det og er gået videre til at bevæge manden fra side til side:
// Her inkluderes nogle header filer
#include <windows.h>
#include <string>
// Program variabler
HINSTANCE InstanceHandle;
HWND MainWindow;
HBITMAP BackGroundBitmap, TueBitmap, ManBitmap;
UINT TueWidth, TueHeight;
UINT ManWidth, ManHeight;
UINT BackGroundWidth, BackGroundHeight;
int SitePos = 5;
std::string mand = "mandland.bmp";
// 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);
TueBitmap = LoadBMP("tue.bmp", &TueWidth, &TueHeight);
ManBitmap = LoadBMP(mand.c_str(), &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, ManBitmap);
BitBlt(dc, SitePos, 0, 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:
SitePos = SitePos + 70;
// Her skal den opdatere billedet
break;
case VK_RIGHT:
SitePos = SitePos - 70;
// Her skal den opdatere billedet
break;
case VK_UP:
break;
case VK_DOWN:
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 mangler stadig at finde ud af hvordan jeg kan opdatere billedet! Så snart jeg trykker på venstre piletast ændre den mandens billede position men det rykker manden sig jo ikke af så jeg skal bruge et eller andet der opdatere billedet så man kan se manden på den nye position! kan du hjælpe???