【问题标题】:Hide or remove button from external application从外部应用程序隐藏或删除按钮
【发布时间】:2017-06-02 05:04:09
【问题描述】:

我正在从我的 WPF 项目中运行一个外部应用程序,并且我正在使用“user32.dll”将外部应用程序放入我的 WPF 表单中

外部应用有一个退出按钮。我想删除或隐藏该按钮。我可以“使用 user32.dll”或其他方法吗?

提前谢谢你。

【问题讨论】:

  • 您是否有权构建此外部应用程序?
  • @OmegaMan 是的,这是外部 exe 文件。我想访问那个按钮。
  • 这个退出按钮是标准框架的一部分还是屏幕上的随机按钮?
  • @OmegaMan 是随机按钮。它有一个退出按钮。我想删除或隐藏它。
  • 这是不受欢迎的。这是一种被视为 virii 或恶意软件所做的操作,并且通常可以被反恶意软件系统标记。此外,根据系统的配置方式或 Windows 的版本,这些时间可能是访问冲突。编写协作应用程序要好得多;正如 Omegaman 指出的那样,命名管道是一种方法。

标签: c# wpf user32


【解决方案1】:

“使用 user32.dll”

不,您不能使用 user32.dll,因为可以说每个应用都在自己的沙箱中,并且应该不受外界不需要的操作的影响。

(问:您是否有权构建此外部应用程序?答:是)...或其他方法?

由于您可以访问这两个应用程序的代码,因此请让它们实现一个名为管道的进程间进程。在接收应用程序中,让它监视管道以获取关闭按钮或更改其窗口框架样式的消息。

How to: Use Named Pipes for Network Interprocess Communication

【讨论】:

    【解决方案2】:

    以下代码找到按钮并将其隐藏。它在我的系统上运行良好。代码搜索窗口标题,然后找到控件。您必须提供窗口标题和按钮文本。您可以根据需要更新代码。

    注意:下面的代码将隐藏所有与常量 TEXT_BUTTON 中指定的匹配文本的控件。

    const string TEXT_TITLE = "My Specific Window";
    const string TEXT_BUTTON = "&HideMeButton";
    
    public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
    
    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
    const int SW_HIDE = 0;
    
    [DllImport("user32")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
    
    [DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Auto)]
    static extern IntPtr GetWindowCaption(IntPtr hwnd, StringBuilder lpString, int maxCount);  
    
    public void HideSpecificButton()
    {            
        //Contains the handle, can be zero if title not found
        var handleWindow = WinGetHandle(TEXT_TITLE);
        if (GetWindowCaption(handleWindow).Trim() != TEXT_TITLE)
            MessageBox.Show("Window is hidden or not running.");
        else
            GetChildWindows(handleWindow);            
    }
    
    public IntPtr WinGetHandle(string title)
    {
        IntPtr hWnd = IntPtr.Zero;
        foreach (Process pList in Process.GetProcesses())
        {
            if (pList.MainWindowTitle.Contains(title))
            {
                hWnd = pList.MainWindowHandle;
            }
        }
        return hWnd; 
    }
    
    private string GetWindowCaption(IntPtr hwnd)
    {
        StringBuilder sb = new StringBuilder(256);
        GetWindowCaption(hwnd, sb, 256);
        return sb.ToString();
    }
    
    public void GetChildWindows(IntPtr parent)
    {
        List<IntPtr> result = new List<IntPtr>();
        GCHandle listHandle = GCHandle.Alloc(result);
        try
        {
            EnumWindowProc childProc = new EnumWindowProc(EnumControls);
            EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
        }
        finally
        {
            if (listHandle.IsAllocated)
                listHandle.Free();
        }
    }
    
    private bool EnumControls(IntPtr handle, IntPtr pointer)
    {
        var controlTitle = GetWindowCaption(handle).Trim();
        if (string.Equals(controlTitle, TEXT_BUTTON, StringComparison.CurrentCultureIgnoreCase))
        {
            //hide the control
            ShowWindow(handle, SW_HIDE);
        }
    
        return true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-05-01
      • 2020-02-02
      • 2017-02-03
      • 2011-12-05
      • 1970-01-01
      • 2014-02-24
      相关资源
      最近更新 更多