|
HI, I should intercept messages of external application, can I use this method? I've tried to pass hwnd of windows calc application but it doesn't work. Should I use API SetWindowLongA?
Thanks in advance
|
|
|
|
|
Hi everyone!
I'm trying to subclassing.
Where can I find a list of the constants used for subclassing (like the folowing)?
Private Const WM_CLOSE As System.Int32 = &H10
Private Const WM_QUERYENDSESSION As System.Int32 = &H11
Private Const SC_CLOSE As System.Int32 = &HF060&
Private Const WM_SYSCOMMAND As System.Int32 = &H112
Private Const WM_KEYDOWN As System.Int32 = &H100
Private Const WM_DESTROY As System.Int32 = &H2
Private Const SC_MAXIMIZE As System.Int32 = &HF030
Private Const SC_MINIMIZE As System.Int32 = &HF020
Private Const SC_RESTORE As System.Int32 = &HF120
thank you
ALex
|
|
|
|
|
Hi! I'm a total beginner and I read (http://www.osix.net/modules/article/?id=653#comments) that you wouldn't be able to debug an application if you used subclassing. is this true? if so, how can the app be tested? many thanks!!
|
|
|
|
|
i want to subclass the vs.net menu control to drae them flat and then move on to higher complexcity, like making them look like office 2003 and other themes. what i want to know that can i do that using subclassing, as you have mentioed here?
if yes then can u give me an example how to do it? i also need that how can i find the messages that windows sends to menus, i mean the message constants and their descriptions?
by the way nice article and very much informative too. thanks a lot for sharing you knowledge with beginer like us. way to go man.
please reply and help me up. please...
|
|
|
|
|
What other measures should be taken to get this code run in a Windows Service?
Murat Eraydin
|
|
|
|
|
Can subclassing menus be done, i have managed it for a form, so my code works, now when i try subclass i custom menu, no messages are sent, is this possible??
|
|
|
|
|
Hi..anyone pls help me...
is there anyone know how to traps windows message constant for terminating my application from windows task manager?
i already know how the windows message for terminating my application.
It seems like queryunload (unloadmode) in vb6, but i have no idea how to implement it in vb.net..
could anyone pls help me how to detect that application is terminated from windows task manager.
anyway..thanks in advance..
regards,
safir
|
|
|
|
|
I just have one question? How do you Subclass the TextBox so that we can add some more options to the Context Menu of the TextBox? I am new to VB.NET.
Thank you.
Rai
|
|
|
|
|
Any idea on how to write a class that could be used to catch messages. For instance, I have a .net gui running in one thread, somehow I get the window handle for an edit control and or a button on the form. I use a tool to send a mouse click to the form and now I want to use my the window handle to create a control so that I can query it's state, like the new text, or foreground color or visibility or whatever. The main goal here is that I don't want to make the control that's on the form public and check the properties that way.
cliff
|
|
|
|
|
Has anyone run across code that will allow me to attach a System.Windows.Forms.Control class to an existing Window Handle? There is a static method call FromHandle that will return an instance of the Control class, but it doesn't attach to the window that I want. What I am attempting to do is create a 'cmd.exe' process through the Process class and control the window. Currently, I am reduced to controling the window via Win32 API calls, but I would prefer to use the .NET Framework methods to control the window if at all possible. I obtain the window handle from the created process via the MainWindowHandle property of the Process class.
Detlef Grohs
|
|
|
|
|
Hi Sameer,
How would go about subclassing all the controls on a form?
|
|
|
|
|
Hi,
I have used "sub-classing" to great lengths coming from VB6 land, but since converting to .NET (VB or C#) I have not used sub-classing in the sense mentioned here. Instead I have found that creating extender controls gives much more flexibility and easy of use for developers using your solution.
This is not to discourage anyone from using subclassing it had it's use but now there are better and more efficient ways to do the same thing.
Allan
|
|
|
|
|
Extender providers are hardly a replacement for subclassing. They add functionality, but they don't change behavior or address the issue of polymorphism. For an application, I have a tree with many subclassed tree nodes, even subclassing from other subclasses. Those tree nodes inherit functionality from their parent - sometimes even override it - but add functionality along the way. Extender providers can't do this. They actually add functionality to specific sets of classes at the expense of themselves (i.e., the providers do the work).
Reminiscent of my younger years...
10 LOAD "SCISSORS"
20 RUN
|
|
|
|
|
Agreed, 100%, but that is not what is being discussed here. The article as I understand it, is about "extending" the functionality of "all/most" controls on a form. Instead of subclassing all the different control objects, isn't easier to create an extender to provide the functionality?
As an example, you could handle the lost focus window message and display an error message and icon if the control is mandatory, or you could create an extender much like the error provider control in Visual Studio.
I have written many classes that use inheritance, (ie person, employee, manager, etc), I have also written new tree node classes that inherit from tree node to provide their own functionality, but I don't beleive this is subclassing in the true sense of the word.
Anyway, it great to see these type of articles, and NO-ONE, including myself, should hinder anyone from submitting more, as they are great in generating discussion, and debate.
Thanks for your input, appreciated.
Allan
|
|
|
|
|
i think tehre is a little mix up of terms here..
subclassing in the OO way means deriving a class and extending it.
subclassing in the VB6 sense means hooking on a listener in the windows message queue.
this article shows how to accomplish the later (vb6 kind of subclassing)
//Roger
|
|
|
|
|
Maybe I am confused. I do a lot of Control Development in VB.NET and I was super happy that Controls could be easily Inherited / SubClassed. The example you have given here (it seems to me) would be something equivalent to what would have been needed in VB6 because VB6 did not allow Inheritance.
In VB.NET, if I wanted to handle a TextBox that changed Case I would write a simple control that would Inherit from TextBox or TextBoxBase. Then I would simply "SubClass" or "Override" all the Messages to it that I wanted.
Public Class MyTextBox: Inherits System.Windows.Forms.TextBox
Private Overrides Sub WndProc(ByRef m as Message)
If (m.Msg = WM_xxx) Then
m.Result = New Intptr(1)
End If
MyBase.WndProc(m)
End Sub
End Class
That's all there is to it! VB.NET also gives more flexiblity by being able to override almost every Method or Property.
|
|
|
|
|
You are right. We really need this kind of thing in VB6. The example you provided was also good, but that is specific to one control only (TextBox in your case). The example which I presented is the generic one which can be used to subclass any control (control which is inherited by NativeWindow class). Anyway, there are a number of ways to do subclassing in .NET as you can see in the comments for this article.
Sameers
|
|
|
|
|
Nice approach. Though the proper subclassing of a control the hard way in .Net is to declare a delegate and then use the SetWindowLong (overloaded) and CallWindowProc APIs where the lParam of SetWindowLong would be typed as the delegate.
For example :
<br />
' Prototype delegate to handle callbacks from subclassed control<br />
Private Delegate Function SubclassCallback(ByVal hWnd As IntPtr, ByVal Msg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr<br />
<br />
' Subclasses a control with a callback<br />
<DllImport("User32", EntryPoint:="SetWindowLongA", CharSet:=CharSet.Unicode)> _<br />
Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Long, ByVal dwNewLong As SubclassCallback) As IntPtr<br />
<br />
' Unsubclasses a control<br />
<DllImport("User32", EntryPoint:="SetWindowLongA", CharSet:=CharSet.Unicode)> _<br />
Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Long, ByVal dwNewLong As IntPtr) As IntPtr<br />
<br />
' Forwards a message to a subclassed control<br />
<DllImport("User32", EntryPoint:="CallWindowProcA", CharSet:=CharSet.Unicode)> _<br />
Private Shared Function CallWindowProc(ByVal lpPrevWndFunc As IntPtr, ByVal hWnd As IntPtr, ByVal Msg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr<br />
<br />
Private GWL_WNDPROC As IntPtr = New IntPtr((-4))<br />
Private prevHandle As IntPtr<br />
Private myCallback As New SubclassCallback(AddressOf Me.SubclassCallbackHandler)<br />
<br />
' The control could be subclassed anywhere really. Depends on what messages you want to intercept.<br />
Private Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load<br />
<br />
' Subclass control by setting the new window proc but also remember was it was originally<br />
Me.prevHandle = SetWindowLong(Me.Handle, GWL_WNDPROC, Me.myCallback)<br />
<br />
End Sub<br />
<br />
Private Form_Closing(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Closing<br />
<br />
' Unsubclass control (use overloaded version)<br />
SetWindowLong(Me.Handle, GWL_WNDPROC, Me.prevHandle)<br />
<br />
End Sub<br />
<br />
' Handles all the messages sent to original handle<br />
Private Function SubclassCallbackHandler(ByVal hWnd As IntPtr, ByVal Msg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr<br />
<br />
' Process whatever messages you want here<br />
<br />
' NOTE : THIS IS VERY IMPORTANT ! FORWARD MESSAGE TO ORIGINAL HANDLE<br />
Return CallWindowProc(Me.prevHandle, hWnd, Msg, wParam, lParam)<br />
<br />
End Sub<br />
Now with that said it's a lot of work. .Net actually makes this a hundred times easier. The reason for me sharing this is took me quite a while to figure out. You can easily subclass a control if you inherit from it and override the following :
<br />
Protected Overrides Sub OnHandleCreated(ByVal e As EventArgs)<br />
<br />
' Invoke base class implementation<br />
MyBase.OnHandleCreated(e)<br />
<br />
' Allow message notifications<br />
Me.SetStyle(ControlStyles.EnableNotifyMessage, True)<br />
<br />
End Sub<br />
<br />
Protected Overrides Sub OnNotifyMessage(ByVal e As Message)<br />
<br />
' Process whatever messages you want here<br />
<br />
' No need to call base class implementation because the documentation says that there isn't any.<br />
<br />
End Sub<br />
THAT'S IT! Ridiculous I know. Too bad there is not more documentation on this. (At least not that I've found) Hope someone finds this useful.
|
|
|
|
|
That's a nice one too.
Well, the next I am looking for a way to Set the Hook to whole application like SetWindowsHookEx in VB 6. I didn't tried this API in .NET but I am planning.
Think for the time when you are developing some application (say of DB) which requires user input of text (many fields on one form). U want to facilitate the user to enter the text in any case (lower or upper) and when the focus losts from the TextBox, then it should automatically convert that to Title case (or any other format). Now, one thing is that you code in each textbox's lostfocus event. But the textboxes may be more than hundereds in your application, then the best way is to subclass your application using a global hook (application wide). In that case you have to write a little code which will do your job. What you think
Sameers
|
|
|
|
|
For your specific Textbox example, why wouldn't you just do the following?
Private Sub AllMyTextBoxes_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles TextBox1.Leave, TextBox2.Leave, TextBox3.Leave, etc.
Dim textbox as TextBox = DirectCast(sender, TextBox)
' Modify the textbox.text value accordingly.
End Sub
Since .NET added the necessary event that was missing from VB6, why not take advantage of it instead of resorting to subclassing techniques? You would still have to check the type of control that has caused the windows message to occur, so to me, this way seems much cleaner.
Also, since it is so simple to create custom controls in .NET, why not create a new control inherited from a TextBox that automatically does this task anyways... this way you would just use the proper control and the code would be self-contained. I've officially written more custom controls in .NET than in any previous language (VB5/6, VC++, Delphi).
Just wanted to give a couple of thoughts. There are times when subclassing is necessary, but I think that subclassing should only be used when absolutely needed because of no other way natively in .NET to accomplish the task... even if it is easier to do in .NET; use it where its appropriate.
|
|
|
|
|
Ok, why don't you just create a really simple control which Inhertis from "TextBox" and extend the Class to have Properties like "TextCasing", "BackColorActive", "BackColorDisabled", etc...
|
|
|
|
|
It is quite good idea and I will implement that. But the problem is that I have almost completed my project. And at this time, I can not replace textboxes of my whole project with a new one. Actually, When I started that project then I didn't cared much and thought that I will do that as I always did in VB6 using SetWindowsHookEx.
Thanks,
Sameers
|
|
|
|
|
I have followed the discussion and I want to add that the standard TextBox in .NET already has this functionality. Use the property CharacterCasing.
TextBox1.CharacterCasing = CharacterCasing.Upper
Or set it at design-time.
Best regards
Lars-Håkan
|
|
|
|
|
Hi,
Now it seems like you now a lot about subclassing and stuff...
I would like my application to receive notification about other windows as well.
How can I do this?
Thanks in advance.
|
|
|
|
|
You'll need a system wide Hook. There are some nice articles here on Codeproject about how to do this. But you will have to write some Unmanaged code I'm afraid.
Greetz,
Cohen
-- modified at 12:18 Thursday 5th January, 2006
|
|
|
|
|