Click here to Skip to main content
15,893,904 members
Home / Discussions / C#
   

C#

 
QuestionNot able to assign the picture to toolbar item. Pin
SRKSHOME26-Mar-10 2:16
SRKSHOME26-Mar-10 2:16 
AnswerRe: Not able to assign the picture to toolbar item. Pin
Ravi Bhavnani26-Mar-10 2:57
professionalRavi Bhavnani26-Mar-10 2:57 
GeneralRe: Not able to assign the picture to toolbar item. Pin
SRKSHOME26-Mar-10 3:49
SRKSHOME26-Mar-10 3:49 
QuestionHow to get the execution folder if the file executed is in another folder? Pin
Ted On The Net26-Mar-10 1:31
Ted On The Net26-Mar-10 1:31 
Answerfound the solution Pin
Ted On The Net26-Mar-10 1:52
Ted On The Net26-Mar-10 1:52 
AnswerRe: How to get the execution folder if the file executed is in another folder? Pin
R. Giskard Reventlov26-Mar-10 2:18
R. Giskard Reventlov26-Mar-10 2:18 
AnswerRe: How to get the execution folder if the file executed is in another folder? Pin
KChandos26-Mar-10 8:28
professionalKChandos26-Mar-10 8:28 
QuestionWindows Mobile Compact Edition 2.0 TextBox problem missing MouseClick event Pin
peks26-Mar-10 1:29
peks26-Mar-10 1:29 
I was spend for a long time looking for a decision of a problem, about the TextBox control that is included with Windows CE has a very limited opportunities for events related to mouse. I discovered that the team that developed the platform as it is a Compact Edition some controls are "clipped" from "unnecessary" events and an elaborate on them using unmanaged code by hooks and messages from the shell remitted. After a long search Internet and Googl-ing discovered fragments code and I have them so that they become finished code which help us for future implementations. You are welcome!

using System;
using System.Drawing;
using System.Runtime.InteropServices;

internal sealed class Win32
{
public delegate int WndProc(IntPtr hwnd, uint msg, uint
wParam, int lParam);

[DllImport("coredll.dll")]
public extern static int DefWindowProc(IntPtr hwnd,
uint msg, uint wParam, int lParam);

[DllImport("coredll.dll")]
public extern static IntPtr SetWindowLong(IntPtr hwnd, int nIndex,
IntPtr dwNewLong);

[DllImport("coredll.dll")]
public extern static int CallWindowProc(IntPtr lpPrevWndFunc,
IntPtr hwnd, uint msg, uint wParam, int lParam);

public const int GWL_WNDPROC = -4;
public const uint WM_PAINT = 0x000F;
public const uint WM_ERASEBKGND = 0x0014;
public const uint WM_KEYDOWN = 0x0100;
public const uint WM_KEYUP = 0x0101;
public const uint WM_MOUSEMOVE = 0x0200;
public const uint WM_LBUTTONDOWN = 0x0201;
public const uint WM_LBUTTONUP = 0x0202;
public const uint WM_LBUTTONDBLCLK = 0x0203;
public const uint WM_NOTIFY = 0x4E;
}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


internal class WndProcHooker
{
public delegate int WndProcCallback(IntPtr hwnd, uint msg,
uint wParam, int lParam, ref bool handled);

private static Dictionary<IntPtr, HookedProcInformation> hwndDict = new Dictionary<IntPtr, HookedProcInformation>();

private static Dictionary<Control, HookedProcInformation> ctlDict = new Dictionary<Control, HookedProcInformation>();

public static void HookWndProc(Control ctl,
WndProcCallback callback, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
hpi = new HookedProcInformation(ctl,
new Win32.WndProc(WndProcHooker.WindowProc));
ctl.HandleCreated += new EventHandler(ctl_HandleCreated);
ctl.HandleDestroyed += new EventHandler(ctl_HandleDestroyed);
ctl.Disposed += new EventHandler(ctl_Disposed);

if (ctl.Handle != IntPtr.Zero)
{
hpi.SetHook();
}
}

if (ctl.Handle == IntPtr.Zero)
{
ctlDict[ctl] = hpi;
}
else
{
hwndDict[ctl.Handle] = hpi;
}

hpi.messageMap[msg] = callback;
}

static void ctl_Disposed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
ctlDict.Remove(ctl);
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}

static void ctl_HandleDestroyed(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (hwndDict.ContainsKey(ctl.Handle))
{
HookedProcInformation hpi = hwndDict[ctl.Handle];
UnhookWndProc(ctl, false);
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}

static void ctl_HandleCreated(object sender, EventArgs e)
{
Control ctl = sender as Control;
if (ctlDict.ContainsKey(ctl))
{
HookedProcInformation hpi = ctlDict[ctl];
hwndDict[ctl.Handle] = hpi;
ctlDict.Remove(ctl);
hpi.SetHook();
}
else
{
System.Diagnostics.Debug.Assert(false);
}
}

private static int WindowProc(IntPtr hwnd, uint msg,
uint wParam, int lParam)
{
if (hwndDict.ContainsKey(hwnd))
{
HookedProcInformation hpi = hwndDict[hwnd];
if (hpi.messageMap.ContainsKey(msg))
{
WndProcCallback callback = hpi.messageMap[msg];
bool handled = false;
int retval = callback(hwnd, msg, wParam, lParam, ref handled);
if (handled)
return retval;
}

return hpi.CallOldWindowProc(hwnd, msg, wParam, lParam);
}

System.Diagnostics.Debug.Assert(false,
"WindowProc called for hwnd we don't know about");

return Win32.DefWindowProc(hwnd, msg, wParam, lParam);
}

public static void UnhookWndProc(Control ctl, uint msg)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}

