首页 > 解决方案 > 如何移动不可见的应用程序

问题描述

这是为 post Quit() 提供的答案之一的后续问题,导致 Access 暂时可见

答案建议我将窗口移出屏幕,以便看不到闪烁的窗口。

如果我理解问题 C# Process.MainWindowHandle 的公认答案总是返回 IntPtr 零

已更改其可见性 ( accApp.Visible = false;) 的应用程序没有/返回/初始化/... IntPtr,它将始终设置为零。

如何将此窗口/应用程序移出屏幕,以便用户永远看不到它。我不想让它可见,然后让我的程序弹出/将其移出屏幕。除非有办法阻止可见性延伸到用户的眼睛(永远不想看到窗口)。

理论上我可以以某种方式暂停屏幕渲染,使其可见,移动它然后恢复屏幕渲染吗?(怀疑这是一个很好的解决方案)

自从我上一篇文章以来,我已将创建应用程序的代码移到其单独的类中,以允许仅运行应用程序的一个实例,而不是以前为每个请求生成一个新应用程序的方式。

public class ConnectionManager
{
    [DllImport("user32.dll")]
    static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);

    //Old Code for tying to resolve flashing access window. 
    //[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    //public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);


    public ConnectionManager()
    {
        Console.WriteLine("Connection Manager Started");
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit);

    }

    void OnProcessExit(object sender, EventArgs e)
    {
         BreakAllConnections();
    }

    private Microsoft.Office.Interop.Excel.Application _excelApp;
    public Microsoft.Office.Interop.Excel.Application excelApp
    {
        get
        {
            if (_excelApp == null)
            {
                try
                {
                    _excelApp = new Microsoft.Office.Interop.Excel.Application();
                }
                catch
                {
                    //TODO Add Termination
                    MessageBox.Show("Abort Error: Could not open Exel Application");
                }
            }
            return _excelApp;
        }

    }

    private Microsoft.Office.Interop.Access.Application _accessApp;
    public Microsoft.Office.Interop.Access.Application accessApp
    {
        get
        {
            if (_accessApp == null)
            {
                try
                {
                    _accessApp = new Microsoft.Office.Interop.Access.Application();
                    //Old Code for tying to resolve flashing access window. 
                    //GetWindowThreadProcessId(_accessApp.hWndAccessApp(), out int id);

                    //IntPtr handle = Process.GetProcessById(id).MainWindowHandle;
                    //if (handle != IntPtr.Zero)
                    //{
                    //    const short SWP_NOSIZE = 1;
                    //    const short SWP_NOZORDER = 0X4;
                    //    SetWindowPos(handle, 0, 10000, 10000, 0, 0, SWP_NOZORDER | SWP_NOSIZE | 0);
                    //}
                }
                catch
                {
                    //TODO Add Termination
                    MessageBox.Show("Abort Error: Could not open Exel Application");
                }
            }
            return _accessApp;
        }

    }

    public void BreakAllConnections()
    {

        try
        {
            if (_excelApp != null) { _excelApp.Quit(); Marshal.ReleaseComObject(_excelApp); }
        }
        catch
        {
            GetWindowThreadProcessId(_excelApp.Hwnd, out int id);
            Process.GetProcessById(id).Kill();
        }

        try
        {
            if (_accessApp != null) { _accessApp.Quit(); Marshal.ReleaseComObject(_accessApp); }
        }
        catch
        {
            GetWindowThreadProcessId(_accessApp.hWndAccessApp(), out int id);
            Process.GetProcessById(id).Kill();
        }
    }
}

标签: c#office-interopvisibilitysetwindowpos

解决方案


推荐阅读