Hello everyone!
I would like to start by saying thanks to everyone who takes some time to view this thread and try to help.
I work on a project where .bmp file must be a background image of a main window.
I work in MS Visual Studio Express Edition 2008, in C++, using WIN32 API, on Windows XP.
I have found solution for putting .bmp file as background image here, on CodeProject.
After modifying my code everything worked fine after my application started.The picture was where it supposed to be.
The problem comes when I maximize the main window.
The picture gets duplicated, and after few more minimizes/maximizes, my background becomes entirely white.
I will submit my code in hope that it will help in finding a solution.
Bitmaps dimensions are 256 x 256, and it was created from a .png file via free converter I have found online (the .png file was quite large).
I have even captured some snapshots of the problem in Paint, but I don't know how to make them available to those who view this question ( I will gladly e-mail them).
If there is anything else I can do, just say it, and I will do it.
IMPORTANT UPDATE #1 ( updated on May,5th 2013):
After adding the following code, picture doesn't duplicate anymore, but after few minimizes/maximizes my background becomes white :
case WM_SIZE:
InvalidateRect(hwnd,NULL,FALSE);
break;
IMPORTANT UPDATE #2 ( updated on May,5th 2013):
I have changed my code in the part where the window class is registered from
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
to
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
After few minimizes/maximizes the picture completely dissapears and background becomes black ( just as I have changed it, in window class registration).
I suspect that the problem is some sort of a leak,but I just can't figure it out.
IMPORTANT UPDATE #3 ( updated on May, 5th 2013):
I have solved the problem, and have submitted my solution.
Please comment on it, I will welcome it.
I will also accept other solutions, as well.
Thank you.
Now, the code:
#include "resource.h"
#include <windows.h>
BOOL CALLBACK FilterDialog( HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam )
{
switch( Message)
{
case WM_INITDIALOG:
return TRUE;
break;
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
case IDCANCEL:
EndDialog(hwnd, IDCANCEL);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HBITMAP hBitmap = LoadBitmap( GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_BITMAP1) );
BITMAP bp;
GetObject( hBitmap, sizeof(bp), &bp);
switch(msg)
{
case WM_PAINT:
HDC hdcInst,hdcBitmap;
PAINTSTRUCT ps;
hdcInst = BeginPaint(hwnd,&ps);
hdcBitmap = CreateCompatibleDC(hdcInst);
SelectObject(hdcBitmap, hBitmap);
RECT r;
GetClientRect(hwnd,&r);
StretchBlt( hdcInst, 0, 0, r.right - r.left, r.bottom - r.top, hdcBitmap, 0, 0, bp.bmWidth,bp.bmHeight, MERGECOPY);
DeleteDC(hdcBitmap);
EndPaint(hwnd, &ps);
break;
case WM_COMMAND:
switch( LOWORD(wParam) )
{
case IDM_PRETRAGA1:
DialogBox( GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG1),hwnd,FilterDialog);
break;
default:
break;
}
break;
case WM_CLOSE:
DeleteObject(hBitmap);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
DeleteObject(hBitmap);
PostQuitMessage(0);
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(hInstance,MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.lpszClassName = "Main_Window";
wc.hIconSm = (HICON)LoadImage(hInstance,MAKEINTRESOURCE(IDI_ICON1),IMAGE_ICON,16,16,0);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "Main_Window", "Rudarsko Geoloski Fakultet",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 500, 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;
}