if (hpi == null)
{
throw new ArgumentException("No hook exists for this control");
}

if (hpi.messageMap.ContainsKey(msg))
{
hpi.messageMap.Remove(msg);
}
else
{
throw new ArgumentException(String.Format(
"No hook exists for message ({0}) on this control",
msg));
}
}

public static void UnhookWndProc(Control ctl, bool disposing)
{
HookedProcInformation hpi = null;
if (ctlDict.ContainsKey(ctl))
{
hpi = ctlDict[ctl];
}
else if (hwndDict.ContainsKey(ctl.Handle))
{
hpi = hwndDict[ctl.Handle];
}
if (hpi == null)
{
throw new ArgumentException("No hook exists for this control");
}

if (ctlDict.ContainsKey(ctl) && disposing)
{
ctlDict.Remove(ctl);
}

if (hwndDict.ContainsKey(ctl.Handle))
{
hpi.Unhook();
hwndDict.Remove(ctl.Handle);
if (!disposing)
{
ctlDict[ctl] = hpi;
}
}
}

class HookedProcInformation
{
public Dictionary<uint, WndProcCallback> messageMap;
private IntPtr oldWndProc;

private Win32.WndProc newWndProc;

private Control control;
public HookedProcInformation(Control ctl, Win32.WndProc wndproc)
{
control = ctl;
newWndProc = wndproc;
messageMap = new Dictionary<uint, WndProcCallback>();
}

public void SetHook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
{
throw new InvalidOperationException(
"Handle for control has not been created");
}

oldWndProc = Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));
}

public void Unhook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
{
throw new InvalidOperationException(
"Handle for control has not been created");
}
Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC, oldWndProc);
}

public int CallOldWindowProc(IntPtr hwnd,
uint msg, uint wParam, int lParam)
{
return Win32.CallWindowProc(oldWndProc, hwnd, msg,
wParam, lParam);
}
}
}


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace karlovo
{
public partial class TextBoxCustom : TextBox
{
new public event EventHandler DoubleClick;
public TextBoxCustom()
: base()
{
InitializeComponent();
WndProcHooker.HookWndProc(this, new WndProcHooker.WndProcCallback(WindowMessageHandler), Win32.WM_LBUTTONDBLCLK);
}
private int WindowMessageHandler(IntPtr hwnd, uint msg, uint wParam,int lParam, ref bool handled)
{
OnMouseDoubleClick(EventArgs.Empty);
handled = true;
return 0;
}
protected void OnMouseDoubleClick(EventArgs e)
{
if (DoubleClick != null)
{
DoubleClick(this, e);
}
}
}
}

So,all u need to do is to put this code in your custom control and implements the event. Thanks!!!
AnswerRe: Windows Mobile Compact Edition 2.0 TextBox problem missing MouseClick event Pin
ginpq31-Mar-11 22:39
ginpq31-Mar-11 22:39 
QuestionMultiple layout usercontrol Pin
john3426-Mar-10 1:07
john3426-Mar-10 1:07 
AnswerRe: Multiple layout usercontrol Pin
TheFoZ26-Mar-10 3:57
TheFoZ26-Mar-10 3:57 
QuestionInstall in single click with prerequisites Pin
anishkannan26-Mar-10 0:33
anishkannan26-Mar-10 0:33 
AnswerRe: Install in single click with prerequisites Pin
The Cake of Deceit26-Mar-10 4:34
The Cake of Deceit26-Mar-10 4:34 
QuestionMoving maximized window Pin
mvermand25-Mar-10 23:15
mvermand25-Mar-10 23:15 
AnswerRe: Moving maximized window Pin
Eddy Vluggen25-Mar-10 23:37
professionalEddy Vluggen25-Mar-10 23:37 
GeneralRe: Moving maximized window Pin
mvermand25-Mar-10 23:39
mvermand25-Mar-10 23:39 
GeneralRe: Moving maximized window Pin
Eddy Vluggen26-Mar-10 0:07
professionalEddy Vluggen26-Mar-10 0:07 
QuestionGeting the name of the form a user control is on Pin
chrisclarke1125-Mar-10 23:14
chrisclarke1125-Mar-10 23:14 
AnswerRe: Geting the name of the form a user control is on Pin
Tony Richards26-Mar-10 0:40
Tony Richards26-Mar-10 0:40 
GeneralRe: Geting the name of the form a user control is on Pin
chrisclarke1126-Mar-10 1:14
chrisclarke1126-Mar-10 1:14 
GeneralRe: Geting the name of the form a user control is on Pin
TheFoZ26-Mar-10 1:28
TheFoZ26-Mar-10 1:28 
GeneralRe: Geting the name of the form a user control is on Pin
chrisclarke1126-Mar-10 1:42
chrisclarke1126-Mar-10 1:42 
GeneralRe: Geting the name of the form a user control is on Pin
TheFoZ26-Mar-10 3:42
TheFoZ26-Mar-10 3:42 
GeneralRe: Geting the name of the form a user control is on Pin
chrisclarke1129-Mar-10 0:00
chrisclarke1129-Mar-10 0:00 
Questiongrid inside grid in windows application Pin
sagarrana25-Mar-10 21:43
sagarrana25-Mar-10 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.