<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Maciej Grabek &#187; InteropServices</title>
	<atom:link href="https://blog.maciejgrabek.com/tag/interopservices/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.maciejgrabek.com</link>
	<description>/* Make it See Sharp - Windows Phone, C#, .NET i nie tylko */</description>
	<lastBuildDate>Mon, 27 Oct 2014 11:18:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>Przeklikaj swój świat! &#8211; kontrola zdarzeń myszki</title>
		<link>https://blog.maciejgrabek.com/2010/08/16/przeklikaj-swoj-swiat-kontrola-zdarzen-myszki/</link>
		<comments>https://blog.maciejgrabek.com/2010/08/16/przeklikaj-swoj-swiat-kontrola-zdarzen-myszki/#comments</comments>
		<pubDate>Mon, 16 Aug 2010 19:59:00 +0000</pubDate>
		<dc:creator><![CDATA[maciek]]></dc:creator>
				<category><![CDATA[Ciekawostki]]></category>
		<category><![CDATA[HOW TO]]></category>
		<category><![CDATA[InteropServices]]></category>
		<category><![CDATA[Mouse Helper]]></category>
		<category><![CDATA[MOUSEEVENTF]]></category>

		<guid isPermaLink="false">http://maciejgrabek.com/maciek_blog/?p=9994</guid>
		<description><![CDATA[Myszka – z pozoru urządzenie, które bez ludzkiej ręki nie jest w stanie nic zrobić. Błąd! Istnieje możliwość nadania jej “życia” poprzez wywoływanie odpowiednich zdarzeń z kodu aplikacji. Aby to osiągnąć przydatne może być wykorzystanie poniższego helpera, a właściwie szablonu helpera, który można w łatwy sposób rozszerzyć o kolejne metody:   1 using System.Drawing;   2 using System.Runtime.InteropServices;   3 using System.Windows.Forms;   4   5 namespace ConsAppClicker   6 {   7     public static class MouseHelper   8     {   9         #region Internal 10         [DllImport(&#8220;user32.dll&#8221;, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)] 11         private static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo); 12 13         private const int MOUSEEVENTF_LEFTDOWN = 0x02; 14         private const int MOUSEEVENTF_LEFTUP = 0x04; 15         private const int MOUSEEVENTF_RIGHTDOWN = 0x08; 16         private const int MOUSEEVENTF_RIGHTUP = 0x10; 17         #endregion 18 19         #region LMB 20         public static void RaiseDoubleClick() 21         { 22             int x = Cursor.Position.X; 23             int y = Cursor.Position.Y; 24             RaiseLeftClick(x, y); 25             RaiseLeftClick(x, y); 26         } 27 28         public static void RaiseLeftClick() 29         { 30             int x = Cursor.Position.X; 31             int y = Cursor.Position.Y; 32             RaiseLeftClick(x, y); 33         } 34 35 private static void RaiseLeftClick(int x, int y) 36 { 37     mouse_event(MOUSEEVENTF_LEFTDOWN &#124; MOUSEEVENTF_LEFTUP, x, y, 0, 0); 38 } 39         #endregion 40 41         #region RMB 42         public static void<a href="https://blog.maciejgrabek.com/2010/08/16/przeklikaj-swoj-swiat-kontrola-zdarzen-myszki/" class="searchmore">Read the Rest...</a><div class="clr"></div>]]></description>
				<content:encoded><![CDATA[<p>Myszka – z pozoru urządzenie, które bez ludzkiej ręki nie jest w stanie nic zrobić. Błąd! Istnieje możliwość nadania jej “życia” poprzez wywoływanie odpowiednich zdarzeń z kodu aplikacji. Aby to osiągnąć przydatne może być wykorzystanie poniższego helpera, a właściwie szablonu helpera, który można w łatwy sposób rozszerzyć o kolejne metody:</p>
<div style="font-family: courier new;"><span style="color: teal;">  1</span> <span style="color: blue;">using</span> System.Drawing;<br />
<span style="color: teal;">  2</span> <span style="color: blue;">using</span> System.Runtime.InteropServices;<br />
<span style="color: teal;">  3</span> <span style="color: blue;">using</span> System.Windows.Forms;<br />
<span style="color: teal;">  4</span><br />
<span style="color: teal;">  5</span> <span style="color: blue;">namespace</span> ConsAppClicker<br />
<span style="color: teal;">  6</span> {<br />
<span style="color: teal;">  7</span>     <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> MouseHelper<br />
<span style="color: teal;">  8</span>     {<br />
<span style="color: teal;">  9</span>         <span style="color: blue;">#region</span> Internal<br />
<span style="color: teal;">10</span>         [DllImport(<span style="color: maroon;">&#8220;user32.dll&#8221;</span>, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]<br />
<span style="color: teal;">11</span>         <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">extern</span> <span style="color: blue;">void</span> mouse_event(<span style="color: blue;">long</span> dwFlags, <span style="color: blue;">long</span> dx, <span style="color: blue;">long</span> dy, <span style="color: blue;">long</span> cButtons, <span style="color: blue;">long</span> dwExtraInfo);<br />
<span style="color: teal;">12</span><br />
<span style="color: teal;">13</span>         <span style="color: blue;">private</span> <span style="color: blue;">const</span> <span style="color: blue;">int</span> MOUSEEVENTF_LEFTDOWN = <span style="color: maroon;">0x02</span>;<br />
<span style="color: teal;">14</span>         <span style="color: blue;">private</span> <span style="color: blue;">const</span> <span style="color: blue;">int</span> MOUSEEVENTF_LEFTUP = <span style="color: maroon;">0x04</span>;<br />
<span style="color: teal;">15</span>         <span style="color: blue;">private</span> <span style="color: blue;">const</span> <span style="color: blue;">int</span> MOUSEEVENTF_RIGHTDOWN = <span style="color: maroon;">0x08</span>;<br />
<span style="color: teal;">16</span>         <span style="color: blue;">private</span> <span style="color: blue;">const</span> <span style="color: blue;">int</span> MOUSEEVENTF_RIGHTUP = <span style="color: maroon;">0x10</span>;<br />
<span style="color: teal;">17</span>         <span style="color: blue;">#endregion</span><br />
<span style="color: teal;">18</span><br />
<span style="color: teal;">19</span>         <span style="color: blue;">#region</span> LMB<br />
<span style="color: teal;">20</span>         <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> RaiseDoubleClick()<br />
<span style="color: teal;">21</span>         {<br />
<span style="color: teal;">22</span>             <span style="color: blue;">int</span> x = Cursor.Position.X;<br />
<span style="color: teal;">23</span>             <span style="color: blue;">int</span> y = Cursor.Position.Y;<br />
<span style="color: teal;">24</span>             RaiseLeftClick(x, y);<br />
<span style="color: teal;">25</span>             RaiseLeftClick(x, y);<br />
<span style="color: teal;">26</span>         }<br />
<span style="color: teal;">27</span><br />
<span style="color: teal;">28</span>         <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> RaiseLeftClick()<br />
<span style="color: teal;">29</span>         {<br />
<span style="color: teal;">30</span>             <span style="color: blue;">int</span> x = Cursor.Position.X;<br />
<span style="color: teal;">31</span>             <span style="color: blue;">int</span> y = Cursor.Position.Y;<br />
<span style="color: teal;">32</span>             RaiseLeftClick(x, y);<br />
<span style="color: teal;">33</span>         }<br />
<span style="color: teal;">34</span><br />
<span style="color: teal;">35</span> <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> RaiseLeftClick(<span style="color: blue;">int</span> x, <span style="color: blue;">int</span> y)<br />
<span style="color: teal;">36</span> {<br />
<span style="color: teal;">37</span>     mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, <span style="color: maroon;">0</span>, <span style="color: maroon;">0</span>);<br />
<span style="color: teal;">38</span> }<br />
<span style="color: teal;">39</span>         <span style="color: blue;">#endregion</span><br />
<span style="color: teal;">40</span><br />
<span style="color: teal;">41</span>         <span style="color: blue;">#region</span> RMB<br />
<span style="color: teal;">42</span>         <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> RaiseRightClick()<br />
<span style="color: teal;">43</span>         {<br />
<span style="color: teal;">44</span>             <span style="color: blue;">int</span> x = Cursor.Position.X;<br />
<span style="color: teal;">45</span>             <span style="color: blue;">int</span> y = Cursor.Position.Y;<br />
<span style="color: teal;">46</span>             RaiseRightClick(x, y);<br />
<span style="color: teal;">47</span>         }<br />
<span style="color: teal;">48</span><br />
<span style="color: teal;">49</span>         <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> RaiseRightClick(<span style="color: blue;">int</span> x, <span style="color: blue;">int</span> y)<br />
<span style="color: teal;">50</span>         {<br />
<span style="color: teal;">51</span>             mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, x, y, <span style="color: maroon;">0</span>, <span style="color: maroon;">0</span>);<br />
<span style="color: teal;">52</span>         }<br />
<span style="color: teal;">53</span>         <span style="color: blue;">#endregion</span><br />
<span style="color: teal;">54</span><br />
<span style="color: teal;">55</span>         <span style="color: blue;">#region</span> Position<br />
<span style="color: teal;">56</span>         <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> SetPosition(<span style="color: blue;">int</span> x, <span style="color: blue;">int</span> y)<br />
<span style="color: teal;">57</span>         {<br />
<span style="color: teal;">58</span>             Cursor.Position = <span style="color: blue;">new</span> Point(x, y);<br />
<span style="color: teal;">59</span>         }<br />
<span style="color: teal;">60</span>         <span style="color: blue;">#endregion</span><br />
<span style="color: teal;">61</span>     }<br />
<span style="color: teal;">62</span> }<br />
<span style="color: teal;">63</span></div>
<p>Oferuje on możliwość ustawienia odpowiedniej pozycji kursora myszy oraz wywołania najważniejszych jej zdarzeń dla aktualnej pozycji. Jak widać przy pomocy PInvoke możemy wykorzystać wywołanie zdarzenia znajdujące się w bibliotece user32.dll, która przyjmuje flagę zdarzenia (bitowa flaga, lub ich suma) oraz współrzędne zdarzenia. Eksperymentując z tym helperem w<br />
pewnym momencie zapewne zauważycie, że zdarzenie jest uruchamiane dla aktualnej pozycji kursora niezależnie od tego co jest podane w linii 51 (wywołanie mouse_event)&#8230; Dziwne zachowanie, być może zależy od czynników, do których jeszcze nie dotarłem podczas drążenia tematu. W związku z tym można by korzystać tylko z metody SetPosition oraz ze zmienionej postaci Raise*Click, na przykład:</p>
<div style="font-family: courier new;"><span style="color: teal;">  1</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> RaiseRightClick(<span style="color: blue;">int</span> x, <span style="color: blue;">int</span> y)<br />
<span style="color: teal;">  2</span> {<br />
<span style="color: teal;">  3</span>     mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, <span style="color: maroon;">0</span>, <span style="color: maroon;">0</span>, <span style="color: maroon;">0</span>, <span style="color: maroon;">0</span>);<br />
<span style="color: teal;">  4</span> }</div>
<div style="font-family: courier new;"> </div>
<p>Oto przykład wykorzystania, który przy domyślnych ustawieniach pulpitu i ikon (“Mój&#8221; komputer” w lewym gornym rogu) po dwóch sekundach od uruchomienia otworzy okno eksploratora plików.</p>
<div style="font-family: courier new;"><span style="color: teal;">  1</span> <span style="color: blue;">using</span> System;<br />
<span style="color: teal;">  2</span><br />
<span style="color: teal;">  3</span> <span style="color: blue;">namespace</span> ConsAppClicker<br />
<span style="color: teal;">  4</span> {<br />
<span style="color: teal;">  5</span>     <span style="color: blue;">class</span> Program<br />
<span style="color: teal;">  6</span>     {<br />
<span style="color: teal;">  7</span>         <span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)<br />
<span style="color: teal;">  8</span>         {<br />
<span style="color: teal;">  9</span>             Console.WriteLine(<span style="color: maroon;">&#8220;Console mouse click demo&#8221;</span>);<br />
<span style="color: teal;">10</span>             System.Threading.Thread.Sleep(<span style="color: maroon;">2000</span>);<br />
<span style="color: teal;">11</span>             MouseHelper.SetPosition(<span style="color: maroon;">20</span>, <span style="color: maroon;">20</span>);<br />
<span style="color: teal;">12</span>             MouseHelper.RaiseDoubleClick();<br />
<span style="color: teal;">13</span>             Console.WriteLine(<span style="color: maroon;">&#8220;Done!&#8221;</span>);<br />
<span style="color: teal;">14</span>             Console.ReadLine();<br />
<span style="color: teal;">15</span>         }<br />
<span style="color: teal;">16</span>     }<br />
<span style="color: teal;">17</span> }</div>
<div style="font-family: courier new;"> </div>
<p>Teraz ożywienie poczciwego “szczura” jest już w zasięgu ręki. Miłej zabawy!</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.maciejgrabek.com/2010/08/16/przeklikaj-swoj-swiat-kontrola-zdarzen-myszki/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
