I have an MFCActiveX control, in which there is a ctrl class created by the wizard. Within that class there is a function named ondraw like this..
Code:
void CGregTestCtrl::OnDraw(
CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
// TODO: Replace the following code with your own drawing code.
pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(GRAY_BRUSH)));
pdc->Ellipse(rcBounds);
}
When I use this control anywhere in an application it displays an Ellipse. I want to display a Bitmap and some written text instead of that ellipse. So what all changes do I have to make in OnDraw() function??
Also u should see compatibility issues as mentioned in msdn:
See the CDC::BitBlt member function for information regarding bit-block transfers between device contexts. To determine whether a device context supports raster operations, see the RC_BITBLT raster capability in the member function
//Creates a memory device context that is compatible with the device specified by pDC.
So you make compatible the pdc to pdc????
Plese look at an example taken from the MSDN:
Code:
void CBlat2View::OnDraw(CDC* pDC)
{
CBlat2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// load IDB_BITMAP1 from our resources
CBitmap bmp;
if (bmp.LoadBitmap(IDB_BITMAP1))
{
// Get the size of the bitmap
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
// Create an in-memory DC compatible with the
// display DC we're using to paint
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
// Find a centerpoint for the bitmap in the client area
CRect rect;
GetClientRect(&rect);
int nX = rect.left + (rect.Width() - bmpInfo.bmWidth) / 2;
int nY = rect.top + (rect.Height() - bmpInfo.bmHeight) / 2;
// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
pDC->BitBlt(nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
}
else
TRACE0("ERROR: Where's IDB_BITMAP1?\n");
}
Best regards,
Gili
Please use code tags [code] [/code]
We would change the world, but God won't give us the sourcecode..
Undocumented futures are fun and useful....
_________
Gili
Thanks for the help Gill. It is successfully creating Compitable memory DC. But now the problem is coming in selecting the bitmap into the memory DC. Over here it crashes
Here is the code I have attached. In order to test it you need to register it and then open Micorsoft word>Insert Objects>Greg Digital Sign.
I have also attached the code for registration.
Thanks once again
Hi maverick786us try to use the following code:
Code:
void CGregTestCtrl::OnDraw(CDC* pDC, const CRect& rcBounds, const CRect& rcInvalid)
{
pDC->Rectangle (rcBounds);
CBitmap bmp;
CClientDC dc(NULL);
if (bmp.LoadBitmap (IDB_BMP_LOGO))
{
// Get the size of the bitmap
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
// Create an in-memory DC compatible with the
// display DC we're using to paint
CDC dcMemory;
if ( dcMemory.CreateCompatibleDC(NULL) )
{
// Select the bitmap into the in-memory DC
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
// Copy the bits from the in-memory DC into the on-
// screen DC to actually do the painting. Use the centerpoint
// we computed for the target offset.
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
dcMemory.SelectObject(pOldBitmap);
}
}
else
{
TRACE ("ERROR: Where's IDB_BITMAP?\n");
}
}
Your original example worked in the ActiveX Control Test container utility, but not in MSWord as an object.
The the COleObject has a null m_hWnd this means you can't use the GetClientRect() and also the CreateCompatibleDC(pdc) ( returned NULL ) failed . So use it in this way:
Code:
if ( dcMemory.CreateCompatibleDC(NULL) )
{
.................
}
Best Regards,
Gili
PS:
I'm not to familiar with OLE
Also attached the file where I made the modifications.
Please use code tags [code] [/code]
We would change the world, but God won't give us the sourcecode..
Undocumented futures are fun and useful....
_________
Gili
i know already how to display a bitmap using the resource. i want to know wat if i want to display a bitmap file without the use of resource? just directly from open dialog box.
i know already how to display a bitmap using the resource. i want to know wat if i want to display a bitmap file without the use of resource? just directly from open dialog box.
Is your problem is to load the image from file? or get the name from the open dialog box?
Cheers
If a post helped you dont forget to "Rate This Post"
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.