Hi,
two questions concerning window-subclassing:
1. How can I subclass a window outside vb (e.g. Winword)?
2. Is it possible to subclass not only single windows using their
handle, but every window of a class?
thx, Quix
Printable View
Hi,
two questions concerning window-subclassing:
1. How can I subclass a window outside vb (e.g. Winword)?
2. Is it possible to subclass not only single windows using their
handle, but every window of a class?
thx, Quix
Hmm,
Are you sure you want to subclass Word when you could do
everything you want in word with the Microsoft Word Int.Int Object Library?
-Lou
To subclass a window outside your thread, you need to create a standard DLL and add your procedure to the DLL instead of your App.
Subclassing a window of every class requires you to use SetClassLong instead of SetWindowLong.
I forgot to mention, SetClassLong will only affect the classes that are going to be created after the function is called (it will not affect the existing classes).
Also, bare in mind that in VB you cannot subclass something that doesnt belong to your thread. So in order for you to subclass other applications, you'll have to use C/C++ as well as other languages to achieve it.
Ok, I'm back to my thread. Thanks for your replies. I've made some tests, but I didn't succeed in subclassing a Word-window using SetWindowLong in an ActiveX-Dll.
@megatron: What do you mean "create a standard DLL"? There is no Standard-DLL in VB. An ActiveX-Dll wouldn't work. Where do I have to put the hook-function? In the class-module? And what about Serge's comment "...in VB you cannot subclass something that doesnt belong to your thread."?
Will I really have to use C++? :( :(
yes, you can't make DLLs with VB
What is your goal? What do you want to subclass in word from VB?
-Lou
And this better not be another 5 month wait-for!
:D
@Kings: An ActiveX-Dll isn't a Dll?
@NotLKH: This time it's not 5 months but 3 days :( I'm making progresses ;)
What I want to do is this:
On maximizing a window the window's height is automatically restricted by the taskbar. I want to create a region like the taskbar that forces Word not to use the whole desktop for maximizing but to leave a rectangle of 150 px width at the right edge of the screen.
I've tried to put Word into a vb-window using the API's SetParent, but word crashed. A second experiment was to disable Word's maximize-button by removing the 'maximize'-entry of the word's system menu. This worked very fine, but you still could use the whole screen by changing the window's size or by moving the window around.
I think the best way is to subclass the Word-window and to control the messages (WM_SIZE, WM_WINDOWPOSCHANGING...). But I don't want to use C++ :(
Any suggestions? :rolleyes:
You can control Word without sub-classing.
Start a new project, add a reference to MS Word Object Library, {See attached image}
http://www.vbforums.com/attachment.php?s=&postid=686077
Add a Command1 button, and paste the Following code:
VB Code:
Private Sub Command1_Click() 'a twip = 1/20 of a point Dim ConvertTwip As Long ConvertTwip = 20 Dim scrwid As Long Dim scrhit As Long scrwid = Screen.Width \ (ConvertTwip) scrhit = Screen.Height \ (ConvertTwip) Dim mWord As Word.Application Set mWord = New Word.Application mWord.Visible = True mWord.WindowState = wdWindowStateNormal DoEvents mWord.Width = 4 * scrwid \ 5 mWord.Height = 3 * scrhit \ 5 mWord.Top = (scrhit - mWord.Height) \ 2 mWord.Left = (scrwid - mWord.Width) \ 2 DoEvents Set mWord = Nothing End Sub
This code illustrates how to control the width, height and positioning of Word.
Really, it should be checking for a previous instance of word, and it should GetObject if it exists, or if not, do a CreateObject, But I figure you can find some sample code for that.
But, this should get you started down the path to success!:D
-Lou
Thanks for your reply Lou, but this is not at all wat I want to do. :( I know about VBA and the possibility to control the height and width of the Word-window. But I want to restrict the region where the Word-window can be displayed. Using 'Application.Width = [any value]' would only set the width to the defined value. But the Word-window still can be moved around and resized. What I want is to set a maximum value for the right edge of the Word-window, that neither is exceeded on resizing nor on moving the window around.
Perhaps I should use a timer to control the window's size and position every 200 msec, but this isn't the best way to do it :D.
Any ideas?
Any ideas?
Perhaps still might give you some ideas?
http://www.planetsourcecode.com/vb/s...14755&lngWId=1
Haven't tried the code myself, just noticed it was there..
You can subclass MS Word from withing word by using the EventVB.dll and putting the following code in the VBA ThisDocument code and saving it as Normal.dot:
VB Code:
Option Explicit Dim WithEvents vbLInk As EventVB.APIFunctions Dim WithEvents vbWnd As EventVB.ApiWindow Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Sub Document_New() End Sub Private Sub Document_Open() Call InitObjects End Sub Public Sub InitObjects() Set vbLInk = New APIFunctions Set vbWnd = New ApiWindow vbWnd.hwnd = FindWindow(vbNullString, Me.Application.Caption) vbLInk.SubclassedWindows.Add vbWnd End Sub Private Sub vbWnd_Moving(ByVal MoveEdges As Long, MoveRectangle As EventVB._APIRect) If MoveRectangle.Right > 800 Then MoveRectangle.Right = 800 End If End Sub Private Sub vbWnd_Sizing(ByVal SizeEdges As Long, DragRectangle As EventVB._APIRect) If DragRectangle.Right > 800 Then DragRectangle.Right = 800 End If End Sub
That will keep Word in it's place ;-)
HTH,
Duncan