I don't understand the wndProc, PreProcessMessage and how to use it's inparameter message
-
Hi I have trouble understanding the code that is on this page http://www.sanity-free.org/13/numeric\_text\_box\_number\_only\_text\_box\_control\_in\_csharp.html. In PreProcessMessage I don't understand what the inparameter Message msg property WParam holds or Msg holds. When to use them and for what? I have also heard about the property LParam and HWnd, when should I use them. In WndProc we have the code m.Result = (IntPtr)0. What holds the inparameter Message m's property Result and what is IntPtr? I have seen codes like
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_GETTEXT:
string s = ModifyOriginalText(this.Text);
int charsToCopy = Math.Min(m.WParam.ToInt32(),s.Length);
// return the string
m.LParam = Marshal.StringToHGlobalAuto(s.Substring(0,
charsToCopy));
// result is the number of characters copied
m.Result = new IntPtr(charsToCopy);
return;
}
base.WndProc(ref m);
}Here they are using the property LParam and here they make a new IntPtr with another amount on int. I need an accurate explanation when to use Msg, LParam, WParam, Result and for what. And when we are calling base.WndProc (ref m); we want something to happen or what? I mean in the NumericBox example, when it finds not a number it just returns, but why does it have to add (IntPtr)0 to m.Result before. Could they instead have written new IntPtr(0)? What does the amount to IntPtr tell us? I know there are many questions, but I so much want to learn this and understand it thoroughly. Hope you can help me and explain accurate. Many thanks Fia
-
Hi I have trouble understanding the code that is on this page http://www.sanity-free.org/13/numeric\_text\_box\_number\_only\_text\_box\_control\_in\_csharp.html. In PreProcessMessage I don't understand what the inparameter Message msg property WParam holds or Msg holds. When to use them and for what? I have also heard about the property LParam and HWnd, when should I use them. In WndProc we have the code m.Result = (IntPtr)0. What holds the inparameter Message m's property Result and what is IntPtr? I have seen codes like
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_GETTEXT:
string s = ModifyOriginalText(this.Text);
int charsToCopy = Math.Min(m.WParam.ToInt32(),s.Length);
// return the string
m.LParam = Marshal.StringToHGlobalAuto(s.Substring(0,
charsToCopy));
// result is the number of characters copied
m.Result = new IntPtr(charsToCopy);
return;
}
base.WndProc(ref m);
}Here they are using the property LParam and here they make a new IntPtr with another amount on int. I need an accurate explanation when to use Msg, LParam, WParam, Result and for what. And when we are calling base.WndProc (ref m); we want something to happen or what? I mean in the NumericBox example, when it finds not a number it just returns, but why does it have to add (IntPtr)0 to m.Result before. Could they instead have written new IntPtr(0)? What does the amount to IntPtr tell us? I know there are many questions, but I so much want to learn this and understand it thoroughly. Hope you can help me and explain accurate. Many thanks Fia
The author is using the this to override the default Windows message handling process which is what drives a Windows program. In this case he checks to see if the message is WM_GETTEXT and if so returns his modified text to the calling process. This is not generally required in .NET applications but if you are really interested you can take a look here[^] on MSDN.
Unrequited desire is character building. OriginalGriff
-
The author is using the this to override the default Windows message handling process which is what drives a Windows program. In this case he checks to see if the message is WM_GETTEXT and if so returns his modified text to the calling process. This is not generally required in .NET applications but if you are really interested you can take a look here[^] on MSDN.
Unrequited desire is character building. OriginalGriff
Hi Thanks for the answer. But it doesn't tell me what the Message's properties Msg, LParam, WParam and Result are for. When to use them and what to do with them. And the question what to assign to m.Result, how do I know what to assign? I have seen so many examples when I have googled, but no explanation. I have seen m.Result = new IntPtr(charsToCopy)and m.Result= (IntPtr)0 or new IntPtr(0), but how do I know what integer to assign m.Result what does it differ. Many thanks Fia
-
Hi Thanks for the answer. But it doesn't tell me what the Message's properties Msg, LParam, WParam and Result are for. When to use them and what to do with them. And the question what to assign to m.Result, how do I know what to assign? I have seen so many examples when I have googled, but no explanation. I have seen m.Result = new IntPtr(charsToCopy)and m.Result= (IntPtr)0 or new IntPtr(0), but how do I know what integer to assign m.Result what does it differ. Many thanks Fia
-
The values of
WPARAM
,LPARAM
and the return are all dependent on the message being processed. For example here[^] are the details for theWM_GETTEXT
message.Unrequited desire is character building. OriginalGriff
Hi Thanks again for your answers, but I have till trouble to understand to use m.Result=IntPtr(0) or what to put in IntPtr. I have two examples I have googled: In this example it stops users to paste anything to a textbox.
protected override void WndProc(ref Message m)
{if (m.Msg != WM\_PASTE ) { base.WndProc(ref m); } }
In this example users can just paste in numbers in a textbox.
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE )
{
IDataObject obj = Clipboard.GetDataObject();
string input = (string)obj.GetData(typeof(string));
foreach (char c in input)
{
if (!char.IsDigit(c))
{
m.Result = (IntPtr)0;
return;
}
}
}
base.WndProc(ref m);
}Why do we have to assign to m.Result the InPtr 0? What is IntPtr and how do I know which int (amount) I should return? Many thanks Fia
-
Hi Thanks again for your answers, but I have till trouble to understand to use m.Result=IntPtr(0) or what to put in IntPtr. I have two examples I have googled: In this example it stops users to paste anything to a textbox.
protected override void WndProc(ref Message m)
{if (m.Msg != WM\_PASTE ) { base.WndProc(ref m); } }
In this example users can just paste in numbers in a textbox.
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE )
{
IDataObject obj = Clipboard.GetDataObject();
string input = (string)obj.GetData(typeof(string));
foreach (char c in input)
{
if (!char.IsDigit(c))
{
m.Result = (IntPtr)0;
return;
}
}
}
base.WndProc(ref m);
}Why do we have to assign to m.Result the InPtr 0? What is IntPtr and how do I know which int (amount) I should return? Many thanks Fia
As I explained in my previous answer you need to look at the documentation for each message to know what values are represented by the
WPARAM
andLPARAM
input parameters, and what values may be returned if you process the message. For example the documentation for WM_PASTE is here[^]. You also need to read about the defaultWndProc()
method in .NET here[^]. The use ofIntPtr
in C# is merely a type that corresponds to the 32 bit DWORD in C++, which is the normal return value from these functions.Unrequited desire is character building. OriginalGriff
-
Hi I have trouble understanding the code that is on this page http://www.sanity-free.org/13/numeric\_text\_box\_number\_only\_text\_box\_control\_in\_csharp.html. In PreProcessMessage I don't understand what the inparameter Message msg property WParam holds or Msg holds. When to use them and for what? I have also heard about the property LParam and HWnd, when should I use them. In WndProc we have the code m.Result = (IntPtr)0. What holds the inparameter Message m's property Result and what is IntPtr? I have seen codes like
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch (m.Msg)
{
case WM_GETTEXT:
string s = ModifyOriginalText(this.Text);
int charsToCopy = Math.Min(m.WParam.ToInt32(),s.Length);
// return the string
m.LParam = Marshal.StringToHGlobalAuto(s.Substring(0,
charsToCopy));
// result is the number of characters copied
m.Result = new IntPtr(charsToCopy);
return;
}
base.WndProc(ref m);
}Here they are using the property LParam and here they make a new IntPtr with another amount on int. I need an accurate explanation when to use Msg, LParam, WParam, Result and for what. And when we are calling base.WndProc (ref m); we want something to happen or what? I mean in the NumericBox example, when it finds not a number it just returns, but why does it have to add (IntPtr)0 to m.Result before. Could they instead have written new IntPtr(0)? What does the amount to IntPtr tell us? I know there are many questions, but I so much want to learn this and understand it thoroughly. Hope you can help me and explain accurate. Many thanks Fia
If your goal here is to understand P/Invoke and 'WndProc,' and the use of parameters to P/Invokes ... then great, but, if your goal is to make progress in .NET and WinForms or WPF, then there are other techniques you can use to create an input field that only accepts numeric input that do not require getting 'outside the .NET box.' Check out 'MaskedTextBox' and 'NumericUpDown' in WinForms, for example. best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"