Wiki - README‎ > ‎Programming‎ > ‎

C#

winforms faq [link]

opentk: save bmp [link]

Clearing TreeView with lots (thousands) of nodes is (really, really) slow [link]
treeview.SelectedNode = null;
treeview.Nodes.Clear();

Low-level keyboard hooks (link)
Low-level mouse hooks (link)
class InterceptMouse { private static LowLevelMouseProc _proc = HookCallback; private static IntPtr _hookID = IntPtr.Zero; public static void Main() { _hookID = SetHook(_proc); Application.Run(); UnhookWindowsHookEx(_hookID); } private static IntPtr SetHook(LowLevelMouseProc proc) { using (Process curProcess = Process.GetCurrentProcess()) using (ProcessModule curModule = curProcess.MainModule) { return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0); } } private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam); private static IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam) { MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y); } return CallNextHookEx(_hookID, nCode, wParam, lParam); } private const int WH_MOUSE_LL = 14; private enum MouseMessages { WM_LBUTTONDOWN = 0x0201, WM_LBUTTONUP = 0x0202, WM_MOUSEMOVE = 0x0200, WM_MOUSEWHEEL = 0x020A, WM_RBUTTONDOWN = 0x0204, WM_RBUTTONUP = 0x0205 } [StructLayout(LayoutKind.Sequential)] private struct POINT { public int x; public int y; } [StructLayout(LayoutKind.Sequential)] private struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; } [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool UnhookWindowsHookEx(IntPtr hhk); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); }
101 Linq Samples (link)

Remote Execution (link)
Remoting Configuration (link)
Dynamicall executing on .NET (link)

IEnumerable example (link)

serializing flag enums (link)
<Flags()> _
Public Enum TestEnumType

Suds = 1
Soda = 2
Beer = 4

End Enum

convert string to stream (link)

convert string to generic (link)
public static T To<T>(this string text)
{
    return (T)Convert.ChangeType(text, typeof(T));
}

useful xml deserialization with OnXmlDeserialization callback.  would be nice to have [OnXmlSerializedAttribute] to stick on to functions... maybe someday
public interface IXmlDeserializationCallback
{
void OnXmlDeserialization(object sender);
}

public class CustomXmlDeserializer<T> : XmlSerializer where T:IXmlDeserializationCallback
{
public CustomXmlDeserializer() : base( typeof( T ) ) { }
public T CustomDeserialize( TextReader reader ) { return (T) Deserialize(reader); }
protected override object Deserialize(XmlSerializationReader reader)
{
T result = (T) base.Deserialize(reader);
Callback( result );
return result;
}
// some black magic happens here :)
public void Callback( object o )
{
Type type = o.GetType();
List<Type> interfaces = new List<Type>( type.GetInterfaces() );
if( interfaces.Contains( typeof( IXmlDeserializationCallback ) ) )
{
((IXmlDeserializationCallback)o).OnXmlDeserialization(this);
}
if( interfaces.Contains( typeof( IEnumerable ) ) || interfaces.Contains( typeof( IList ) ) )
{
foreach( object obj in (IEnumerable) o )
Callback( obj );
}
foreach( FieldInfo fi in type.GetFields(BindingFlags.Public|BindingFlags.Instance) )
{
object obj = fi.GetValue(o);
if( obj != null ) Callback( obj);
}
}
}



Comments