Click here to Skip to main content
14,974,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Step:

(1) Create ATL Project
(2) Right Click on ATL Project, Added COM Control
(3) Added Context Menu Manganer Event
1. CMenu
2. SetMenuItemInfo -- Inserted Transperant Png for each Menu Item

Problem:
Transperant Png is appearing with black background in XP, it should be transperant

Header File:

XML
// MenuSampleCtrl.h : Declaration of the CMenuSampleCtrl
#pragma once
#include "resource.h"       // main symbols
#include <atlctl.h>
#include "MenuSample_i.h"
#include "_IMenuSampleCtrlEvents_CP.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

using namespace ATL;
#include <GdiPlus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib")

// CMenuSampleCtrl
class ATL_NO_VTABLE CMenuSampleCtrl :
    public CComObjectRootEx<CComSingleThreadModel>,
    public CStockPropImpl<CMenuSampleCtrl, IMenuSampleCtrl>,
    public IPersistStreamInitImpl<CMenuSampleCtrl>,
    public IOleControlImpl<CMenuSampleCtrl>,
    public IOleObjectImpl<CMenuSampleCtrl>,
    public IOleInPlaceActiveObjectImpl<CMenuSampleCtrl>,
    public IViewObjectExImpl<CMenuSampleCtrl>,
    public IOleInPlaceObjectWindowlessImpl<CMenuSampleCtrl>,
    public ISupportErrorInfo,
    public IConnectionPointContainerImpl<CMenuSampleCtrl>,
    public CProxy_IMenuSampleCtrlEvents<CMenuSampleCtrl>,
    public IObjectWithSiteImpl<CMenuSampleCtrl>,
    public IServiceProviderImpl<CMenuSampleCtrl>,
    public IPersistStorageImpl<CMenuSampleCtrl>,
    public ISpecifyPropertyPagesImpl<CMenuSampleCtrl>,
    public IQuickActivateImpl<CMenuSampleCtrl>,
#ifndef _WIN32_WCE
    public IDataObjectImpl<CMenuSampleCtrl>,
