Ahmad Masykur

Share your knowledge although one function!

About the author

Ahmad Masykur is a Software Architecture Engineer at PT. Freeport Indonesia Jakarta Indonesia.
In this blog, I share things of interest to me. Most topics are likely to be related to software development, but don't hold me to it.

Certificates



Awards


Powered by

Widget Prayer Time not found.

There is an error in XML document (4, 16278).X

Page List

Validators


Ahmad Masykur

Menangkap MouseWheel Event dengan Silverlight 2

Pada Silverlight 1.0 saya pernah menulis bagaimana mendeteksi event mousewheel (scroll) di Hacking Mouse Scroll Event pada Silverlight 1.0. Setelah keluarnya Silverlight 2, pemprograman tidak lagi menggunakan JavaScript sehingga cara tersebut sudah tidak lagi bisa digunakan. Untungnya Silverlight 2 memiliki mekanisme untuk menangkap event yang terjadi di HTML object yaitu System.Windows.Browser.HtmlPage.Window.AttachEvent. Dengan method tersebut, kita bisa tangkap event untuk mousewheel.

Event mousewheel masing-masing browser yang didukung oleh Silverlight memiliki perbedaan yaitu:

  • Mozilla dan Safari menggunakan event "DOMMouseScroll" pada element window
  • IE menggunakan event "onmousewheel" pada element document
  • Opera menggunakan event "onmousewheel" pada element window.

Perbedaan ini mengharuskan untuk menangkap ketiga event tersebut dan menghandle di satu buah event handler.

HtmlPage.Window.AttachEvent("DOMMouseScroll", OnMouseWheel); HtmlPage.Window.AttachEvent("onmousewheel", OnMouseWheel); HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel);

Setelah event diregister ke handler yang sama, ada lagi perbedaan masing-masing browser pada waktu menangkap nilai property mouse wheel.

  • Pada IE dan Opera, nilai diambil dari property "wheelDelta".
  • Pada Mozilla dan Safari, nilai diambil dari property "detail".

Selain property-nya berbeda nilai yang dihasilkan juga berbeda.

  • Pada IE dan Opera, setiap scroll akan menghasilkan nilai kelipatan +120 atau -120. Pada Opera terjadi perbedaan dengan IE mengenai tanda plus/minus. Opera menghasilkan nilai kebalikan dari IE. Oleh karena itu, untuk menyamakan nilai keluaran, pada Opera tandanya harus dibalik.
  • Pada Mozilla dan Safari, setiap scroll akan menghasilkan nilai kelipatan +3 atau -3

Perbedaan nilai antara masing-masing browser perlu disamakan. Untuk menyamakan, digunakan +1 atau -1 sehingga penanganan di aplikasi menjadi lebih mudah.

