SendMessage issue
-
Maybe I wasnt clear enough in my last thread: I want to use the API function SendMessage in my .NET CF apllication. I use the following code to get the handle of my TextBox window: [DllImport("coredll.dll")] internal extern static IntPtr GetCapture(); this.Capture = true; hwnd = GetCapture(); this.Capture = false; I want to send messages in order to extend the functionality of my TextBox. I use: [DllImport("coredll.dll")] internal extern static int SendMessage(IntPtr Hwnd, int Msg, int WParam, int LParam); SendMessage(this.hwnd, (int)0x0300/*WM_CUT*/, 0, 0); This does not cut the text in my TextBox. I have read all through the .NET CF and could not find the reason for this. Any other p/invoke I use works fine (like GetCapture) but the SendMessage doesnt. Please help... Thanks. avivhal
-
Maybe I wasnt clear enough in my last thread: I want to use the API function SendMessage in my .NET CF apllication. I use the following code to get the handle of my TextBox window: [DllImport("coredll.dll")] internal extern static IntPtr GetCapture(); this.Capture = true; hwnd = GetCapture(); this.Capture = false; I want to send messages in order to extend the functionality of my TextBox. I use: [DllImport("coredll.dll")] internal extern static int SendMessage(IntPtr Hwnd, int Msg, int WParam, int LParam); SendMessage(this.hwnd, (int)0x0300/*WM_CUT*/, 0, 0); This does not cut the text in my TextBox. I have read all through the .NET CF and could not find the reason for this. Any other p/invoke I use works fine (like GetCapture) but the SendMessage doesnt. Please help... Thanks. avivhal
Seems kind of convoluted. Are you just trying to move some text from a textbox to the clipboard? Anyway, if you are doing this in an event handler that may be the problem; have you tried PostMessage() instead? Matt Gerrans
-
Seems kind of convoluted. Are you just trying to move some text from a textbox to the clipboard? Anyway, if you are doing this in an event handler that may be the problem; have you tried PostMessage() instead? Matt Gerrans
Thanks for the answer. I tried PostMessage as well. It does not work. I do not do it in an event handler of a menu. My goal is to create a more robust editor then TextBox(that’s all there is in the .net CF, no RichTextBox). Therefore I build an extended TextBox with cut,copy,paste and more. My other problem is that when I try to add text programmatically: int len = textBoxTerminal.Text.Length; textBoxTerminal.SelectionStart = len; textBoxTerminal.SelectionLength = len; textBoxTerminal.SelectedText = (string)StringToAdd; the caret always positions itself in the beginning of the TextBox's Text. I can reposition it to the end and scroll, but this causes the TextBox to flicker. I thought PostMessage will help solving this, but I cannot test it for the Since PostMesage does not work for me. Hope you have an answer for this... Thanks. avivhal
-
Maybe I wasnt clear enough in my last thread: I want to use the API function SendMessage in my .NET CF apllication. I use the following code to get the handle of my TextBox window: [DllImport("coredll.dll")] internal extern static IntPtr GetCapture(); this.Capture = true; hwnd = GetCapture(); this.Capture = false; I want to send messages in order to extend the functionality of my TextBox. I use: [DllImport("coredll.dll")] internal extern static int SendMessage(IntPtr Hwnd, int Msg, int WParam, int LParam); SendMessage(this.hwnd, (int)0x0300/*WM_CUT*/, 0, 0); This does not cut the text in my TextBox. I have read all through the .NET CF and could not find the reason for this. Any other p/invoke I use works fine (like GetCapture) but the SendMessage doesnt. Please help... Thanks. avivhal
What is the return code for
SendMessage
? Check this against the errors in winerror.h. You should also set theDllImportAttribute.CharSet
field toCharSet.Auto
. There is actually noSendMessage
function, butSendMessageA
(ANSI) andSendMessageW
(Wide-char, or Unicode). The Windows .NET CE platform is actually Unicode, so you could P/InvokeSendMessageW
and setDllImportAttribute.EntryPoint
to"SendMessageW"
(sinceDllImportAttribute.ExactSpelling
is not supported for the .NET CF). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
What is the return code for
SendMessage
? Check this against the errors in winerror.h. You should also set theDllImportAttribute.CharSet
field toCharSet.Auto
. There is actually noSendMessage
function, butSendMessageA
(ANSI) andSendMessageW
(Wide-char, or Unicode). The Windows .NET CE platform is actually Unicode, so you could P/InvokeSendMessageW
and setDllImportAttribute.EntryPoint
to"SendMessageW"
(sinceDllImportAttribute.ExactSpelling
is not supported for the .NET CF). This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]Neither DllImportAttribute.CharSet nor SendMessageW worked. I did get it to work though (somehow)... I took the declarations I have of SendMessage(I have a few overloads) : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); and wrapped them in a separate class. Then I called them as this class's static members and it works! I do not understand why would this solve the problem though...? Maybe the problem was that they were members of a TextBox (Form) derived class and this caused congestion problems with the Form’s own messages? I wish I had the OS source code so I could check this;) avivhal
-
Neither DllImportAttribute.CharSet nor SendMessageW worked. I did get it to work though (somehow)... I took the declarations I have of SendMessage(I have a few overloads) : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); and wrapped them in a separate class. Then I called them as this class's static members and it works! I do not understand why would this solve the problem though...? Maybe the problem was that they were members of a TextBox (Form) derived class and this caused congestion problems with the Form’s own messages? I wish I had the OS source code so I could check this;) avivhal
How were you doing it before? C# does not support functions - only methods. They would've had to have been members of a class before or else you couldn't have even compiled your source file. BTW - those last two parameters are process-dependent types and should be
IntPtr
. If you're needing to pass pointers to structs or char arrays (strings), you can define overloads that would amount to a pointer, such as simplystring
orref MyStruct
.int
is anInt32
in C# and will always be 32 bits. If you ever hope to port this - whether the source or binary - you'll need to change it anyway. External methods must always be static, too, but you declared them statically before, did you not? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog] -
How were you doing it before? C# does not support functions - only methods. They would've had to have been members of a class before or else you couldn't have even compiled your source file. BTW - those last two parameters are process-dependent types and should be
IntPtr
. If you're needing to pass pointers to structs or char arrays (strings), you can define overloads that would amount to a pointer, such as simplystring
orref MyStruct
.int
is anInt32
in C# and will always be 32 bits. If you ever hope to port this - whether the source or binary - you'll need to change it anyway. External methods must always be static, too, but you declared them statically before, did you not? This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]They were static members of the my TextBoxEx class before. All I did was build a new class and pass the methods to be static members of the new class. then call them as the new class methods. e.g. if my old class was Aclass and the new Bclass then in Aclass I call: Bclass.SendMessage(...); I have a few overloads such as [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, String lParam); [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, byte[] lParam); that take addresses (pointers). I only use : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); to call with 0 lparam and wparam thus: Bclass.SendMessage(this.Handle, (int)ClipboardMessage.WM_UNDO, 0, 0); I probably should use 2 pointers (IntPtr) instead and lock the IntPtr for the GC before calling this method? Thanks, Aviv. avivhal
-
They were static members of the my TextBoxEx class before. All I did was build a new class and pass the methods to be static members of the new class. then call them as the new class methods. e.g. if my old class was Aclass and the new Bclass then in Aclass I call: Bclass.SendMessage(...); I have a few overloads such as [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, String lParam); [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, byte[] lParam); that take addresses (pointers). I only use : [DllImport("coredll")] public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam); to call with 0 lparam and wparam thus: Bclass.SendMessage(this.Handle, (int)ClipboardMessage.WM_UNDO, 0, 0); I probably should use 2 pointers (IntPtr) instead and lock the IntPtr for the GC before calling this method? Thanks, Aviv. avivhal
On what class they're defined shouldn't matter, but perhaps some of your overloads were conflicting. With value types - like
int
- you do not need to pin them (see theGCHandle
class, BTW). Reference types may be moved by the GC on the heap, but it depends how the message handler uses the data; you may not need to pin the memory. Strings are reference types but this data is typically used synchronously withSendMessage
(PostMessage
, on the other hand, is a different story). When passing addresses of reference objects (asIntPtr
, or just aGCHandle
which works, too, as well as aHandleRef
) you should pin them. For robust code, never use fixed-size integers for theWPARAM
andLPARAM
. Always useIntPtr
. For 0, just useIntPtr.Zero
and to wrap anint
orlong
usenew IntPtr(int)
ornew IntPtr(long)
, respectively. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]