在C#中隐藏和显示窗口exe(MS word,excel等) [英] Hiding and showing window exe(MS word, excel, etc) in C#
问题描述
你好,我正在c#中做一个应用程序,在我的应用程序中我打开一些像MS word,calculator这样的exe文件,所以当我打开应用程序然后我隐藏它我想在应用程序中创建一种快捷方式所以当我点击拍摄时,窗口会再次打开。
我尝试过的事情:
我做的是我能够将exe文件的窗口放在topMost上但是当它被隐藏或调整大小时我想在C#上的应用程序中创建一个快捷方式,这样当我点击在shorcut上,exe文件将重新打开。
谢谢。
Hello, I am doing an application in c# and in my application I am opening some exe file like MS word, calculator so when I open the application and then I hide it I would like to create a sort of shortcut in the app so that when I click on the shotcut the window will open again.
What I have tried:
What I did is that I was able to put the window of the exe file on topMost but when it is hidden or resized I would like to create a shortcut in the application on C# so that when I click on the shorcut the exe file will reopen.
Thank you.
推荐答案
你能在下面试试吗?我现在已经测试了它并且工作正常。
Can you try below. I have now tested it and it is working fine.
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static void ShowToFront(string windowName)
{
try
{
IntPtr firstInstance = FindWindow(null, windowName);
ShowWindow(firstInstance, 1);
SetForegroundWindow(firstInstance);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
您必须完整调用该功能要还原的应用程序的名称。 (完全如在Windows任务管理器上看到的那样 - >应用程序选项卡)。您可以从process.MainWindowTitle获取名称。检查以下活动..
You have to call the function with the full name of the application that you want to restore. (exactly as seen on the windows task manager -> application tab). You can get the name from process.MainWindowTitle. Check the below event..
private void button1_Click(object sender, EventArgs e)
{
try
{
Process[] Processes = Process.GetProcessesByName("winword");
foreach (Process p in Processes)
{
ShowToFront(p.MainWindowTitle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
以下是检查窗口是否最小化然后恢复的方法。 />
Here is how you can check whether the window is minimised and then restore it.
private void button1_Click(object sender, EventArgs e)
{
try
{
var processes = Process.GetProcessesByName("winword");
foreach (var p in processes)
{
MaximizeWindowIfMinized(p.MainWindowTitle);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
[DllImportAttribute("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImportAttribute("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImportAttribute("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
public static void MaximizeWindowIfMinized(string windowName)
{
try
{
var instance = FindWindow(null, windowName);
if (IsIconic(instance))
{
MessageBox.Show(@"Window is minimized");
ShowWindow(instance, 1);
SetForegroundWindow(instance);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Hey guys, i've tried something like this... and the problem was that I had no control over the overall System Wide applications.... when I tried using the following:
<pre> List<Form> oForms = new List<Form>();
foreach (Form form in Application.OpenForms)
{
MessageBox.Show("form.text");
MessageBox.Show(form.Text);
oForms.Add(form);
}
foreach (var form in oForms)
{
//MessageBox.Show("form.text");
//MessageBox.Show(form.Text);
form.Hide();
}
Application.OpenForms总是适用于我正在处理的应用程序......它不会覆盖在屏幕上打开的任何其他内容,如CALC,NOTEPAD,IEXPLORE等...我尝试了几件事情,它只会在已注释掉中显示我的应用程序的标题'MessageBoxes ...是否有其他C#'全球'类型变量我应该使用...?
先谢谢你们,
the "Application.OpenForms" was always just something that applied to the application I was working on... It wouldn't cover anything else that was open on the screen like "CALC", "NOTEPAD", "IEXPLORE", etc... I tried several things and it would only display the title of my application in the 'commented-out' MessageBoxes... Is there maybe another C# 'Global' type variable I should be using instead...?
Thanks in advance guys,
这篇关于在C#中隐藏和显示窗口exe(MS word,excel等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!