Go to pimshell (3)-implement tabnew behavior (behavior)

Source: Internet
Author: User

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:

  1. <A style = "behavior: URL (# default # tabnewdemo);" tabnewactive = "true" href = "http://www.pimshell.com"> test </a>
  2. Tabnewactive: indicates whether the new tag is activated.

You can also

  1. . Tabnewdemo {behavior: URL (# default # tabnewdemo );}
  2. <A class = "tabnewdemo" href = "http://www.pimshell.com"> goto my homepage </a>

Let's take a look at how IE handles tabnewdemo behavior.

  1. 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.
  2. Create an instance of the behavior factory component and call the behavior factory method findbehavior to obtain the behavior instance.
  3. Call the init method of the behavior instance to pass environment parameters.
  4. Call the action instance method notify. Here, the action is bound to The onclick event of the current A element.
  5. 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.
  6. 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:

  1. HKLM
  2. {
  3. Noremove Software
  4. {
  5. Noremove Microsoft
  6. {
  7. Noremove 'Internet explorer'
  8. {
  9. Noremove 'default behaviors'
  10. {
  11. Val tabnewdemo = s 'clsid: E785D34B-7C76-4FC2-B6FE-E2A2AABEEDD9'
  12. }
  13. }
  14. }
  15. }
  16. }

Step 2: Inherit the ielementbehaviorfactory interface and implement the findbehavior method:

  1. // Ielementbehaviorfactory
  2. Stdmethodimp ctabnew: findbehavior (BSTR bstrbehavior, BSTR bstrbehaviorurl,
  3. Ielementbehaviorsite * psite, ielementbehavior ** ppbehavior)
  4. {
  5. Hresult hr;
  6. // Check
  7. If (! CVB: _ strcmpi (bstrbehavior, df_behavior_name ))
  8. Return e_fail;
  9. // Create
  10. Ccomobject <ctabnew> * ptabnew;
  11. HR = ccomobject <ctabnew>: createinstance (& ptabnew );
  12. If (failed (HR ))
  13. Return hr;
  14. // OK
  15. Return ptabnew-> QueryInterface (iid_ielementbehavior, (void **) ppbehavior );
  16. }

Step 3: Inherit the ielementbehavior interface and implement three methods:

  1. // Ielementbehavior
  2. Stdmethodimp ctabnew: Init (ielementbehaviorsite * pbehaviorsite)
  3. {
  4. _ Safecall_begin;
  5. // Cache the ielementbehaviorsite interface pointer.
  6. M_psite = pbehaviorsite;
  7. // Cache the ielementbehaviorsiteom interface pointer.
  8. M_pomsite = m_psite;
  9. //
  10. M_pomsite-> registername (df_behavior_name );
  11. _ Safecall_end;
  12. }
  13. //
  14. Stdmethodimp ctabnew: Notify (long Levent, variant * pvar)
  15. {
  16. _ Safecall_begin;
  17. Switch (Levent)
  18. {
  19. Case behaviorevent_contentready:
  20. // Attachevents
  21. This->__ attachevents (true );
  22. Break;
  23. }
  24. _ Safecall_end;
  25. }
  26. //
  27. Stdmethodimp ctabnew: Detach ()
  28. {
  29. _ Safecall_begin;
  30. // Detach events
  31. This->__ attachevents (false );
  32. //
  33. This-> m_psite = NULL;
  34. This-> m_pomsite = NULL;
  35. _ Safecall_end;
  36. }

Step 4: bind an onclick event using the _ attachevents Method

  1. //
  2. Void ctabnew ::__ attachevents (bool battach)
  3. {
  4. // Element
  5. Mshtml: ihtmlelementptr pelement = This-> m_psite-> getelement ();
  6. Mshtml: ihtmlelement2ptr pelement2 = pelement;
  7. //
  8. If (battach)
  9. {
  10. // Psystem
  11. // Shocould install pimshell (http://www.pimshell.com)
  12. Pimshellcore: iajaxsystem_helperptr psystem;
  13. Psystem. createinstance (_ uuidof (pimshellcore: cajaxsystem ));
  14. // This
  15. Iunknownptr pthis = this;
  16. // Delegate
  17. M_pdelegate_onevents = psystem-> createdelegatevc1 (pthis, (Longlong) & sink_onevents );
  18. // Attach event of onclick
  19. Pelement2-> attachevent (L "onclick", m_pdelegate_onevents );
  20. }
  21. Else
  22. {
  23. If (m_pdelegate_onevents! = NULL)
  24. {
  25. // Detach event of onclick
  26. Pelement2-> detachevent (L "onclick", m_pdelegate_onevents );
  27. //
  28. M_pdelegate_onevents = NULL;
  29. }
  30. }
  31. }

Step 5: Respond to The onclick event

  1. //
  2. Hresult ctabnew: sink_onevents (variant vevent, variant vcontext, iunknown * pinstance, variant * pvarresult)
  3. {
  4. _ Safecall_begin;
  5. // Init
  6. Ctabnew * pthis = dynamic_cast <ctabnew *> (pinstance );
  7. Mshtml: ihtmleventobjptr E = vevent. pdispval;
  8. //
  9. Cstring stype = e-> type;
  10. //
  11. If (stype = l "click ")
  12. {
  13. Pthis->__ onclick (E );
  14. }
  15. _ Safecall_end;
  16. }
  17. //
  18. Void ctabnew :__ onclick (const mshtml: ihtmleventobjptr & E)
  19. {
  20. // Shift or Ctrl may by pressed by code.
  21. Variant_bool bshift, bctrl, Balt;
  22. Bshift = e-> shiftkey;
  23. Bctrl = e-> ctrlkey;
  24. Balt = e-> altkey;
  25. // If shift or Ctrl then return
  26. If (bshift | bctrl | Balt)
  27. {
  28. Return;
  29. }
  30. // Element
  31. Mshtml: ihtmlelementptr pelement = This-> m_psite-> getelement ();
  32. // Psystem
  33. // Shocould install pimshell (http://www.pimshell.com)
  34. Pimshellcore: iajaxsystemptr psys;
  35. Psys. createinstance (_ uuidof (pimshellcore: cajaxsystem ));
  36. // Tabnewactive href
  37. Bool btabnewactive = psys-> getattribute (pelement, l "tabnewactive", false );
  38. _ Bstr_t bstrhref = psys-> getattribute (pelement, l "href", l "");
  39. // Webbrowser
  40. Ccomptr <iwebbrowser2> pwebbrowser;
  41. _ Ccutegeneric ::__ getwebbrowser (this-> m_pomsite, & pwebbrowser );
  42. // URL
  43. _ Variant_t vurl = bstrhref;
  44. // Flag
  45. Long nflags = btabnewactive? (Navopeninnewtab) :( navopeninbackgroundtab );
  46. _ Variant_t vflag = nflags;
  47. // Navigate
  48. Pwebbrowser-> navigate2 (& vurl, & vflag, null );
  49. // OK
  50. E-> returnvalue = false;
  51. E-> cancelbubble = true;
  52. }

* 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

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.