#endif
    public IProvideClassInfo2Impl<&CLSID_MenuSampleCtrl, &__uuidof(_IMenuSampleCtrlEvents), &LIBID_MenuSampleLib>,
    public IPropertyNotifySinkCP<CMenuSampleCtrl>,
    public IObjectSafetyImpl<CMenuSampleCtrl, INTERFACESAFE_FOR_UNTRUSTED_CALLER>,
    public CComCoClass<CMenuSampleCtrl, &CLSID_MenuSampleCtrl>,
    public CComControl<CMenuSampleCtrl>
{
    ULONG_PTR           gdiplusToken;
    HBITMAP             hSampleBitmap;
    GdiplusStartupInput gdiplusInput;

    HBITMAP     CreatePARGBIcon(UINT uID);
    Bitmap*     LoadGDIPlusImageFromResouce(UINT nResID, LPCTSTR pResType);

public:
    CMenuSampleCtrl()
    {
        GdiplusStartup(&gdiplusToken,&gdiplusInput,NULL);
        hSampleBitmap = CreatePARGBIcon(IDB_PNG1);
        m_bWindowOnly = TRUE;
    }

DECLARE_OLEMISC_STATUS(OLEMISC_RECOMPOSEONRESIZE |
    OLEMISC_CANTLINKINSIDE |
    OLEMISC_INSIDEOUT |
    OLEMISC_ACTIVATEWHENVISIBLE |
    OLEMISC_SETCLIENTSITEFIRST
)

DECLARE_REGISTRY_RESOURCEID(IDR_MENUSAMPLECTRL)


BEGIN_COM_MAP(CMenuSampleCtrl)
    COM_INTERFACE_ENTRY(IMenuSampleCtrl)
    COM_INTERFACE_ENTRY(IDispatch)
    COM_INTERFACE_ENTRY(IViewObjectEx)
    COM_INTERFACE_ENTRY(IViewObject2)
    COM_INTERFACE_ENTRY(IViewObject)
    COM_INTERFACE_ENTRY(IOleInPlaceObjectWindowless)
    COM_INTERFACE_ENTRY(IOleInPlaceObject)
    COM_INTERFACE_ENTRY2(IOleWindow, IOleInPlaceObjectWindowless)
    COM_INTERFACE_ENTRY(IOleInPlaceActiveObject)
    COM_INTERFACE_ENTRY(IOleControl)
    COM_INTERFACE_ENTRY(IOleObject)
    COM_INTERFACE_ENTRY(IPersistStreamInit)
    COM_INTERFACE_ENTRY2(IPersist, IPersistStreamInit)
    COM_INTERFACE_ENTRY(ISupportErrorInfo)
    COM_INTERFACE_ENTRY(IConnectionPointContainer)
    COM_INTERFACE_ENTRY(ISpecifyPropertyPages)
    COM_INTERFACE_ENTRY(IQuickActivate)
    COM_INTERFACE_ENTRY(IPersistStorage)
#ifndef _WIN32_WCE
    COM_INTERFACE_ENTRY(IDataObject)
#endif
    COM_INTERFACE_ENTRY(IProvideClassInfo)
    COM_INTERFACE_ENTRY(IProvideClassInfo2)
    COM_INTERFACE_ENTRY(IObjectWithSite)
    COM_INTERFACE_ENTRY(IServiceProvider)
    COM_INTERFACE_ENTRY_IID(IID_IObjectSafety, IObjectSafety)
END_COM_MAP()

BEGIN_PROP_MAP(CMenuSampleCtrl)
    PROP_DATA_ENTRY("_cx", m_sizeExtent.cx, VT_UI4)
    PROP_DATA_ENTRY("_cy", m_sizeExtent.cy, VT_UI4)
    // Example entries
    // PROP_ENTRY_TYPE("Property Name", dispid, clsid, vtType)
    // PROP_PAGE(CLSID_StockColorPage)
END_PROP_MAP()

BEGIN_CONNECTION_POINT_MAP(CMenuSampleCtrl)
    CONNECTION_POINT_ENTRY(IID_IPropertyNotifySink)
    CONNECTION_POINT_ENTRY(__uuidof(_IMenuSampleCtrlEvents))
END_CONNECTION_POINT_MAP()

BEGIN_MSG_MAP(CMenuSampleCtrl)
    CHAIN_MSG_MAP(CComControl<CMenuSampleCtrl>)
    DEFAULT_REFLECTION_HANDLER()
    MESSAGE_HANDLER(WM_CONTEXTMENU, OnContextMenu)
END_MSG_MAP()
// Handler prototypes:
//  LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
//  LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
//  LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);

// ISupportsErrorInfo
    STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid)
    {
        static const IID* const arr[] =
        {
            &IID_IMenuSampleCtrl,
        };

        for (int i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
        {
            if (InlineIsEqualGUID(*arr[i], riid))
                return S_OK;
        }
        return S_FALSE;
    }

// IViewObjectEx
    DECLARE_VIEW_STATUS(VIEWSTATUS_SOLIDBKGND | VIEWSTATUS_OPAQUE)

// IMenuSampleCtrl
public:
    HRESULT OnDraw(ATL_DRAWINFO& di)
    {
        RECT& rc = *(RECT*)di.prcBounds;
        // Set Clip region to the rectangle specified by di.prcBounds
        HRGN hRgnOld = NULL;
        if (GetClipRgn(di.hdcDraw, hRgnOld) != 1)
            hRgnOld = NULL;
        bool bSelectOldRgn = false;

        HRGN hRgnNew = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);

        if (hRgnNew != NULL)
        {
            bSelectOldRgn = (SelectClipRgn(di.hdcDraw, hRgnNew) != ERROR);
        }

        Rectangle(di.hdcDraw, rc.left, rc.top, rc.right, rc.bottom);
        SetTextAlign(di.hdcDraw, TA_CENTER|TA_BASELINE);
        LPCTSTR pszText = _T("MenuSampleCtrl");
#ifndef _WIN32_WCE
        TextOut(di.hdcDraw,
            (rc.left + rc.right) / 2,
            (rc.top + rc.bottom) / 2,
            pszText,
            lstrlen(pszText));
#else
        ExtTextOut(di.hdcDraw,
            (rc.left + rc.right) / 2,
            (rc.top + rc.bottom) / 2,
            ETO_OPAQUE,
            NULL,
            pszText,
            ATL::lstrlen(pszText),
            NULL);
#endif

        if (bSelectOldRgn)
            SelectClipRgn(di.hdcDraw, hRgnOld);

        return S_OK;
    }

    STDMETHOD(_InternalQueryService)(REFGUID /*guidService*/, REFIID /*riid*/, void** /*ppvObject*/)
    {
        return E_NOTIMPL;
    }

    DECLARE_PROTECT_FINAL_CONSTRUCT()

    HRESULT FinalConstruct()
    {
        return S_OK;
    }

    void FinalRelease()
    {
    }
    LRESULT OnContextMenu(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};