private void OnMouseWheel(object sender, HtmlEventArgs args) { double delta = 0; ScriptObject e = args.EventObject; if (e.GetProperty("detail") != null) { // Mozilla and Safari delta = ((double)e.GetProperty("detail")); } else if (e.GetProperty("wheelDelta") != null) { // IE and Opera delta = ((double)e.GetProperty("wheelDelta")); // In Opera 9, delta differs in sign as compared to IE. if (HtmlPage.BrowserInformation.UserAgent.IndexOf("opera") > -1) delta *= -1; } delta = Math.Sign(delta); if (MouseWheelScroll != null) MouseWheelScroll(new MouseWheelEventArgs(delta)); }

Nilai delta dalam method di atas yang kemudian diambil dalam aplikasi. Supaya method-method ini dapat dipakai di beberapa tempat (reusable). Perlu dibungkus dalam class dan nilai delta dilempar dalam event. Kode selengkapnya dapat dilihat di bawah.

using System; using System.Windows.Browser; /// <summary> /// Delegate for the MouseWheelScroll event. /// </summary> /// <param name="args">Event data for the MouseWheelScroll event.</param> public delegate void MouseWheelHandler(MouseWheelEventArgs args); /// <summary> /// Event data for the MouseWheelScroll event. /// </summary> public class MouseWheelEventArgs : EventArgs { private readonly double m_delta; public double Delta { get { return m_delta; } } public MouseWheelEventArgs(double delta) { m_delta = delta; } } /// <summary> /// Provides access to the browser-generated mouse wheel scrolling events. /// </summary> public class MouseWheelListener : IDisposable { public event MouseWheelHandler MouseWheelScroll; public MouseWheelListener() { HtmlPage.Window.AttachEvent("DOMMouseScroll", OnMouseWheel); HtmlPage.Window.AttachEvent("onmousewheel", OnMouseWheel); HtmlPage.Document.AttachEvent("onmousewheel", OnMouseWheel); } private void OnMouseWheel(object sender, HtmlEventArgs args) { double delta = 0; ScriptObject e = args.EventObject; if (e.GetProperty("detail") != null) { // Mozilla and Safari delta = ((double)e.GetProperty("detail")); } else if (e.GetProperty("wheelDelta") != null) { // IE and Opera delta = ((double)e.GetProperty("wheelDelta")); // In Opera 9, delta differs in sign as compared to IE. if (HtmlPage.BrowserInformation.UserAgent.IndexOf("opera") > -1) delta *= -1; } delta = Math.Sign(delta); if (MouseWheelScroll != null) MouseWheelScroll(new MouseWheelEventArgs(delta)); } /// <summary> /// Detaches from the browser-generated scroll events. /// </summary> public void Dispose() { HtmlPage.Window.DetachEvent("DOMMouseScroll", OnMouseWheel); HtmlPage.Window.DetachEvent("onmousewheel", OnMouseWheel); HtmlPage.Document.DetachEvent("onmousewheel", OnMouseWheel); } }

Semoga bermanfaat.


Categories: Silverlight
Permalink | Comments (13) | Post RSSRSS comment feed

Comments

busvy seo test Indonesia | Reply

Sunday, November 16, 2008 10:43 PM

busvy seo test

nampaknya saya harus berburu kepda anda ni//Smile

busby seo test Indonesia | Reply

Sunday, November 16, 2008 10:58 PM

busby seo test

wadugh bos.ribet bgd dagh

Busby SEO Test United States | Reply

Saturday, December 06, 2008 6:18 PM

Busby SEO Test

Apanya yg ribet ?

busby seo test United States | Reply

Saturday, December 13, 2008 3:17 PM

busby seo test

susah amat ya mencerna bahasa ASP Frown

charisma Indonesia | Reply

Tuesday, December 16, 2008 11:33 AM

charisma

assalammu'alaikum wr wb
aku pusing pusing cari cara untuk menggunakan silverlight pada php ...eh nggak taunya dapat ide "kenapa nggak coba pelajari aja silverligth dengan html selanjutnya dari file html itu nanti kan lebih mudah dipahami untuk membangun web site silverligh dengan file php,agar bisa menggunakan server linux..",allhamdulillah akhirnya tutorial anda benar benar sangat membantu saya...terima kasih..wassalammu'alaikum wr wb

ahmad Indonesia | Reply

Wednesday, January 07, 2009 3:53 PM

ahmad

Sebelum nya...saya minta Mafff dan Ucapkan terimakasih....  pada Yang Empunya Blog Ini...

saya kenal dan tau silverlight dari seminar sehari Microsoft Update Windows 7...saya sangat tertarik ...cuma...saya ga Punya Softwarenya....
Bolehkan saya Minta Sofrware silverlight ...
sebelumnya ucapkan terimakasih...

Ahmad maridi
Batam...
(maridek@yahoo.com)

Bovik Laan Penge Online United States | Reply

Tuesday, January 13, 2009 11:37 PM

Bovik Laan Penge Online

In Silverlight, animations can enhance your Web site by adding movement and interactivity. By animating a background color or applying an animated transform, you can create dramatic screen transitions or provide helpful visual cues. This QuickStart shows you how to create basic animations by changing property values and by using key frames.

Dubai Hotels United States | Reply

Friday, January 23, 2009 9:58 PM

Dubai Hotels

Silverlight powers rich application experiences wherever the Web works.

Los Angeles Attorney United States | Reply

Saturday, January 24, 2009 4:40 PM

Los Angeles Attorney

Use Silverlight in the 32-bit Internet Explorer process on x64 systems. Most browser plug-ins (including Silverlight, Flash, Java and almost ...

Charlotte Internet Marketing United States | Reply

Sunday, January 25, 2009 3:02 PM

Charlotte Internet Marketing

Microsoft Corp., the worldwide leader in software, services and solutions, and Akamai Technologies Inc., the leader in powering rich media, dynamic transactions and enterprise applications online, today announced plans to enable high-definition (HD) quality experiences on PCs. Akamai will release a beta version of a new service, powered by Windows Server 2008 and Microsoft Silverlight, to deliver rich Internet applications via the world’s largest HTTP edge network.

fly reels United States | Reply

Tuesday, January 27, 2009 10:53 PM

fly reels

Get the latest news on Silverlight. MIX09  brings together the best ideas, sessions, parties and people. It's where you'll find everything you need to <3 your Web.

Neil United States | Reply

Thursday, January 29, 2009 1:04 PM

Neil

Microsoft Silverlight powers rich application experiences and delivers high quality, interactive video across the Web and mobile devices through the most powerful runtime available on the Web.

Organic SEO United States | Reply

Sunday, February 08, 2009 3:07 PM

Organic SEO

Keeping it all straight, especially as we go through release cycles will be very difiicult and there is a real risk that developers coming to this site will be confused as to which version of, for example, Visual Studio, they want to use with which version of (for example) Silverlight.

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading