Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sarlok

Simple question, and I'm sure there's a simple answer...

This topic is 6174 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My task is simple, I want to load a bitmap for my window's background (the background of my window, not MS windows). I thought I got it right, but then I ran my program, and the Window registration failed... What do I have to do to make my program load a bitmap into it's background? For those who are interested, here's what I tried: (just going to put my WinMain() function as I think that's all I need to change) --- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc; HWND hwnd; MSG Msg; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_DBLCLKS; //wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(hInstance, (LPTSTR)IDI_ICON1); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)LoadBitmap(hInstance, (LPCSTR)IDB_BITMAP1); wc.lpszMenuName = NULL; wc.lpszClassName = g_szClassName; wc.hIconSm = LoadIcon(wc.hInstance, (LPTSTR)IDI_ICON2); if(!RegisterClassEx(&wc)) { MessageBox(NULL, "Error at: RegisterClassEx(&wc)", "Fail", MB_ICONEXCLAMATION | MB_OK); return 0; } hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "A Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, hInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, "Error at: hwnd == NULL", "Fail", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } ---- Now, what's missing? Do I need to create a rectangle or something? Thanks in advance. Edit: )Some Typos "When you kill one, it is a tragedy. When you kill ten million, it is a statistic." - Sarlok Edited by - Sarlok on September 20, 2001 4:07:46 PM

Share this post


Link to post
Share on other sites
Advertisement
Try this code... :

WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICON1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

//--!!!!!This is the part you need!!!!!!
HBITMAP bmp = LoadBitmap( hInstance, MAKEINTRESOURCE( IDC_BMP ));
wcex.hbrBackground = (HBRUSH)CreatePatternBrush( bmp );

wcex.lpszMenuName = (LPCSTR)IDC_MENU;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

ATOM atm = RegisterClassEx(&wcex);

//--Insert rest of creation code here...

Hope that helps.....

Zerapolis

Share this post


Link to post
Share on other sites
Hmm...

I tried that, but it still fails when it tries to register my window.

Any other ideas?

"When you kill one, it is a tragedy. When you kill ten million, it is a statistic."
- Sarlok

Share this post


Link to post
Share on other sites
No Worky? But I tested... hmmm, here is the full application. Perhaps there is another problem....


#include "stdafx.h"
#include "resource.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND g_hWnd;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_ICON1 );
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);

HBITMAP bmp = LoadBitmap( hInstance,
MAKEINTRESOURCE(IDB_BITMAP1));
wcex.hbrBackground = (HBRUSH)CreatePatternBrush( bmp );
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "MyClass";
wcex.hIconSm = LoadIcon(wcex.hInstance,
(LPCTSTR)IDI_SMALL);

ATOM atm = RegisterClassEx(&wcex);
if( atm )
{
g_hWnd = CreateWindow( "MyClass", "MyTitle",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0, NULL,
NULL, hInstance, NULL);

if( g_hWnd )
{
ShowWindow( g_hWnd, nCmdShow);
UpdateWindow( g_hWnd);

MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//endwhile
return msg.wParam;
}//endif
}//endif
return TRUE;
}

This should work fine as long as you have the requested resources in the resource file. In your code are you including a class name when you register the class?

If this code still doesn''t work for you, create a new Win32 project, using the "Typical ''Hello World''" template, and replace the WNDCLASSEX data with the data provided here, and add the necessary resources.

Additionally, the above code is a terrible example of a win32 frame, and should be burned after you verify that it works...


Zerapolis

Share this post


Link to post
Share on other sites
Does the window registration fail when you just use something like this:
  
wc.hbrBackground = GetSysColorBrush(COLOR_APPWORKSPACE);


If so, then...well...your problem is elsewhere. Though this most likely isn''t the problem, I''d suggest using the MAKEINTRESOURCE macro for converting resource IDs rather than casting them to LPTSTRs.

According to MSDN, in Windows 95/98, the CreatePatternBrush function does not allow bitmaps of larger than 8x8 pixels--it''ll keep at most that many pixels. So, my suspicion is that you''ll have to do it the old-fashioned way: use BitBlt.

Share this post


Link to post
Share on other sites
Thanks Zerapolis, that second one worked. I''ll take a look at it and see if I can find where I went wrong.

"When you kill one, it is a tragedy. When you kill ten million, it is a statistic."
- Sarlok

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!