Jump to content
  • Advertisement
Sign in to follow this  
superqtip182

Need major help with LoadImage and BITMAP ...

This topic is 4044 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

I'm trying load an image into a window with the open file dialog. The window is the child of a dialog. The first time I try to open the image after a compilation, the image will load and appear just fine. After that, however, no image will load. Here is what you basically need from my code to understand what is wrong:
BOOL LoadBitmapToEdit(HWND hEdit, LPCTSTR pszFileName)
{
	BOOL bSuccess = TRUE;

	if (hBitmap != NULL)
	{
		DeleteObject(hBitmap);
		hBitmap = NULL;
	}

	hBitmap = (HBITMAP)LoadImage(0, pszFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);

 	SendMessage(hwndChild, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);

	return bSuccess;
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
	switch(Message)
	{
		case WM_CREATE:
		{
			
		}
		break;
		case WM_PAINT:
		{			
			BITMAP bm;
			PAINTSTRUCT ps;

			HDC hdc = BeginPaint(hwndChild, &ps);

			HDC hdcMem = CreateCompatibleDC(hdc);
			HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);

			GetObject(hBitmap, sizeof(bm), &bm);
			RECT rect;
			GetClientRect(hwndChild, &rect);
			int windowWidth = rect.right - rect.left;
			int windowHeight = rect.bottom - rect.top;

			BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
			/*StretchBlt(
				  hdc, 
				  0, 
				  0, 
				  windowWidth, 
				  windowHeight, 
				  hdcMem, 
				  0, 
				  0, 
				  bm.bmWidth, 
				  bm.bmHeight, 
				  SRCCOPY);*/

			SelectObject(hdcMem, hbmOld);
			DeleteDC(hdcMem);

			EndPaint(hwndChild, &ps);
		}
		break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				
			}
		break;

		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default: break;
			//return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return (DefWindowProc(hwnd, Message, wParam, lParam));
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	MSG Msg;

	hWnd = CreateDialog(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_MAIN), NULL, MainWndDlgProc);
	dlgWndList.push_back(hWnd);

	WNDCLASSEX wc;

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = NULL;//(HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
 
	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	RECT rect;
	GetClientRect(hWnd, &rect);
	int windowWidth = rect.right - rect.left;
	int windowHeight = rect.bottom - rect.top;

	hwndChild = CreateWindowEx(WS_EX_CLIENTEDGE | WS_EX_ACCEPTFILES , g_szClassName, "", 
				WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL, 
				0, 0, windowWidth / 2 - 100, windowHeight, hWnd, NULL, GetModuleHandle(NULL), NULL);

    while(GetMessage(&Msg, NULL, 0, 0))
    {
		int i=0;
		for (; i<AccessDlgWndList().size(); i++)
		{
			if (IsDialogMessage(AccessDlgWndList(), &Msg) > 0)
			{
				break;
			}
		}
		
		if (i == AccessDlgWndList().size())
		{
			TranslateMessage(&Msg); // ok it must be for some other window
			DispatchMessage(&Msg); 		
		}
	}

	return Msg.wParam;
}


Any help from you guys would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
When you call BeginPaint() or EndPaint() you should pass in the HWND that is passed in as a parameter to your window procedure. Next, you should not always return DefWindowProc() from your window procedure. If you handle a message it might require you to return some other value (as WM_PAINT does). In the case of WM_PAINT, if you handle the message you should return 0. Finally, I dont see anywhere in your code that you actually call loadBitmapToEdit(). Where and when are you calling it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!