OBJECT_ENTRY_AUTO(__uuidof(MenuSampleCtrl), CMenuSampleCtrl)



CPP File:

// MenuSampleCtrl.cpp : Implementation of CMenuSampleCtrl
#include "stdafx.h"
#include "MenuSampleCtrl.h"


// CMenuSampleCtrl


LRESULT CMenuSampleCtrl::OnContextMenu(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
WORD xPos = LOWORD(lParam);
WORD yPos = HIWORD(lParam);

CMenu *myPopup = new CMenu();
myPopup->LoadMenu(IDR_MENU1);

MENUINFO mi;
mi.cbSize = sizeof(mi);
mi.fMask = MIM_STYLE|MIM_APPLYTOSUBMENUS|MIM_BACKGROUND;
mi.hbrBack = GetSysColorBrush(COLOR_WINDOW);
mi.dwStyle = MNS_CHECKORBMP;
myPopup->GetSubMenu(0)->SetMenuInfo(&mi);

MENUITEMINFO mii;
mii.cbSize = sizeof mii;
mii.fMask = MIIM_BITMAP;

mii.hbmpChecked = mii.hbmpUnchecked = mii.hbmpItem = hSampleBitmap;
myPopup->GetSubMenu(0)->SetMenuItemInfo(ID_REFRESH,&mii,FALSE);
myPopup->GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, xPos, yPos, CWnd::FromHandle(m_hWnd));

return 0;
}

HBITMAP CMenuSampleCtrl::CreatePARGBIcon(UINT uID)
{
HBITMAP hResBmp = NULL;
Bitmap *pBmp = LoadGDIPlusImageFromResouce(uID,_T("PNG"));
if(pBmp)
{
if(pBmp->GetHBITMAP(Color::Color(0,0,0,0),&hResBmp) == Ok)
{}
delete pBmp;
}
return hResBmp;
}

Bitmap* CMenuSampleCtrl::LoadGDIPlusImageFromResouce(UINT nResID, LPCTSTR pResType)
{
Bitmap* pImage = NULL;
HRSRC hRes = ::FindResource(_AtlBaseModule.GetResourceInstance(), MAKEINTRESOURCE(nResID),pResType);
if(hRes)
{
long szLength = ::SizeofResource(_AtlBaseModule.GetResourceInstance(), hRes);
HGLOBAL hGlobal = ::LoadResource(_AtlBaseModule.GetResourceInstance(), hRes);
if(szLength && hGlobal)
{
LPVOID pData = LockResource(hGlobal);
DWORD dwSz = SizeofResource(_AtlBaseModule.GetResourceInstance(), hRes);
if(pData && dwSz)
{
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, dwSz);
LPVOID pImageData = ::GlobalLock(hMem);
if(pImageData)
{
memcpy(pImageData, pData, dwSz);
IStream *pStream = NULL;
if(::CreateStreamOnHGlobal(hMem,TRUE,&pStream) == S_OK)
{
pImage = Bitmap::FromStream(pStream);
if(pImage)
{
Status s = pImage->GetLastStatus();
if(s != Ok)
{
delete pImage;
pImage = NULL;
}
}
pStream->Release();
}
::GlobalUnlock(hMem);
}
}
}
}
return pImage;
}
Posted
Updated 24-Jan-14 19:27pm
v2
Comments
Nelek 21-Dec-13 5:58am
   
Don't think we can read minds or do astral projections to see your monitor. If you need help, the least you could do is to add some relevant code to your question or to explain your problem in such a way, that the users of CP can understand it. Otherwise, nobody will be able to help you.

Please use the "improve question" and add relevant information or a piece of code envolved. There are thousands of ways to screw things up, how are we supposed to know which one did you choose?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900