IE7 implements the multi-tab feature. You can use the target property to indicate that the link is opened in a window, but cannot use a similar property to indicate that the link is opened in a new tag. Now we implement a behavior (behavior) and attach it to element a, so that the new tag is automatically opened when the link is clicked. The effect is as follows:
- <A style = "behavior: URL (# default # tabnewdemo);" tabnewactive = "true" href = "http://www.pimshell.com"> test </a>
- Tabnewactive: indicates whether the new tag is activated.
You can also
- . Tabnewdemo {behavior: URL (# default # tabnewdemo );}
- <A class = "tabnewdemo" href = "http://www.pimshell.com"> goto my homepage </a>
Let's take a look at how IE handles tabnewdemo behavior.
- IE finds that the behavior name appended to element a is "# default # tabnewdemo". Default indicates that this is a registered default behavior. In the registry key HKLM/software/Microsoft/Internet Explorer/default behaviors, find the classid of the behavior factory component corresponding to tabnewdemo.
- Create an instance of the behavior factory component and call the behavior factory method findbehavior to obtain the behavior instance.
- Call the init method of the behavior instance to pass environment parameters.
- Call the action instance method notify. Here, the action is bound to The onclick event of the current A element.
- Ready. When a user clicks Element A, the Action responds to the onclick event and opens the link in the new tag by calling webbrowser. navigate2.
- When the page is closed, call the action instance method detach and clear the resource here.
The following describes how to implement tabnewdemo behavior.
Step 1: Create an atl com named tabnew,
Add the following registry key to pimshelldemo. RGS:
- HKLM
- {
- Noremove Software
- {
- Noremove Microsoft
- {
- Noremove 'Internet explorer'
- {
- Noremove 'default behaviors'
- {
- Val tabnewdemo = s 'clsid: E785D34B-7C76-4FC2-B6FE-E2A2AABEEDD9'
- }
- }
- }
- }
- }
Step 2: Inherit the ielementbehaviorfactory interface and implement the findbehavior method:
- // Ielementbehaviorfactory
- Stdmethodimp ctabnew: findbehavior (BSTR bstrbehavior, BSTR bstrbehaviorurl,
- Ielementbehaviorsite * psite, ielementbehavior ** ppbehavior)
- {
- Hresult hr;
- // Check
- If (! CVB: _ strcmpi (bstrbehavior, df_behavior_name ))
- Return e_fail;
- // Create
- Ccomobject <ctabnew> * ptabnew;
- HR = ccomobject <ctabnew>: createinstance (& ptabnew );
- If (failed (HR ))
- Return hr;
- // OK
- Return ptabnew-> QueryInterface (iid_ielementbehavior, (void **) ppbehavior );
- }
Step 3: Inherit the ielementbehavior interface and implement three methods:
- // Ielementbehavior
- Stdmethodimp ctabnew: Init (ielementbehaviorsite * pbehaviorsite)
- {
- _ Safecall_begin;
- // Cache the ielementbehaviorsite interface pointer.
- M_psite = pbehaviorsite;
- // Cache the ielementbehaviorsiteom interface pointer.
- M_pomsite = m_psite;
- //
- M_pomsite-> registername (df_behavior_name );
- _ Safecall_end;
- }
- //
- Stdmethodimp ctabnew: Notify (long Levent, variant * pvar)
- {
- _ Safecall_begin;
- Switch (Levent)
- {
- Case behaviorevent_contentready:
- // Attachevents
- This->__ attachevents (true );
- Break;
- }
- _ Safecall_end;
- }
- //
- Stdmethodimp ctabnew: Detach ()
- {
- _ Safecall_begin;
- // Detach events
- This->__ attachevents (false );
- //
- This-> m_psite = NULL;
- This-> m_pomsite = NULL;
- _ Safecall_end;
- }
Step 4: bind an onclick event using the _ attachevents Method
- //
- Void ctabnew ::__ attachevents (bool battach)
- {
- // Element
- Mshtml: ihtmlelementptr pelement = This-> m_psite-> getelement ();
- Mshtml: ihtmlelement2ptr pelement2 = pelement;
- //
- If (battach)
- {
- // Psystem
- // Shocould install pimshell (http://www.pimshell.com)
- Pimshellcore: iajaxsystem_helperptr psystem;
- Psystem. createinstance (_ uuidof (pimshellcore: cajaxsystem ));
- // This
- Iunknownptr pthis = this;
- // Delegate
- M_pdelegate_onevents = psystem-> createdelegatevc1 (pthis, (Longlong) & sink_onevents );
- // Attach event of onclick
- Pelement2-> attachevent (L "onclick", m_pdelegate_onevents );
- }
- Else
- {
- If (m_pdelegate_onevents! = NULL)
- {
- // Detach event of onclick
- Pelement2-> detachevent (L "onclick", m_pdelegate_onevents );
- //
- M_pdelegate_onevents = NULL;
- }
- }
- }
Step 5: Respond to The onclick event
- //
- Hresult ctabnew: sink_onevents (variant vevent, variant vcontext, iunknown * pinstance, variant * pvarresult)
- {
- _ Safecall_begin;
- // Init
- Ctabnew * pthis = dynamic_cast <ctabnew *> (pinstance );
- Mshtml: ihtmleventobjptr E = vevent. pdispval;
- //
- Cstring stype = e-> type;
- //
- If (stype = l "click ")
- {
- Pthis->__ onclick (E );
- }
- _ Safecall_end;
- }
- //
- Void ctabnew :__ onclick (const mshtml: ihtmleventobjptr & E)
- {
- // Shift or Ctrl may by pressed by code.
- Variant_bool bshift, bctrl, Balt;
- Bshift = e-> shiftkey;
- Bctrl = e-> ctrlkey;
- Balt = e-> altkey;
- // If shift or Ctrl then return
- If (bshift | bctrl | Balt)
- {
- Return;
- }
- // Element
- Mshtml: ihtmlelementptr pelement = This-> m_psite-> getelement ();
- // Psystem
- // Shocould install pimshell (http://www.pimshell.com)
- Pimshellcore: iajaxsystemptr psys;
- Psys. createinstance (_ uuidof (pimshellcore: cajaxsystem ));
- // Tabnewactive href
- Bool btabnewactive = psys-> getattribute (pelement, l "tabnewactive", false );
- _ Bstr_t bstrhref = psys-> getattribute (pelement, l "href", l "");
- // Webbrowser
- Ccomptr <iwebbrowser2> pwebbrowser;
- _ Ccutegeneric ::__ getwebbrowser (this-> m_pomsite, & pwebbrowser );
- // URL
- _ Variant_t vurl = bstrhref;
- // Flag
- Long nflags = btabnewactive? (Navopeninnewtab) :( navopeninbackgroundtab );
- _ Variant_t vflag = nflags;
- // Navigate
- Pwebbrowser-> navigate2 (& vurl, & vflag, null );
- // OK
- E-> returnvalue = false;
- E-> cancelbubble = true;
- }
* Source Code download:Http://www.pimshell.com/pimshell/downloads/PIMShellDemo.zip
* System binds the mechanism of the element onclick event, see: http://blog.csdn.net/pimshell/archive/2008/08/05/2773717.aspx