Witam.
Postanowiłem zmierzyć się z samym sobą i stworzyć własną wersję starej już gry "kulki". Wszystko idzie jak należy, ale napotkałem problem i nie mam pomysłu jak go rozwiązać.
Chcę wyświetlić bitmapę w postaci kulki. Dołączam bitmapę w pliku Kulki.rc, który znajduje się w folderze resources. Na razie chciałem sprawdzić, czy funkcja wyświetlania bitmapy zadziała i instrukcję jej wyświetlenia napisałem w procedurze okna "WindowProcedure", w komunikacie "WM_CREATE". Napisałem warunek, który sprawdza czy zmienna "kulka_czerw" zadeklarowana jako zmienna "HBITMAP". Program ma wyświetlić okno z napisem "błąd", w przypadku nie załadowania pliku i niestety tak się właśnie dzieję.
Proszę o pomoc. Programuję w darmowym kompilatorze Dev-C++ v. 4.9.9.1.
Oto kod pliku main.cpp:
#include <windows.h>
#include <windows.h>
#include "Kulki.h"
#include <mmsystem.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
BITMAP bm;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
MaxX, /* The programs width */
MaxY, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
HMENU menu = LoadMenu(hThisInstance, MAKEINTRESOURCE(ID_MENU));
SetMenu(hwnd, menu);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
static int x_kursora, y_kursora;
x_kursora = LOWORD(lParam);
y_kursora = HIWORD(lParam);
int przycisniecie;
switch (message) /* handle the messages */
{
case WM_LBUTTONDOWN:
InvalidateRect(hwnd, NULL, FALSE);
break;
case WM_CREATE:
static HINSTANCE hInst = (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE);
static HWND hWndButton6 = CreateWindowEx(
0,
"BUTTON",
"Nowa Gra",
WS_CHILD | WS_VISIBLE | WS_EX_DLGMODALFRAME,
40,
570,
160,
50,
hwnd,
(HMENU) 106,
hInst,
NULL);
kulka_czerw = LoadBitmap(hInst, "K_Czerw.bmp");
if(!kulka_czerw)
{
MessageBoxA(hwnd, "błąd", "", 0);
}
kulka_zielona = LoadBitmap(hInst, "K_Ziel.bmp");
kulka_niebieska = LoadBitmap(hInst, "K_Nieb");
kulka_zolta = LoadBitmap(hInst, "K_Zol.bmp");
kulka_pomaranczowa = LoadBitmap(hInst, "K_Poma.bmp");
rect.left = 5;
rect.top = 5;
rect.right = 1000;
rect.bottom = 1000;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDB_BUTTON1:
flaga = 1;
InvalidateRect(hwnd, &rect, FALSE);
break;
case 106:
NowaGra();
break;
}
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
hdc_czerw = CreateCompatibleDC(hdc);
hdc_ziel = CreateCompatibleDC(hdc);
hdc_nieb = CreateCompatibleDC(hdc);
hdc_zol = CreateCompatibleDC(hdc);
SelectObject(hdc_czerw, kulka_czerw);
SelectObject(hdc_ziel, kulka_zielona);
SelectObject(hdc_nieb, kulka_niebieska);
SelectObject(hdc_zol, kulka_czerw);
SelectObject(hdc_poma, kulka_pomaranczowa);
BitBlt(hdc, 342, 102, 400, 160, hdc_czerw, 342, 102, SRCCOPY);
Rysuj(hdc);
EndPaint(hwnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
Kulki.h:
#define ID_MENU 501
#define IDB_BUTTON1 101
#define IDB_BUTTON2 102
#define IDB_BUTTON3 103
#define IDB_BUTTON4 104
#define IDB_BUTTON5 105
#define IDB_BUTTON6 106
#include <math.h>
HBITMAP kulka_czerw, kulka_zielona, kulka_niebieska, kulka_zolta, kulka_pomaranczowa;
HDC hdc, hdc_czerw, hdc_ziel, hdc_nieb, hdc_zol, hdc_poma;
HWND hwnd;
//zmienne
const int MaxX = 978;
const int MaxY = 752;
int flaga;
int powtorzenie;
int kursor;
bool kratka1, kratka2, kratka3;
int kulka1, kulka2, kulka3;
int color1, color2, color3;
HFONT hFont;
RECT rect;
//Tablice
int xtable[] = {340, 402, 464, 526, 588, 650, 712, 774, 836};
int ytable[] = {100, 162, 224, 286, 348, 410, 472, 534, 596};
char colorsTable[] = {};
//Funkcje
void UtworzPole(int x, int y) //Rysuj pole gry
{
Rectangle(hdc, x, y, x+60, y+60);
}
//Funkcja pola gry
void Rysuj(HDC hdc)
{
Rectangle(hdc, 1, 5, MaxX/3-70, MaxY-52);
//Tytuł
SetBkMode(hdc, TRANSPARENT);
static LOGFONT lf;
lf.lfHeight = 100;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 0;
lf.lfItalic = 1;
lf.lfUnderline = 1;
lf.lfStrikeOut = 0;
lf.lfCharSet = 0;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
lstrcpy (lf.lfFaceName, TEXT ("Gabriola"));
hFont = CreateFontIndirect(&lf);
SelectObject(hdc, hFont);
//tytuł
TextOut(hdc, MaxX/2-21, 1, "Kulki", 18);
//Okno z wynikiem
lf.lfHeight = 50;
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = 0;
lf.lfItalic = 0;
lf.lfUnderline = 1;
lf.lfStrikeOut = 0;
lf.lfCharSet = 0;
lf.lfOutPrecision = 0;
lf.lfClipPrecision = 0;
lf.lfQuality = 0;
lf.lfPitchAndFamily = 0;
lstrcpy (lf.lfFaceName, TEXT ("Gabriola"));
hFont = CreateFontIndirect(&lf);
SelectObject(hdc, hFont);
TextOut(hdc, 50, 80, "Wynik: ", 6);
Rectangle(hdc, 40, 120, 200, 190);
//Okno z rekordem
TextOut(hdc, 40, 210, "Najlepszy wynik: ", 16);
Rectangle(hdc, 40, 250, 200, 320);
// Ilość kolorów w grze
TextOut(hdc, 40, 360, "Ilosc kolorów w grze: ", 23);
//Następne kolory
TextOut(hdc, 40, 420, "Następne kolory:", 16);
Rectangle(hdc, 40, 460, 200, 530);
Ellipse(hdc, 45, 470, 92, 520);
Ellipse(hdc, 97, 470, 145, 520);
Ellipse(hdc, 148, 470, 195, 520);
//Pole gry
int indexX;
int indexY;
for(int i=0; i<81; i++)
{
indexX = i%9;
indexY = int(floor(i/9));
UtworzPole(xtable[indexX], ytable[indexY]);
}
}
void NowaGra()
{
int pole;
int xpola;
int ypola;
int losColor;
for(int i=0; i<3; i++)
{
//losowanie pola
pole=rand()%80;
xpola=pole%9;
ypola= int(floor(pole/9));
//losowanie kolorów
losColor=rand()%4;
}
}
oraz Kulki.rc:
#include "Kulki.h"
kulka_czerw BITMAP "K_Czerw.bmp"
kulka_nieb BITMAP "K_Nieb.bmp"
kulka_ziel BITMAP "K_Ziel.bmp"
kulka_zol BITMAP "K_Zol.bmp"
kulka_poma BITMAP "K_Poma.bmp"
501 MENU
{
POPUP "&Gra"
{
MENUITEM "&Nowa gra" , IDB_BUTTON1
MENUITEM "&Zapisz grę" , IDB_BUTTON2
MENUITEM "&Wczytaj grę" , IDB_BUTTON3
}
POPUP "&Opcje"
{
MENUITEM "&Opcje", IDB_BUTTON4
}
POPUP "&Pomoc"
{
MENUITEM "&Pomoc" , IDB_BUTTON5
}
}
Wklejam cały kod, żeby była całkowita jasność.
Z góry dziękuję.