setParent之后子窗口的奇怪行为

本文关键字:之后 窗口 setParent | 更新日期: 2023-09-27 17:56:08

我尝试使用以下代码在我的Winform应用程序中嵌入"Windows Journal"应用程序。它有效,一切看起来都很好。但是,当我开始使用 Windows 日记本应用程序(它已经是我的应用程序的子窗口)时,我发现鼠标的行为不一致。更具体地说,例如,我试图画一条从(x,y)到(x+100,y)的线,但是这条线显示在(x-50,y-50)到(x,y-50)的窗口中。我使用关键字"鼠标同步/不一致","设置父级后的应用程序异常行为","子窗口的奇怪行为"进行了谷歌搜索,但尚未找到任何相关解决方案。我还尝试在setParent之前清除WS_POPUP样式,但没有奏效。谁能给我一些想法?谢谢。

//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);

代码驻留在名为 JournalControl 的控制器中.cs JournalControl 位于应用程序主窗体的 splitContainer.Panel 中。因此,当单击主窗体的按钮时,首先打开用户在日记应用程序中指示的jnt文件

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);
    }
} 

然后在 timCheckJournal_Tick() 中,将应用程序放入面板中

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;
    }
}

setParent之后子窗口的奇怪行为

听起来您需要在两个工作区之间执行转换...

这是首先阅读的链接:http://support.microsoft.com/kb/11570

如何实现它在很大程度上取决于鼠标绘制从何处启动以及它应该绘制到哪里,但假设您的"托管应用程序"是鼠标侦听器,而 Journal 是您想要绘制的内容 - 序列可能看起来像(伪代码):

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

然后无论你做什么来吸引日记 - WM_MOUSE消息等。