2009. 6. 16. 10:08

동적으로 Option 엘리먼트 추가하기

// Option 엘리먼트를추가하는것이쉽지않다.. (MSDN 처럼동작하지않음)

// 참고: http://www.codeproject.com/KB/COM/htmldocument.aspx

 

 

CComQIPtr<IHTMLDocument2> spHTMLDoc2 =  GetHtmlDocument();

CComQIPtr<IHTMLWindow2> spWindow;

CComPtr<IDispatch> spDispatch;

 

spHTMLDoc2->get_Script(&spDispatch);

spWindow = spDispatch;

 

CComQIPtr<IHTMLOptionElementFactory> spOptionFactory;

spWindow->get_Option(&spOptionFactory);

 

CComQIPtr<IHTMLOptionElement> spOptionElement;

 

_variant_t varSelected = VARIANT_FALSE;

spOptionFactory->create(_variant_t(_T("New Option")), _variant_t(VARIANT_FALSE), _variant_t(VARIANT_FALSE), varSelected, &spOptionElement);

 

CComQIPtr<IHTMLSelectElement> spSelectElement = spHTMLElement;

ASSERT(spSelectElement != NULL);

CComQIPtr<IHTMLElement> spTempElement = spOptionElement;

 

strOptionValue.Format(_T("%d"), nIndex);

 

spTempElement->setAttribute(_T("value"), _variant_t(_bstr_t(_T("Some Value"))), 0 );

spSelectElement->add(spTempElement, _variant_t(1));

 

/*

// MSDN 에서언급한대로하면아래와같은데...동작하지않는다.

CComQIPtr<IHTMLSelectElement> spSelectElement = spHTMLElement;

CComQIPtr<IHTMLElement> spTempElement;

hr = spHTMLDoc2->createElement(_T("option"), &spTempElement);

hr = spSelectElement->add(spTempElement, _variant_t(1));*/


출처 : http://www.codeproject.com/KB/COM/htmldocument.aspx