Freitag, 11. Oktober 2013

How to capture a mouseclick system-wide using System.Runtime.InteropServices

If you need a short sample to handle this rare issue, consider the following steps...

* create a new WPF Application
* add a reference to System.Runtime.InteropServices
* add a public static class called "hook.cs"
* in App.xaml.cs add a static variable to buffer the hook result code
public static int hHook =0 ;

* in hook.cs add the namespace System.Runtime.InteropServices
* in hook cs add a static public method called Register() and save the hook value in the hHook of App.xaml.cs
* in hook.cs add a static public method called Unregister()
* in hook.cs add a static method as the MouseProcedure to handle the global messages
* in hook.cs add 2x Imports to register, unregister the hook and add 1x Import to pass the hook-Event. (DllImport user32.dll)
* in hook.cs -> MouseProcedure reacts on the mouseclick signal WM_LBUTTONDOWN
* register the hook in the startup of the application through calling the static method register() of the class in the hook.cs
* by leaving the application unregister such hook again through the appropriate method in hook.cs

download the short sample project here and check it in detail!

(based on

Benefit from the best Windows Desktop app in the world and use Strokey.Net!

Dienstag, 20. August 2013

variant how to read a text-stream blockwise using StreamReader ReadBlock

string Textfile = @"C:\hello.txt";
Encoding enc = Encoding.Default;

FileInfo fi = new FileInfo(textfile);long remainingTillEnd = fi.Length;

int readTotalUntilNow = 0;int blocksize = 4096;
char[] buffer = new char[blocksize];

using (StreamReader sr = new StreamReader(Textfile,enc))
bool readon = true;

while (readon)
if (remainingTillEnd > blocksize)

readTotalUntilNow += sr.ReadBlock(buffer, 0, blocksize);

readTotalUntilNow += sr.ReadBlock(buffer, 0, (int)remainingTillEnd);

readon = false; // last block indicator
remainingTillEnd = fi.Length - readTotalUntilNow;

Benefit from the best Windows Desktop app in the world and use Strokey.Net!