• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

GDI Plus Draw and Message Handling

Hello,

In the past I used a function to draw a image to a dialog. This works and I was able to receive the Message like RBUTTONDOWN on the dialog.
Now I chanegd it to DrawImage and now I do not receive any message while the image is drawn.

Any suggestion what is wrong?

Ocrana
Old:
LRESULT CVideoWindow::OnUpdateVideoPicture(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	HBITMAP hBitmap = (HBITMAP)wParam;
	BITMAP btm;
	GetObject(hBitmap, sizeof(BITMAP), &btm);
	if(!m_hDC)
	{
		m_hDC = GetDC();
	}
	if(!m_hMemDC && m_hDC)
	{
		m_hMemDC = CreateCompatibleDC(m_hDC);
	}
		
	SelectObject(m_hMemDC, hBitmap);

	RECT rect; 
	GetClientRect(&rect); // rectangle for the primary surface

	if(btm.bmWidth < rect.right || btm.bmHeight < rect.bottom) SetStretchBltMode(m_hDC, COLORONCOLOR);
	else													   SetStretchBltMode(m_hDC, HALFTONE);

	StretchBlt(m_hDC, rect.left, 0, rect.right, rect.bottom, m_hMemDC, 0, 0, btm.bmWidth, btm.bmHeight, SRCCOPY);

	return 0;
}

New:
LRESULT CVideoWindow::OnUpdateVideoPicture(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	HBITMAP hBitmap = (HBITMAP)wParam;
	BITMAP btm;
	GetObject(hBitmap, sizeof(BITMAP), &btm);
	if(!m_hDC)
	{
		m_hDC = GetDC();	

	}


	RECT rect; 
	GetClientRect(&rect); // rectangle for the primary surface

	Gdiplus::Bitmap SrcBitmap(hBitmap,NULL);

	Graphics graphics(m_hDC);
	
    graphics.DrawImage(&SrcBitmap, 
            rect.left, rect.top,
            rect.right - rect.left, 
            rect.bottom - rect.top);

	return 0;
}

Open in new window

0
Ocrana
Asked:
Ocrana
  • 3
  • 2
1 Solution
 
pgnatyukCommented:
There is no obvious relation between WM_RBUTTONDOWN and GDI+.
Check WM_LBUTTONDOWN, WM_MOUSEMOVE. Your window should be enabled.

Your "old" code has a serious problem - after StretchBlt you need to select old bitmap in the HDC and delete the temporary dc. I hope you do it somewhere when your window is destroyed. But you need to release the DC you obtain in GetDC.

Same is correct for the "new" code.

0
 
OcranaAuthor Commented:
Hi,

the problem is, I do not get any message. Not LButton, not DblClick, nothing. But if I go back to GDI StretchBlt all mesages are there. It is amazing.

Ocrana
0
 
pgnatyukCommented:
>>Not LButton, not DblClick, nothing
The application simply gets stuck? So how you close it? Have you initialized GDI+ correctly?

Ok. So comment these three lines and check if your window receives the messages:
Gdiplus::Bitmap SrcBitmap(hBitmap,NULL);
Graphics graphics(m_hDC);
graphics.DrawImage(&SrcBitmap,
            rect.left, rect.top,
            rect.right - rect.left,
            rect.bottom - rect.top);
If your suggestion is correct, when these lines are commented, the mouse buttons should work.
If it is so, uncomment the first line and check, then the second.

But I think that there is another mistake somewhere. For example, your window cannot validate itself and continuously draw itself. You can put a TRACE in this function and check how often it happens with StretchBlt and with this GDI implementation. Maybe another message comes all the time,... you see the code and may find it. You can use Spy from the VS tools if it will help.

0
 
OcranaAuthor Commented:
Hi,

I tried this before and if I just commented out DrawImage, the message handling works, after put DrawImage back, it wont work again. Do DrawImage occour/fire a message?

Ocrana
0
 
pgnatyukCommented:
No. I think it just gets stuck.
You may call DrawImage with less parameters (only x and y instead of the full rectangle), so the image will be drawn without stretching. Please check this way too.

HBITMAP hBitmap = (HBITMAP)wParam;
What does it mean?

Can we replace this bitmap with something else, for example, loaded from a file or a resource.
You know that you can load a bitmap from a file:
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, _T(test.bmp),
      IMAGE_BITMAP, 0, 0,
      LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE);

Simply load a bitmap somewhere in the beginning and keep it in a global variable. Use this global variable for DrawImage call.

I hope you initialize GDI+ correctly:
#include
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

and then:
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

in the end of your app you have:
GdiplusShutdown(gdiplusToken);

It will be funny if you will see that the image is simply broken.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now