|
I over came this problem by using the ASCII value for the return key (13). Here's how it works in code:
<br />
private void YouCallBackMethod(object sender, System.Windows.Forms.KeyPressEventArgs e)<br />
{<br />
if (e.KeyChar == (char)13)<br />
{<br />
}<br />
else<br />
{<br />
}<br />
}<br />
Hope this helped...
|
|
|
|
|
Peter Vertes wrote:
if (e.KeyChar == (char)13)
You could also do the following:
if(e.KeyChar == Keys.Return)
- Nick Parker My Blog | My Articles
|
|
|
|
|
Thanks guys for the reply.
But my problem is a little deeper.
I can't hit WndProc or OnKeyPressed method at all when I press Return.
For other keys I can hit methods. Of course everething works fine if I put .Net control on c# Windows.Form. But if I reside .Net control on C++ CView form something goes wrong. System messaging works differently in these two cases. In Spy++ I can see that C# RichTextbox got message WM_KEYDOWN for VK_RETURN and then WM_KEYUP.
Normally WM_CHAR should follow WM_KEYDOWN which I can see when work in C# environment. Looks like ::TranslateMessage(msg) failed.
Roman
|
|
|
|
|
Is it a CFormView ? CFormView calls IsDialogMessage , which assumes that you're trying to press the default button on the form, or raises WM_COMMAND for IDOK if there is no default.
IsDialogMessage first probes the control with focus to see if it wants to handle this character. It does this by sending the control the WM_GETDLGCODE message. IIRC, the control has to return a code including the flag DLGC_WANTALLKEYS or DLGC_WANTMESSAGE in order to swallow the RETURN keypress. You'll need to experiment a little.
In your C# code, you'll probably have to override WndProc . The reason it works on a C# form is that the C# form manager doesn't use the pre-existing Windows dialog manager code - it's a new implementation.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Thanks Mike for attention to the problem.
Actually I managed to catch pressing Enter key in WindowProc overriden method in CFormView with WM_COMMAND message for IDOK. Then I simplify my testing project and put C# control on CDialog. Same thing. On C# side I cannot catch Enter in overriden WndProc. No messages at all when pressing Enter. Probably message could not reach C# control on its way. So I need to do all necessary message calls on C++ side in WindowPoc. I tried to call TranslateMessage(msg) with hwnd of my RichTextBox to get WM_CHAR in textbox. This call was not successful and returned 0. When I did the same call against CEdit control it worked and message was translated. Now the question for me is : which kind of message should I send to RichTextBox ? Maybe it is IsDialogMessage(RichTextBox_HWND, WM_KEYDOWN_MSG) if I understood your comment correctly. I hope RichTextBox will swallow Return keypress. But I need to know hwnd of RichTextBox. For testing app it was not difficult. But in real app will not be so easy to get it on surface. Any idea how to avoid knowing RichTextBox hwnd ?
Thanks a lot
Roman
|
|
|
|
|
Finally solution was found
I had to override WndProc in the derived class of RichEditBox,
catch pressing Enter key and do my job.
protected override void WndProc(ref Message m)
{
int WM_GETDLGCODE = 0x0087;
int VK_RETURN = 0x0D;
if (m.Msg == WM_GETDLGCODE)
{
if (m.WParam.ToInt32() == VK_RETURN)
{
base.SelectedText = "\n";
return ;
}
}
base.WndProc (ref m);
}
|
|
|
|
|
aloa
okay buddies =D my idea is to develop an ip-blocker. the user defines a set of ips that are blocked from the system.
my question is now: is this in c# actually possible or do i have to switch to c++?
regards
|
|
|
|
|
As discussed before, .NET is too high level for such a task at this time. There is a lot of native APIs and interfaces to both implement and consume. You should download and read the networking sections of the Microsoft Platform SDK[^]. There's a lot to understand. A firewall is a very low-level application and requires that you understand how the Microsoft networking protocols work together and with the network interface cards (NICs).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
please introduce me a way to notify user that a process
request a socket in c#
koosha
|
|
|
|
|
.NET is too high-level for this sort of operation. This requires very low-level APIs and implementations with the Microsoft networking protocols, which also requires extremely efficient code with a small footprint - another reason not to use .NET.
There are already several commercial (typically free for home use) applications that do this already.
This has been discussed in the forums before. If you want to see how that thread progressed (to the same conclusion), please click "Search comments" directly above the message board.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
is there any messsage to notify user that a process
request a socket in c# or by win apis
koosha
|
|
|
|
|
Hi~
Lei wrote
"I have 2 forms, form1 contains a ListView. What I want to do is, when
clicking an item in the ListView, hide form1 and show form2. Code for
ListView's SelectedIndexChanged: { form2.Show(); this.Hide(); }. When I
click the item, form2 shows with form1's title and menu. I noticed that when mouse down, the SelectedIndexChanged event was triggered and bring form2 to front. But when mouse up, system redrawed the selected item and bring title and menu back to form1's. I tried to handle MouseDown and MouseUp event of ListView but they were never triggered. Now I used a tricky way to solve the problem: start a thread and use Control.Invoke to switch forms. It is not good, I think there must be a better solution. Thanks."
I am facing the same problem. Someone solve it with setting timer. However, is there any other way to solve it?
Thanks~
|
|
|
|
|
Don't use the SelectedIndexChanged event. It won't tell you if the user clicks on the currently selected item, either. Also, if the user is trying to use the direction pad or scroll wheel, the event will fire as they're scrolling between items; you probably want it to fire only when they press the Action button.
What you should do is set the Activation property to ItemActivation.OneClick, and handle the ItemActivate event. This also gives you keyboard/keypad interaction for free.
Don't you just love the complete, comprehensive nature of the documentation? In response to your post, I tried this out on both .NET CF 1.0 SP3 and on 2.0 Beta 1. At least 2.0 Beta 1 gives you a compiler warning. I've raised the issue on the Compact Framework newsgroup, and I hope that will encourage clearer documentation.
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
It works!
Thank you very much!
|
|
|
|
|
How about treeview?
treeview got that same problem
there is no activation event handler in compact framework
|
|
|
|
|
Hi gurus,
I have 2 questions:
1) How can I attach a hwnd to a graphics?
When I do Graphics gr=new Graphics(m.HWnd) , I have the message "System.Drawing.Graphics.Graphics(System.IntPtr) is inaccessible due to its protection level"
2) What is the equivalent of the GetSystemMetrics() API in C#?
Best regards.
Thanks.
There is no spoon.
|
|
|
|
|
Graphics gr = Graphics.FromHwnd( m.HWnd ) If the HWnd is coming from a Windows Forms control, use ctl.CreateGraphics() instead.
As for GetSystemMetrics, that depends on what values you want. The simplest thing to do is often to use P/Invoke.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int GetSystemMetrics( int metric );
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Hi,
For the graphics, I need to draw a button in the Non client area. I can get the handle of the non client area. I want to draw a button using the ControlPaint.DrawButton method wich need a Graphics as input.
ok for the dll import
There is no spoon.
|
|
|
|
|
When you receive the WM_NCPAINT notification message I mentioned to you before, the Message.WParam contains the HRGN (handle to a region) to be painted. You P/Invoke GetDCEx passing your window handle (HWND - the Handle property on all Control s), the HRGN mentioned above, and DCX_WINDOW|DCX_INTERSECTRGN ( =0x81 ). This is covered in the Platform SDK for the WM_NCPAINT message. IntelliSense will not help. You must read about the notification messages and APIs when working with this type of stuff.
The WM_NCPAINT documentation in the Platform SDK even includes a sample of calling GetDCEx .
The return value from GetDCEx is the HDC (handle to a device context) from which you create a Graphics object. Use the static Graphics.FromHdc method to get a Graphics instance for the non-client region.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath,
I have done what you said, but there is nothing. Maybe I have wrongly declared the things...
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, long flags);
in the WndProc method, I trap the WN_NCPAINT
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
ControlPaint.DrawButton(gr, 50, 1, 5, 5, ButtonState.Normal);
nothing happens. it should draw a button 5x5 at pos (50, 1) in the title bar, but I see nothing. Maybe I don't use it well...
There is no spoon.
|
|
|
|
|
A DWORD translates to an int (or uint ), not a long . The value of DCX_WINDOW|DCX_INTERSECTRGN wouldn't, therefore, by read from the execution stack when calling GetDCEx - only 0 would be read and that wouldn't produce the results you want.
Also, don't bunch your method calls together so much. Try debugging that and you'll see that you can't check return values so it will be hard to know why a particular method failed.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, I have changed the long to an int
but I still have the error "An unhandled exception of type 'System.OutOfMemoryException' occurred in system.drawing.dll
Additional information: Out of memory."
when I try to do:
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
I have now declared the functions like this:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
and the constants:
private const int DCX_WINDOW=0x0001;
private const int DCX_INTERSECTRGN=0x0080;
while these constants are declared as long in the WinUser.h from the Platform SDK.
Also, Platform SDK declares the GetDCEx function like this:
HDC GetDCEx(
HWND hWnd,
HRGN hrgnClip,
DWORD flags
);
Maybe I'm wrong somewher in the C# translation...
There is no spoon.
|
|
|
|
|
A native long is 32 bits. A managed long is 64 bits (System.Int64 ). This is documented in the Platform SDK (and I know from many years of experience). If you want further proof, read Platform Invoke Data Types[^] in the .NET Framework SDK.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
ok for the length between Win32 and .NET.
I have written the declarations as you advised me, but I get an error when I execute the line
Graphics gr=Graphics.FromHdc(GetDCEx(m.HWnd, m.WParam, DCX_WINDOW|DCX_INTERSECTRGN));
There is no spoon.
|
|
|
|
|
It would help to know on which operation, which means - as I said before - you have to split them up.
Microsoft MVP, Visual C#
My Articles
|
|
|
|