Skip to main content
gamedev.net
🔒 Locked

Windows programming headache

Started by EccoDomi 4 replies 800+ views
Original Post
EccoDomi
EccoDomi
I'm sorry to be such a newbie, but frankly I am. I'm in school right now hopefully to be a game dev. Fist thing I wanted to do was create a simple and cheesy launcher. I got it all running but I cant seem to texture my buttons. I've spent the last 5 hours scouring the web (googling for examples), read the books I have (PEtzold's Programming Windows) and generally banging my head on my desk. I know the right syntax, but some how the buttons show up with no texture (bitmap). So I figured I'd paste an snippet of my source and hopefully you guys can tell me what I'm doing wrong. Code: (In WinProc) ... HBITMAP hBmPlay; HWND hBtnPlay; ... case WM_CREATE: { hBtnPlay = CreateWindowEx(NULL, TEXT ("button"), TEXT (""), WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | BS_BITMAP, 100, 250, 80, 30, hwnd, NULL, hInstance_app, NULL); hBmPlay = LoadBitmap(hInstance_app, TEXT("play")); // play in resource file line: play BITMAP DISCARDABLE "play.bmp" SendMessage(hBtnPlay, BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hBmPlay); return 0; } ... So what's wrong? Every exampleon the web has shown me todo it this way. I'm sending the message to the handle for the child window, and giving it the handle to the bitmap. The button should be created right because I can see it in the program and the program compiles with no errors... Anyone shed some light? -J [edited by - EccoDomi on March 11, 2004 1:45:09 PM]
Xiachunyi
Xiachunyi
Add this after a function you think is the problem:

char buffer[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),buffer,256,0);
MessageBox(hwnd,buffer,"Error",MB_OK|MB_ICONERROR);


The TEXT("play") section in the LoadBitmap function looks wierd, but that is because I''ve never used a resource name like that, I always used something like: MAKEINTRESOURCE(IDB_BUTTON);
EccoDomi
EccoDomi
HAHAHA! You rock Xiachunyi. Thanks!

Error: The specified resource name cannot be found in the image file.

Heh. Now I gotta figure that one out. I think you''re right, it has something to do with my TEXT command (it''s out of Petzold''s book though).

My paint switch uses it though, so thats odd:

