Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I copy the style from one control to another?

I need to programmatically copy the style from one graph to another. I'm currently using the importstyle and export style functions but I'd like to avoid that since: 1) I'm creating >100 of the same graphs in a scrolling window and execution time is a concern, and 2) it makes it harder to redistribute the application, and 3) you shouldn't have to import/export from disk just to copy a graph style.

I noticed the copy constructor was disabled so you can't just create a new one from the original. I suppose I could iterate through all the styles and transfer them from the master graph to all the copies but is there an easier way to do that? If not, is there some sample code for that?

I'm using MStudio 7.0 for C
++.

Thanks,
-Bob
0 Kudos
Message 1 of 3
(3,274 Views)
One way that you could do this would be to create a helper method that configures your graph rather than configuring it at design-time, then use that helper method to apply the settings to the new graphs that you create. However, this would only work if you wanted all graphs to be configured exactly the same way - this would not work if the settings of your master graph are changing at run-time and you want the new graphs to be configured with the current settings of the master graph.

Another approach is to query each control for IPersistPropertyBag, create an IPropertyBag, pass the IPropertyBag to the master graph's IPersistPropertyBag::Save, then pass the IPropertyBag to the new graph's IPersistPropertyBag::Load implementation. I'm not aware of any implementations of IPropertyBag that are readily available for use in applications, so the tricky part is creating the IPropertyBag. Below is a very simple implementation of IPropertyBag that should be enough to get the job done for this example. First, add this to your stdafx.h:

#include <atlbase.h>
CComModule _Module;
#include <atlcom.h>
#include <atlcoll.h>

Here's the simple IPropertyBag implementation:

class ATL_NO_VTABLE CSimplePropertyBag :
public CComObjectRootEx<CComSingleThreadModel>,
public IPropertyBag
{
private:
CAtlMap<CComBSTR, CComVariant> m_propertyMap;

public:
BEGIN_COM_MAP(CSimplePropertyBag)
COM_INTERFACE_ENTRY(IPropertyBag)
END_COM_MAP()

STDMETHODIMP Read(LPCOLESTR pszPropName, VARIANT* pVar, IErrorLog* pErrorLog)
{
HRESULT hr = E_FAIL;

if ((pszPropName == NULL) || (pVar == NULL))
hr = E_POINTER;
else
{
if (SUCCEEDED(::VariantClear(pVar)))
{
CComBSTR key = pszPropName;
CComVariant value;

if (!m_propertyMap.Lookup(key, value))
hr = E_INVALIDARG;
else
{
if (SUCCEEDED(::VariantCopy(pVar, &value)))
hr = S_OK;
}
}
}

return hr;
}

STDMETHODIMP Write(LPCOLESTR pszPropName, VARIANT* pVar)
{
HRESULT hr = E_FAIL;

if ((pszPropName == NULL) || (pVar == NULL))
hr = E_POINTER;
else
{
m_propertyMap.SetAt(pszPropName, *pVar);
hr = S_OK;
}

return hr;
}
};

Once you have a way to create an implementation of IPropertyBag, you can use IPropertyBag and IPersistPropertyBag to copy the settings from one control to another like this:

void CopyGraphStyle(CNiGraph& source, CNiGraph& target)
{
LPUNKNOWN pSourceUnknown = source.GetControlUnknown();
LPUNKNOWN pTargetUnknown = target.GetControlUnknown();

if ((pSourceUnknown != NULL) && (pTargetUnknown != NULL))
{
CComQIPtr<IPersistPropertyBag> pSourcePersist(pSourceUnknown);
CComQIPtr<IPersistPropertyBag> pTargetPersist(pTargetUnknown);

if ((pSourcePersist != NULL) && (pTargetPersist != NULL))
{
CComObject<CSimplePropertyBag>* pPropertyBag = 0;
CComObject<CSimplePropertyBag>::CreateInstance(&pPropertyBag);

if (pPropertyBag != NULL)
{
CComQIPtr<IPropertyBag> spPropertyBag(pPropertyBag);

if (spPropertyBag != NULL)
{
if (SUCCEEDED(pSourcePersist->Save(spPropertyBag, FALSE, TRUE)))
pTargetPersist->Load(spPropertyBag, NULL);
}
}
}
}
}

(Note that "CreateInstan ce" above should be CreateInstance - a space gets added for some unknown reason after I click Submit.)

Then you can use this CopyGraphStyle method to copy the settings of the master graph to the new graph. Hope this helps.

- Elton
Message 2 of 3
(3,274 Views)
Elton,
That's exactly what I wanted. I tried it and it works as expected. Thank you very much!
-Bob
0 Kudos
Message 3 of 3
(3,274 Views)