C# 如何移动不可见的应用程序

C# 如何移动不可见的应用程序,c#,office-interop,visibility,setwindowpos,C#,Office Interop,Visibility,Setwindowpos,这是一个后续问题的一个答案提供后 答案建议我将窗口移出屏幕,这样就看不到闪烁的窗口 如果我理解这个问题的公认答案 已更改其可见性的应用程序(accApp.Visible=false;)没有/return/initialize/。。。IntPtr,它将始终设置为零 如何将此窗口/应用程序移出屏幕,以便用户永远看不到它。我不想让它可见,然后让我的程序在之后弹出/移出屏幕。除非有办法防止可见性延伸到用户的眼睛(永远都不想看到窗口) 理论上,我可以暂停屏幕渲染,使其可见,移动它,然后恢复屏幕渲染吗?(怀

这是一个后续问题的一个答案提供后

答案建议我将窗口移出屏幕,这样就看不到闪烁的窗口

如果我理解这个问题的公认答案

已更改其可见性的应用程序(
accApp.Visible=false;
)没有/return/initialize/。。。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();
        }
    }
}

您遇到过类似的问题吗?@Hans尝试了.hWnd(),但它说Access应用程序没有相应的定义。And.hWndAccessApp()返回零。@Sanson根据我所知,应用程序正在正确终止。只是在执行quit命令时,它们会在屏幕上闪烁。