none
VC中調用Java Script的問題 RRS feed

  • 问题

  • 您好,

    本人有個在MFC中調用Java script的問題想提問.

    公司的專案禮有一段code大致如下:

     

    // a custimize control
    class CMotionHtmlCtrl: public CHtmlView
    {
    ...
    ...
    }
    
    // a custimize viewer which contains CMotionHtmlCtrl
    class CMagic_MotionPage : public CWnd
    {
    ...
    }
    
    void CMagic_MotionPage::Initialize()
    {
    	m_pMotionStylePage = new CMotionHtmlCtrl;
    	//...
    
        //To navigate a html page at local end
    	m_pMotionStylePage->Navigate2(L"C:\MotionStyle.htm");
    	m_pMotionStylePage->CreateFromRect(rcBrowser, this,TRUE);
    	m_pMotionStylePage->ShowScrollBar(SB_BOTH, FALSE);
    	
    	//wait for m_pMotionStylePage OnDocumentComplete
        ...
    	
    	CComPtr<IDispatch> spDisp = m_pMotionStylePage->GetHtmlDocument();
    	CComPtr<IHTMLDocument2> pDoc;
    	CComPtr<IDispatch> spScript;
    	DISPID dispid = NULL;
    
        // it is the function name in java script in MotionStyle.htm
    	CString strFunc = L"InitMotionStyleTable";
    	
    	HRESULT hr = spDisp->QueryInterface(IID_IHTMLDocument,(void**)&pDoc);
    	hr = pDoc->get_Script(&spScript);
    	CComBSTR bstrMember(strFunc);
    
        // Failure to get ID, but it works before.
    	hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1, LOCALE_SYSTEM_DEFAULT,&dispid);
    
        ...
        //Try to invoke function in Java script
        spScript->Invoke(dispid,IID_NULL,0,DISPATCH_METHOD,&dispparams,&vaResult,&excepInfo,&nArgErr);
    
    }
    

     

    MotionStyle.htm內容如下

     

    <html>
    	<script type="text/javascript">
    	
    	var MotionStyleArray = new Array(22);
    	
    	MotionStyleArray[0] = "MM_Path00.gif";
    	MotionStyleArray[1] = "MM_Path01.gif";
    	MotionStyleArray[2] = "MM_Path02.gif";
    	MotionStyleArray[3] = "MM_Path03.gif";
    	MotionStyleArray[4] = "MM_Path04.gif";
    	MotionStyleArray[5] = "MM_Path05.gif";
    	MotionStyleArray[6] = "MM_Path06.gif";
    	MotionStyleArray[7] = "MM_Path07.gif";
    	MotionStyleArray[8] = "MM_Path08.gif";
    	MotionStyleArray[9] = "MM_Path09.gif";
    	MotionStyleArray[10] = "MM_Path10.gif";
    	MotionStyleArray[11] = "MM_Path11.gif";
    	MotionStyleArray[12] = "MM_Path12.gif";
    	MotionStyleArray[13] = "MM_Path13.gif";
    	MotionStyleArray[14] = "MM_Path14.gif";
    	MotionStyleArray[15] = "MM_Path15.gif";
    	MotionStyleArray[16] = "MM_Path16.gif";
    	MotionStyleArray[17] = "MM_Path17.gif";
    	MotionStyleArray[18] = "MM_Path18.gif";
    	MotionStyleArray[19] = "MM_Path19.gif";
    	MotionStyleArray[20] = "MM_Path20.gif";
    	MotionStyleArray[21] = "MM_Path_user.gif"; 
    	
    	var m_nTotalStyle = 22;
    	
    	var StyleNameArray = new Array(22);
    	var StyleNameToolTipsArray = new Array(22);
    	
    	var m_nWindowWidth = 0;
    	var m_nWindowHeight = 0;
    	var m_nStyleInWidth = 0;
    	var m_nVisableStartY = 0;
    	var m_nCellSpacing = 0.1;
    	var m_strSelectedDiv = '-1';
    	var m_ScrollHeight = 0;
    	var m_strFontName = 'arial';
    	var m_bHideUserDefineStyle = false;
    
    	function SetMotionStyleTootips(strStyleNum, strMotionStyle)
    	{
    		var nStyleNum = parseInt(strStyleNum);  
    		StyleNameToolTipsArray[nStyleNum] = strMotionStyle;
    	}
    	
    	function SetMotionStyleString(strStyleNum, strMotionStyle)
    	{
    		var nStyleNum = parseInt(strStyleNum);  
    		StyleNameArray[nStyleNum] = strMotionStyle;
    	}
    	
    	function SetFont(strFontName)
    	{ 
    		m_strFontName = strFontName;
    	}
    			
    	function InitMotionStyleTable()
    	{	
    		updateTableWH();
    		CreateMotionStyleTable();
    		//setTimeout("SetScrollHeight();",5000);
    		SetScrollHeight();
    		
    		// Reset motion Style when resize
    		if(m_strSelectedDiv != '-1')
    		{
    			var selectedDiv = document.getElementById(m_strSelectedDiv);
    			selectedDiv.style.backgroundImage = "url('Mask_Grid_4_3_Selected.png')";
    		}
    		
    	}
    	
    	function updateTableWH()
    	{
    		m_nWindowHeight = document.body.clientHeight;
    		var height = m_nWindowHeight;	
    		m_nWindowWidth = document.body.clientWidth;
    		var width = m_nWindowWidth;
    		
    		for(var i=1; i<23; i++)
    		{
    			var CurStyleWidth = (i*97)+((i+1)*5); 
    			if(m_nWindowWidth<CurStyleWidth)
    			{
    				m_nStyleInWidth = i-1;
    				var nTotalSpacing = m_nWindowWidth - (m_nStyleInWidth * 97);
    				m_nCellSpacing = (nTotalSpacing/(m_nStyleInWidth + 1))-5;
    				break;
    			}
    		}
    	}
    	
    	function ScrollBarScroll(strNewStartY)
    	{
    		var nNewStartY = parseInt(strNewStartY);  
    		document.body.scrollTop = nNewStartY;
    	}
    	
    	function onClick(this_style)
    	{	
    		if(m_strSelectedDiv != this_style.id )
    		{
    		   if(m_strSelectedDiv != "-1")
    		   {
    		   	var selectedDiv = document.getElementById(m_strSelectedDiv);
    			//selectedDiv.style.backgroundColor = '';
    			selectedDiv.style.backgroundImage = '';
    		   }
    			
    		}
    		m_strSelectedDiv = this_style.id;
    		//this_style.style.backgroundColor = '#fcfc00';
    		this_style.style.backgroundImage = "url('Mask_Grid_4_3_Selected.png')";
    		
    		
    		var ref = "SetStyle:"+ this_style.id;
    		location = ref; 
    	}
    	
    	function SetScrollPos()
    	{
    		// Reset scrollbar position to current selection item 
    		var offsetTop = 0;
    		if(m_strSelectedDiv != "-1")
    		{
    			var CurrentDiv = document.getElementById(m_strSelectedDiv);
    			var offsetTop = CurrentDiv.parentNode.offsetTop;
    		}
    		var ref = "SetScrollPos:"+ offsetTop;
    		location = ref;
    		document.body.scrollTop = offsetTop;
    	
    	}
    	
    	function SetMotionStyle(strMotionStyle)
    	{	
    		if(m_strSelectedDiv != strMotionStyle)
    		{
    		   if(m_strSelectedDiv != "-1")
    		   {
    			var previousDiv = document.getElementById(m_strSelectedDiv);
    			previousDiv.style.backgroundImage = '';
    		   }
    		   m_strSelectedDiv = strMotionStyle;
    		   
    		   if(strMotionStyle != "-1")
    		   {
    		   	var CurrentDiv = document.getElementById(m_strSelectedDiv);
    		   	CurrentDiv.style.backgroundImage = "url('Mask_Grid_4_3_Selected.png')";	
    		   }
    		}
    			
    	}
    	
    	function HideMotionStyle(strMotionStyle)
    	{		
    	   if(m_strSelectedDiv != "-1")
    	   {
    		var previousDiv = document.getElementById(m_strSelectedDiv);
    		previousDiv.style.backgroundImage = '';
    		m_strSelectedDiv = -1;
    	   }
    	   
    	   var nStyleID = parseInt(strMotionStyle); 
    	   if(nStyleID < m_nTotalStyle)
    	   {
    	   		var CurrentDiv = document.getElementById(strMotionStyle);
    	   		CurrentDiv.style.display = "none";
    	   }
    	   
    	   if(StyleNameToolTipsArray[nStyleID]=="User Defined")
    	   	HideUserDefineStyle();
    	}
    	
    	function SetAlwaysRandom()
    	{	
    		// only append random style into table	
    		m_nTotalStyle = 1;
    	}
    	
    	function HideUserDefineStyle()
    	{
    		m_bHideUserDefineStyle = true;
    	}
    	
    	function IsMotionStyleSelected()
    	{	
    		if(m_strSelectedDiv == '-1')
    		{
    			var ref = "IsMotionStyleSelectedCB:"+ "0";
    			location = ref; 
    		}
    		else
    		{
    			var ref = "IsMotionStyleSelectedCB:"+ "1";
    			location = ref; 
    		}
    		
    	}
    	
    	function SetScrollHeight()
    	{ 
    	   if(m_ScrollHeight != document.body.scrollHeight)
    	   {   
    	       m_ScrollHeight = document.body.scrollHeight;
    		   var ref = "SetScrollHeight:"+ m_ScrollHeight;
    		   location = ref;
    		   setTimeout("SetScrollHeight();",100);
    	   }  
    	}
    	
    	function CreateMotionStyleTable()
    	{
    		//alert(typeof(document.createElement("MotionStyleTable")));
    		var body = document.getElementById("bkArea");
    		//body.innerHTML = "<table id='MotionStyleTable'></table>";
    		body.innerHTML = "<table id='MotionStyleTable' cellspacing='" + m_nCellSpacing.toString() + "'></table>";
    		//body.innerHTML = "<table id='MotionStyleTable' cellspacing='20'></table>";
    		
    		var tbl   = document.getElementById("MotionStyleTable");
    		tbl.setAttribute('cellspacing', m_nCellSpacing);
    		var tblBody = document.createElement("tbody");
    		
    		var nCount = 0;	
    		var row = document.createElement("tr");
    		var nCount = 0;
    		
    		for(var i=0; i<m_nTotalStyle; i++)
    		{
    			// Check need to hide User-Define style or not
    			if(m_bHideUserDefineStyle && MotionStyleArray[i]=="MM_Path_user.gif")
    				continue;
    				
    			if(nCount>m_nStyleInWidth-1)
    			{
    				nCount = 0;
    				row = document.createElement("tr");
    			}
    			nCount++;
    			
    			var cell = document.createElement("td");
    			var div = document.createElement('div');
    			div.setAttribute('class','maindiv');
    			//div.setAttribute('width', 90);
    			//div.setAttribute('height', 100);
    			div.setAttribute('align','center');
    			div.setAttribute('id', i.toString());
    			div.style.backgroundRepeat = "no-repeat";
    			
    			
    			
    			var divImage = document.createElement('div');
    			//divImage.setAttribute('width', 80);
    			//divImage.setAttribute('height', 60);
    			divImage.setAttribute('align','center');
    			var image = document.createElement('img');
    			image.setAttribute('src', MotionStyleArray[i]);
    			image.setAttribute('id', i.toString());
    			image.setAttribute('tooltips', StyleNameToolTipsArray[i]);
    			image.setAttribute('title', StyleNameToolTipsArray[i]);
    			
    			image.onclick = function()
    			{
    				//this.style.cursor= 'hand';
    				
    			}
    			
    			image.onmouseover = function()
    			{
    				this.style.cursor= 'hand';
    			}
    			
    			divImage.appendChild(image);
    			
    			
    			var divStyleName = document.createElement('div');
    			var StyleName = document.createElement('font');
    			StyleName.setAttribute('fac', m_strFontName);
    			//StyleName.setAttribute('size', '2');		
    			StyleName.style.fontSize="12px";
    			StyleName.setAttribute('color', '#FFFFFF');
    			var textNode = document.createTextNode(StyleNameArray[i]);
    			StyleName.appendChild(textNode);
    			divStyleName.appendChild(StyleName);
    			
    				
    			div.appendChild(divImage);
    			div.appendChild(divStyleName);
    			
    			div.onmouseover = function()
    			{
    				if(this.id != m_strSelectedDiv)
    				{
    					//this.style.backgroundColor = '#b4b4b4';
    					//this.style.border="1px solid #FFFFFF";
    					this.style.backgroundImage = "url('Mask_Grid_4_3_Pressed.png')";
    				}
    
    			}
    			
    			div.ondragstart = function()
    			{
    				return false;
    			}
    			
    			div.onmouseout = function()
    			{
    				if(this.id != m_strSelectedDiv)
    				{
    					//this.style.backgroundColor = '';
    					this.style.backgroundImage = '';
    				}
    			}
    			
    			div.onclick = function()
    			{
    				//this.style.backgroundColor = '#fcfc00';
    				onClick(this);
    			}
    			
    			cell.appendChild(div);
    			
    			row.appendChild(cell);
    			tblBody.appendChild(row);
    			
    		}
    		// put the <tbody> in the <table>
    		tbl.appendChild(tblBody);
    		
    		// appends <table> into <body>
    		body.appendChild(tbl);
    		//alert(typeof(document.MotionStyleTable));	
    	}
    	
    
    	
    	</script>
    	<HEAD>
    	<STYLE type="text/css">
    		 BODY
    		 { 
    		 	margin-right: 0px;	
    		 	margin-left: 0px;	
    		 	margin-top: 0px;	
    		 	scrollbar-face-color: #303030;
    			scrollbar-highlight-color: white;
    			scrollbar-shadow-color: #000000;
    			scrollbar-3dlight-color: #000000;
    			scrollbar-arrow-color: white;
    			scrollbar-darkshadow-color: #000000;
    			scrollbar-track-color: #000000;
    
    		 }
    		 div 
    		 {
    			width: 97px;
    			text-align: center;
    			padding: 2px;
    		 }
    		 
    	</STYLE>
    	</HEAD>
    	<body BACKGROUND="Motion_HtmlCtrl_BK.bmp" id='bkArea' scroll="no" onload = "InitMotionStyleTable()">
    		<!--font face="Segoe UI">
    			<form name="myForm">
    			</form>
    			<div style="OVERFLOW:hidden" id="resultArea">	
    			</div>
    		</font-->
    		<!--table id='MotionStyleTable' cellspacing='20'></table-->
    		
    	</body>
    </html>
    
    
    
    

     

    目前遇到的問題是, 這段code以往是有效的, 但是最近發現在某些平台下, GetIDsOfNames會回傳

    COMError: -2147352570, "不明的名稱", 找不到javaScript裡面的function name, 而這些平台的共同點是OS都是win7,不知道

    為甚麼會遇到這樣的問題?

     

     

     

    2011年4月19日 3:57

答案

  • 我觉得这种问题也许和IE8 的安全性有关系。 建议找GTSC 开个 case 搞搞清楚。


    Would you know my name, if I saw you in heaven......
    2011年4月19日 12:01
    版主