strange behavior of child windows after setParent

1.5k Views Asked by At

I tried to embedded "Windows Journal" application in my Winform application by using following code. It works and everything looks good. However, when I start using Windows Journal application, which is already a child window of my application, I found the mouse behave inconsistent. Be more specific, for example, I tried to draw a line from (x, y) to (x+100, y), but the line show up on the window at (x-50, y-50) to (x, y-50). I have googling by using keywords “Mouse synchronize/inconsistent”, “application abnormal behavior after setparent, “Strange behavior of child-windows”, but not found any related solution yet. I also tried to clear the WS_POPUP style before setParent, but it didn't work. Could anyone give me some ideas? Thanks.

//IntPtr appWin is the MainWindowHandle of Windows Journal process 
ShowWindowAsync(appWin, SW_SHOWNORMAL);

// Put it into this form
SetParent(appWin, this.Handle);

// Remove border and whatnot
SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);

// Move the window to overlay it on this window
MoveWindow(appWin, 0, 0, this.Width, this.Height, true);

The code is resides in a Controller called JournalControl.cs and JournalControl is located in a splitContainer.Panel in the Main Form of the application. So, when a button of the main form be clicked, first, open a jnt file which the user indicates in Journal application

public void OpenJournal(string JNTPath)
{
    try
    {
        if (File.Exists(JNTPath))
        {
            proc.StartInfo.FileName = @"C:\Program Files\Windows Journal\Journal.exe";
            proc = Process.Start(JNTPath); 

            this.timCheckJournal = new System.Windows.Forms.Timer();
            this.timCheckJournal.Tick += new System.EventHandler(this.timCheckJournal_Tick);          
        }
        else
        {
            MessageBox.Show("Couldn't find  JNTPath: " + JNTPath);
        }
    }
    catch (Exception ex)
    {
        ExceptionPublisher.PublishEx(ex);
    }
} 

Then in timCheckJournal_Tick(), puts the application inside the Panel

private void timCheckJournal_Tick(object sender, EventArgs e)
{
    timCheckJournal.Enabled = false;
    IntPtr appWin = GetJournalTopWindowHandle();
    if (appWin != IntPtr.Zero)
    {
        ShowWindowAsync(appWin, SW_SHOWNORMAL);

        // Put it into this form
        SetParent(appWin, this.Handle);

        // Remove border and whatnot
        SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);

        // Move the window to overlay it on this window
        MoveWindow(appWin, 0, 0, this.Width, this.Height, true);
    }
    else
    {
        timCheckJournal.Enabled = true;
    }
}
1

There are 1 best solutions below

1
On

It sounds like you need to perform the translation between the two client areas...

Here's a link to read first: http://support.microsoft.com/kb/11570

How you implement it will depend a lot on where the mouse-drawing is initiating from and where it's supposed to be drawing to, but let's say your "hosting app" is the mouse listener and Journal is the thing you'd want to draw to - the sequence would probably look something like (pseudocode):

screenCoords = ClientToScreen(mouse coords)
journalCoords = ScreenToClient(screenCoords)

Then whatever you were doing to draw to Journal - WM_MOUSE messages, etc.