By "embedding" here, I mean that we take a non-Qt program window and embed 
it into a certain kind of Qt widget.  Then we can put that widget where 
ever we want in our Qt program.  Side by side, whatever.  With a Qt widget 
A, we can attach it to another one, B, like a splitter, by making B be the 
"parent" of A.  If A has no parent, it will be a free-floating window.  
With a non-Qt window like that calculator, we get its window ID from 
Windows, and use that to make a child of B. Now we can put it into our own 
program.  We haven't done anything yet to connect its signaling to the Qt 
system. 

This is probably more than you care about, but in case it's interesting to 
someone, here's the core of how it's done.  I wrote a class called 
ForeignWindowEmbedder.  Here's the method that gets our calculator window 
and re-parents it into a Qt widget:

          # Embed the foreign window using its title
    def embed_foreign_window(self):
        self.hwnd = find_window_by_title(self.title)
        if self.hwnd:
            win32gui.SetParent(self.hwnd, int(self.winId()))

            # Modify the foreign window's style
            style = win32gui.GetWindowLong(self.hwnd,
                            win32con.GWL_STYLE)
            # Remove title bar (caption)
            new_style = style & ~win32con.WS_CAPTION
            win32gui.SetWindowLong(self.hwnd,
                            win32con.GWL_STYLE, new_style)

            # Move the foreign window to the 0, 0 corner of self
            win32gui.SetWindowPos(self.hwnd, None, 0, 0, 0, 0,
                    win32con.SWP_NOSIZE | win32con.SWP_NOZORDER)

self.winId()is the Windows ID of our  ForeignWindowEmbedder widget. With a 
Qt Widget, we would set its parent widget with setParent().  With a raw 
Windows object we use win32gui.SetParent()instead.

On Friday, October 11, 2024 at 9:32:57 PM UTC-4 [email protected] wrote:

On Thursday, September 12, 2024 at 7:30:31 AM UTC+8 [email protected] wrote:

Here is a screen shot of the corrected version of the embedded calculator 
program.  Notice that the calculator's native menu is now available, which 
is a pleasant improvement. In this version, the calculator program only 
receives mouse clicks and key presses when it has been focused by clicking 
on it. That was the tricky part to work out.

You could embed other programs just by changing the program window's 
display name and the path to its executable.  One restriction, which could 
be lifted, is that the external program's window must be non-resizable, as 
this calculator's is.


Hi Thomas, I feel like I'm not following your thoughts, is there any 
benefit to embedding them this way, rather than putting them side by side 
directly?

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/e444f300-561a-407d-9959-21bdf2602a58n%40googlegroups.com.

Reply via email to