case WM_PAINT:
{
hdc = beginpaint(hwnd, &ps);
hdcBitmapImg = CreateCompatibleDC(NULL);
hBmBg = LoadBitmap(hInstance_app, TEXT("test"));
GetObject(hBmBg, sizeof(BITMAP), &bitmap);
SelectObject(hdcBitmapImg, hBmBg);
BitBlt(hdc, 0, 0, 400, 300, hdcBitmapImg, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
}

So I dunno why that would work and not the above lines in WM_CREATE (which I copied the LoadBitmap function).

I''ll fudge with it, thanks though!

-J
daerid
daerid
Not only that, but you might try making sure that your HWND for the button isn''t local only to the Window Procedure (make it global or static).


daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
EccoDomi
EccoDomi
quote:
#define WIN32_LEAN_AND_MEAN
#define WINDOW_CLASS_NAME loadmain

#include <windows.h>
#include <windowsx.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "resources.h"

HINSTANCE hInstance_app;
HWND hBtnPlay, hBtnExit;

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
HBITMAP hBmBg=NULL, hBmPlay=NULL, hBmExit=NULL;

PAINTSTRUCT ps;
HDC hdc, hdcBitmapImg;
BITMAP bitmap;

switch(msg)
{
case WM_CLOSE:
{
int result = MessageBox(hwnd, "Are you sure you want to quit?", "Quit", MB_YESNO | MB_ICONQUESTION);
if( result == IDYES )
return ( DefWindowProc(hwnd, msg, wparam, lparam) );
else
return 0;
}
break;

case WM_CREATE:
{
/* hBtnPlay = CreateWindowEx(NULL,
TEXT ("button"),
TEXT (""),
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | BS_BITMAP,
100, 250,
80, 30,
hwnd,
NULL,
hInstance_app,
NULL);

hBtnExit = CreateWindowEx(NULL,
TEXT ("button"),
TEXT (""),
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | BS_BITMAP,
225, 250,
75, 25,
hwnd,
NULL,
hInstance_app,
NULL);

hBmPlay = LoadBitmap(hInstance_app, MAKEINTRESOURCE(1000));
char buffer[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, 256, 0);
MessageBox(hwnd, buffer, "Error", MB_OK | MB_ICONERROR);
SendMessage(hBtnPlay, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hBmPlay);

hBmExit = LoadBitmap(hInstance_app, TEXT("exit"));
char buffer2[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer2, 256, 0);
MessageBox(hwnd, buffer2, "Error", MB_OK | MB_ICONERROR);
SendMessage(hBtnExit, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hBmExit);
*/
return 0;
}
break;

case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
hdcBitmapImg = CreateCompatibleDC(NULL);
hBmBg = LoadBitmap(hInstance_app, TEXT("test"));
/* char buffer3[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer3, 256, 0);
MessageBox(hwnd, buffer3, "Error", MB_OK | MB_ICONERROR);
*/ GetObject(hBmBg, sizeof(BITMAP), &bitmap);
SelectObject(hdcBitmapImg, hBmBg);
BitBlt( hdc, 0, 0, 400, 300, hdcBitmapImg, 0, 0, SRCCOPY);

hBtnPlay = CreateWindowEx(NULL,
TEXT ("button"),
TEXT (""),
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | BS_BITMAP,
100, 250,
80, 30,
hwnd,
NULL,
hInstance_app,
NULL);

hBtnExit = CreateWindowEx(NULL,
TEXT ("button"),
TEXT (""),
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW | BS_BITMAP,
225, 250,
75, 25,
hwnd,
NULL,
hInstance_app,
NULL);

hBmPlay = LoadBitmap(hInstance_app, MAKEINTRESOURCE(1000));
char buffer[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, 256, 0);
MessageBox(hwnd, buffer, "Error", MB_OK | MB_ICONERROR);
SendMessage(hBtnPlay, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hBmPlay);

hBmExit = LoadBitmap(hInstance_app, TEXT("exit"));
/* char buffer2[256];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer2, 256, 0);
MessageBox(hwnd, buffer2, "Error", MB_OK | MB_ICONERROR);
*/ SendMessage(hBtnExit, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hBmExit);

EndPaint(hwnd, &ps);
return 0;
}
break;

case WM_DESTROY:
{
DeleteObject(hBmBg);
PostQuitMessage(0);
return 0;
}
break;

default:
break;
}

return ( DefWindowProc(hwnd, msg, wparam, lparam) );
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Create the class
WNDCLASSEX loadmain;

// Create variables
HWND hwnd;
MSG msg;
HDC hDC;

// Define the class
loadmain.cbSize = sizeof(WNDCLASSEX); // Class Size
loadmain.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS; // Window Style
loadmain.lpfnWndProc = WinProc; // Procedure Function
loadmain.cbClsExtra = 0;
loadmain.cbWndExtra = 0;
loadmain.hInstance = hInstance;
loadmain.hIcon = LoadIcon(hInstance, "MAIN_ICON");
loadmain.hCursor = LoadCursor(NULL, IDC_ARROW);
loadmain.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
loadmain.lpszMenuName = NULL;
loadmain.lpszClassName = "loadmain";
loadmain.hIconSm = LoadIcon(hInstance, "MAIN_ICON");

// Register the class with Windows
if( !RegisterClassEx(&loadmain) )
return 0;

// Create a window with the class
if( !( hwnd = CreateWindowEx(NULL,
"loadmain",
"Launcher",
WS_POPUP | WS_VISIBLE,
(GetSystemMetrics(SM_CXSCREEN) / 2 - 200),
(GetSystemMetrics(SM_CYSCREEN) / 2 - 150),
400,
300,
NULL,
NULL,
hInstance,
NULL)))

return 0;

hInstance_app = hInstance;

// Show the window
ShowWindow(hwnd, nCmdShow);

// Update the window
UpdateWindow(hwnd);

hDC = GetDC(hwnd);

// MAIN GAME LOOP!
while( true )
{
if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
{
if( msg.message == WM_QUIT )
break;

// Translate msg
TranslateMessage(&msg);

// Send msg to WinProc
DispatchMessage(&msg);
}

// Game main entry point
//Game_Main();
}

return (msg.wParam);
}


Thats my source... bleh. I cant figure it out. tickin me off... If I create the buttons in WM_PAINT I can see the program trying to draw them (and with the debug msg box I get about 25 of those) but still the same thing, my background image (which works fine!) and two grey boxes where the buttons should be... arg!

-J

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.