Jeg havde ikke skrevet det ind i kode, da jeg troede den måske stod et forkert sted, og du så ville skrive den ind i kode, men her er koden så MED dit eksempel(og med fejl):
#include <stdio.h>
#include <iostream.h>
#include <windows.h>
#include "resource.h"
const char g_szClassName[] = "myWindowClass";
const int ID_TIMER = 1;
const int BALL_MOVE_DELTA = 2;
typedef struct _BLOK
{
int x;
int y;
int width;
int height;
int l;
}BLOK;
typedef struct _MUS
{
POINTS mouse;
}MUS;
typedef struct _BALLINFO
{
int width;
int height;
int x;
int y;
int dx;
int dy;
}BALLINFO;
BALLINFO g_ballInfo;
BLOK blok;
MUS mus;
HBITMAP g_hbmBall = NULL;
HBITMAP g_hbmMask = NULL;
HBITMAP g_hbmBlok = NULL;
HBITMAP g_hbmBlokMask = NULL;
HBITMAP CreateBitmapMask(HBITMAP hbmColour, COLORREF crTransparent)
{
HDC hdcMem, hdcMem2;
HBITMAP hbmMask;
BITMAP bm;
GetObject(hbmColour, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hbmColour);
SelectObject(hdcMem2, hbmMask);
SetBkColor(hdcMem, crTransparent);
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
return hbmMask;
}
void UpdateBlok(POINTS p)
{
mus.mouse = p;
blok.x = p.x - blok.width / 2;
}
void Draw(HDC hdc, RECT* prc)
{
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, prc->right, prc->bottom);
HBITMAP hbmOldBuffer = SelectObject(hdcBuffer, hbmBuffer);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmOld = SelectObject(hdcMem, g_hbmMask);
HDC hdcMem1 = CreateCompatibleDC(hdc);
HBITMAP hbmOld1 = SelectObject(hdcMem1, g_hbmBlokMask);
FillRect(hdcBuffer, prc, GetStockObject(WHITE_BRUSH));
blok.y = prc->bottom - blok.height;
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCAND);
BitBlt(hdcBuffer, blok.x, blok.y, blok.width, blok.height, hdcMem1, 0, 0, SRCAND);
SelectObject(hdcMem, g_hbmBall);
BitBlt(hdcBuffer, g_ballInfo.x, g_ballInfo.y, g_ballInfo.width, g_ballInfo.height, hdcMem, 0, 0, SRCPAINT);
SelectObject(hdcMem1, g_hbmBlok);
BitBlt(hdcBuffer, blok.x, blok.y, blok.width, blok.height, hdcMem1, 0, 0, SRCPAINT);
BitBlt(hdc, 0, 0, prc->right, prc->bottom, hdcBuffer, 0, 0, SRCCOPY);
SelectObject(hdcMem, hbmOld);
DeleteDC(hdcMem);
SelectObject(hdcMem1, hbmOld1);
DeleteDC(hdcMem1);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
SelectObject(hdcBuffer, hbmOldBuffer);
DeleteDC(hdcBuffer);
DeleteObject(hbmBuffer);
}
void UpdateBall(RECT* prc)
{
g_ballInfo.x += g_ballInfo.dx;
g_ballInfo.y += g_ballInfo.dy;
if(g_ballInfo.x < 0)
{
g_ballInfo.x = 0;
g_ballInfo.dx = BALL_MOVE_DELTA;
}
else if(g_ballInfo.x + g_ballInfo.width > prc->right)
{
g_ballInfo.x = prc->right - g_ballInfo.width;
g_ballInfo.dx = -BALL_MOVE_DELTA;
}
if(g_ballInfo.y < 0)
{
g_ballInfo.y = 0;
g_ballInfo.dy = BALL_MOVE_DELTA;
}
else if(g_ballInfo.y + g_ballInfo.height >= blok.y)
{
if(g_ballInfo.x + g_ballInfo.width / 2 >= blok.x + blok.l && g_ballInfo.x + g_ballInfo.width / 2 <= blok.x + blok.width - blok.l)
{
if(g_ballInfo.y + g_ballInfo.height >= blok.y + BALL_MOVE_DELTA)
{
g_ballInfo.y = blok.y - g_ballInfo.height;
g_ballInfo.dy = -BALL_MOVE_DELTA;
}
else
{
//calc();
}
}
}
}
VOID CALLBACK MyTimerProc( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
RECT rcClient;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClient);
UpdateBall(&rcClient);
Draw(hdc, &rcClient);
ReleaseDC(hwnd, hdc);
if(g_ballInfo.y > blok.y + blok.height)
{
KillTimer(hwnd, ID_TIMER);
MessageBox(hwnd, "You have lost!", "Program:", MB_OK | MB_ICONINFORMATION);
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
UINT ret;
BITMAP bm;
g_hbmBall = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BALL));
if(g_hbmBall == NULL)
MessageBox(hwnd, "Could not load IDB_BALL!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hbmMask = CreateBitmapMask(g_hbmBall, RGB(0, 0, 0));
if(g_hbmMask == NULL)
MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
GetObject(g_hbmBall, sizeof(bm), &bm);
ZeroMemory(&g_ballInfo, sizeof(g_ballInfo));
g_ballInfo.width = bm.bmWidth;
g_ballInfo.height = bm.bmHeight;
g_ballInfo.dx = BALL_MOVE_DELTA;
g_ballInfo.dy = BALL_MOVE_DELTA;
//////////////////////////////////////////////////////////
g_hbmBlok = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BLOK));
if(g_hbmBlok == NULL)
MessageBox(hwnd, "Could not load IDB_BLOK!", "Error", MB_OK | MB_ICONEXCLAMATION);
g_hbmBlokMask = CreateBitmapMask(g_hbmBlok, RGB(0, 0, 0));
if(g_hbmBlokMask == NULL)
MessageBox(hwnd, "Could not create mask!", "Error", MB_OK | MB_ICONEXCLAMATION);
GetObject(g_hbmBlok, sizeof(bm), &bm);
ZeroMemory(&blok, sizeof(blok));
blok.width = bm.bmWidth;
blok.height = bm.bmHeight;
ZeroMemory(&mus, sizeof(mus));
//////////////////////////////////////////////////////////
char s[20];
UNIT i;
DWORD d;
sprintf(s, "%d %d", i, d);
MessageBox(0, s, "", MB_OK);
ret = SetTimer(hwnd, ID_TIMER, 10, (TIMERPROC) MyTimerProc);
if(ret == 0)
MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_PAINT:
{
RECT rcClient;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rcClient);
Draw(hdc, &rcClient);
EndPaint(hwnd, &ps);
}
break;
case WM_TIMER:
{
RECT rcClient;
HDC hdc = GetDC(hwnd);
GetClientRect(hwnd, &rcClient);
UpdateBall(&rcClient);
Draw(hdc, &rcClient);
ReleaseDC(hwnd, hdc);
if(g_ballInfo.y > blok.y + blok.height)
{
KillTimer(hwnd, ID_TIMER);
MessageBox(hwnd, "You have lost!", "Program:", MB_OK | MB_ICONINFORMATION);
}
}
break;
case WM_MOUSEMOVE:
{
UpdateBlok(MAKEPOINTS (lParam));
}
break;
case WM_DESTROY:
{
KillTimer(hwnd, ID_TIMER);
DeleteObject(g_hbmBall);
DeleteObject(g_hbmMask);
DeleteObject(g_hbmBlok);
DeleteObject(g_hbmBlokMask);
PostQuitMessage(0);
}
break;
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_KEYDOWN:
{
if (wParam == 13){
int test = wParam;
MessageBox(hwnd, "test", "This program is:", MB_OK | MB_ICONINFORMATION);
}
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Ping Pong!",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}