Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / WTL

Form Designer

26 Jul 2021CPOL24 min read 351K   82.5K   230  
Component for adding scriptable forms capabilities to an application.
// MenuHelpers.cpp
//
// Author : David Shepherd
//			Copyright (c) 2002, DaeDoe-Software
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MenuHelpers.h"

CMenuHandle CreateEmptyPopupMenu(DWORD EmptyTextId)
{
	USES_CONVERSION;

	// create the empty popup menu
	CMenu Menu;
	if(Menu.CreatePopupMenu()==FALSE)
	{
		throw std::exception();
	}
	// insert the empty menu item
	CComBSTR Text;
	if(Text.LoadString(EmptyTextId)==FALSE)
	{
		throw std::exception();
	}
	if(Menu.AppendMenu(
		MF_STRING|MF_GRAYED,0,W2CT(BSTR2W(Text)))==FALSE)
	{
		throw std::exception();
	}
	return Menu.Detach();
}

void SetPopupMenu(CMenuHandle hParent,long ItemId,CMenuHandle hPopup)
{
	// check parameters
	if(hParent==NULL)
	{
		throw std::exception();
	}
	if(hPopup==NULL)
	{
		throw std::exception();
	}
	// get the menu item text length
	long TextLength=hParent.GetMenuStringLen(ItemId,MF_BYCOMMAND);
	if(TextLength==0)
	{
		throw std::exception();
	}
	// get the menu item text
	std::auto_ptr<TCHAR> pText(new TCHAR[TextLength+1]);
	if(hParent.GetMenuString(ItemId,pText.get(),TextLength+1,MF_BYCOMMAND)==0)
	{
		throw std::exception();
	}
	// set the popup menu
	if(hParent.ModifyMenu(ItemId,MF_BYCOMMAND|MF_STRING|MF_POPUP,
		(UINT)(HMENU)hPopup,pText.get())==FALSE)
	{
		throw std::exception();
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions