Lm th? no ?: Ch?m ?c qu?n l? C# cc s? ki?n trong Internet Explorer Script

D?ch tiu ? D?ch tiu ?
ID c?a bi: 313891 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?

? Trang ny

TM T?T

Bi vi?t t?ng b?c ny cho b?n th?y lm th? no ? ch?m cc s? ki?n ?c qu?n l? t? cc m h?nh ?i t?ng thnh ph?n (COM) khch hng (khng ?c qu?n l? m?) khi b?n vi?t.NET Windows i?u khi?n. V d?, b?n ch?m cc s? ki?n ?c qu?n l? t? COM khch hng khi b?n ch?y script trong Internet Explorer.

Thng tin v? lm th? no ? vi?t v s? d?ng cc lo?i ?c qu?n l? t? COM, ch? ?n Microsoft sau.Ti li?u h?ng d?n pht tri?n NET Framework:
L?.NET Framework cc thnh ph?n COM
http://msdn2.Microsoft.com/en-US/Library/zsfww439 (vs.71) .aspx

Yu c?u

Danh sch sau v?ch ra ?c ? ngh? ph?n c?ng, ph?n m?m, c s? h? t?ng m?ng v gi d?ch v? m b?n c?n:
  • Microsoft Visual Studio.NET
  • Microsoft Internet Explorer (chng tr?nh) Phin b?n 5.5 ho?c m?i hn

Cc b?c ? r?a chn qu?n l? s? ki?n trong Internet Explorer Script

  1. T?o ra m?t i?u khi?n c?a s? h?nh th?c ty ch?nh:
    1. B?t ?u Microsoft Visual Studio.NET.
    2. Trn cc Tp tr?nh n, i?m ?n M?i, sau b?m D an.
    3. Trong cc D? n m?i h?p tho?i h?p, b?m vo Visual C# cc d? n d?i Cc lo?i d? n, sau b?m Th vi?n i?u khi?n c?a Windows d?i Khun mu.
  2. Xc ?nh m?t giao di?n m? ngu?n cho cc s? ki?n ?c ti?p xc.
  3. Thm m?t GuidAttribute l?p giao di?n m? ngu?n. B?n ph?i ?nh d?ng chu?i m b?n v?t qua v?i cc thu?c tnh nh l ?i s? nh xy d?ng ch?p nh?n ?c ?i v?i lo?i Guid. B?n c th? s? d?ng cc t?p tin Guidgen.exe ? t?o ra m?t GUID khng s? d?ng.
    [GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ]
        
        public interface ControlEvents 
    
    		
        {
            //  Insert code here.
        }
    					
  4. Thm m?t InterfaceType thu?c tnh cho cc giao di?n ngu?n ? l? COM l m?t IDispatch giao di?n:
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
        public interface ControlEvents 
    
    		
        {
           
            // Insert code here.
        }
    					
  5. Thm m?t DispIdAttribute cho b?t k? thnh vin trong giao di?n m? ngu?n ? ch? ?nh COM g?i ?nh danh (DISPID) c?a m?t phng php ho?c m?t l?nh v?c:
    public interface ControlEvents 
    	
        {
            [DispIdAttribute(0x60020000)]
            void ClickEvent(int x, int y);
        }
    					
  6. T?o ra m?t lo?i s? ki?n m?i ? b?c cc s? ki?n mong mu?n ? l?.
  7. Th?c hi?n cc giao di?n ngu?n trn i?u khi?n c?a s? h?nh th?c ty ch?nh.
  8. Thm m?t ComSourceInterfaces thu?c tnh cho b? i?u khi?n ? xc ?nh danh sch cc giao di?n ?c ti?p xc nh COM s? ki?n ngu?n:
    [ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))]
    	public class MyWindowControl : System.Windows.Forms.UserControl 
        {
    
          // Insert code here.
        }
    					
  9. Bin d?ch s? ki?m sot nh l m?t t?p tin th vi?n lin k?t ?ng (DLL).
  10. T?o ra m?t kh?i t?p l?nh trn trang HTML ? treo cc s? ki?n. Vi du:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=iso-8859-1' />
    
    <HTML>
    	<HEAD>
    		<TITLE>Sink managed event in Internet Explorer</TITLE>
    		
    		
    	</HEAD>
    	
    	<BODY>
    		
    		<OBJECT id="ctrl" classid="YourDllName.dll#ActiveXSourcing.MyWindowControl">
    		</OBJECT>
    		<SCRIPT LANGUAGE="JScript">
                function ctrl::ClickEvent(a,b)
                {
                    alert("MyWindowControl_ClickEvent");
                }
    		</SCRIPT>
    		
    	</BODY>
    </HTML>
    					
  11. Trn b?t k? h? th?ng khch hng, s? d?ng cc.Cng c? c?u h?nh NET Framework (Mscorcfg.msc) ? c?p cho h?i ?ng quy?n c nhn ?c yu c?u.

Danh sch m? ?y ?

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ActiveXSourcing
{
	public delegate void ClickEventHandler(int x, int y); 

	//Source interface for events to be exposed.
	//Add GuidAttribute to the source interface to supply an explicit System.Guid.
	//Add InterfaceTypeAttribute to indicate that interface is IDispatch interface.
	[GuidAttribute("0422D916-C11A-474e-947D-45A107038D12") ]
	[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
	public interface ControlEvents 

		//Add a DisIdAttribute to any members in the source 
		//interface to specify the COM DispId.
	{
		[DispIdAttribute(0x60020000)]
		void ClickEvent(int x, int y);
	}

	//Add a ComSourceInterfaces attribute to the control to 
	//identify the list of interfaces that are exposed as COM event sources. 
	[ClassInterface(ClassInterfaceType.None),ComSourceInterfaces(typeof(ControlEvents))]
	public class MyWindowControl : System.Windows.Forms.UserControl 
		//, ComInteropControlInterface
	{
        System.Windows.Forms.TextBox tx = new TextBox();

		private void InitializeComponent()
		{
			
			this.Name = "MyWindowControl";

		}
	
		event ActiveXSourcing.ClickEventHandler ClickEvent;
    	
		public MyWindowControl() : base()
		{				               

			initMyWindowControl();

		}
        
		private void initMyWindowControl() 
		{

			Size = new System.Drawing.Size(300, 50);
			tx.Text = "Click on the TextBox to invoke  'ClickEvent'";
			tx.Size = this.Size;                  
			tx.Click += new System.EventHandler(ClickHandler);
			this.Controls.Add(tx);
			
		}
            
        
		private void ClickHandler(object sender, System.EventArgs e)
		{
			if (ClickEvent != null) 
			{
				ClickEvent(0, 0);
			}
		}
	}
}
				

THAM KH?O

? bi?t thm chi ti?t, ch? ? sau Microsoft Web site:
X? l? v nng cao s? ki?n
http://msdn2.Microsoft.com/en-US/Library/edzehd2t (vs.71) .aspx

.NET Framework cng c? c?u h?nh (Mscorcfg.msc)
http://msdn2.Microsoft.com/en-US/Library/2bc0cxhc (vs.71) .aspx

Interoperating v?i khng ?c qu?n l? m?
http://msdn2.Microsoft.com/en-US/Library/sd10k43k (vs.71) .aspx

My ch? lu tr? an ton, nh? pha khch hng ki?m sot trong Microsoft Internet Explorer
http://MSDN.Microsoft.com/msdnmag/issues/02/01/UserCtrl/Default.aspx
? bi?t thm chi ti?t, b?m vo s? bi vi?t d?i y ? xem bi vi?t trong c s? ki?n th?c Microsoft:
316510 L?I: An ninh ngo?i l? khi b?n s? d?ng x? l? s? ki?n trong Internet Explorer

Thu?c tnh

ID c?a bi: 313891 - L?n xem xt sau cng: 27 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual C# .NET 2002 Standard Edition
T? kha:
kbcominterop kbhowto kbhowtomaster kbsample kbmt KB313891 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:313891

Cung cp Phan hi

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com