日期:2014-05-19  浏览次数:21148 次

如何获得隐藏窗体句柄?(coredll.dll找不到)
我的用意是,运行程序单实例化,再次点击程序图标,激活已运行程序(最小化系统托盘或者其他状态)。  
原因在于:最小化托盘后,MainWindowHandle   无法正确获取窗口句柄值。

using   System.Runtime.InteropServices;

[DllImport( "coredll.dll ",   EntryPoint   =   "FindWindow ")]
private   extern   static   IntPtr   FindWindow(string   lpClassName,   string   lpWindowName);

调用时出现以不错误:
Unable   to   load   DLL   'coredll.dll ':   找不到指定的模块。   (Exception   from   HRESULT:   0x8007007E)

------解决方案--------------------
你的单实例问题和这个帖子问题一样吗?
http://community.csdn.net/Expert/TopicView3.asp?id=5512414
------解决方案--------------------
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Bak
{
static class Program
{
/// <summary>
/// 應用程式的主要進入點。
/// </summary>
[STAThread]
static void Main()
{
if (ExistRunningInstance())
{
Environment.Exit(1); // App is running, exit
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

[DllImport( "User32.dll ")]
public static extern void SetForegroundWindow(IntPtr hwnd);

[DllImport( "User32.dll ")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);

// 0-Hidden, 1-Centered, 2-Minimized, 3-Maximized
private const int WS_SHOWNORMAL = 1;

///
/// Finds the running instance.
///
/// true if exist a running instace, otherwise false
private static bool ExistRunningInstance()
{
Process currentProcess = Process.GetCurrentProcess();
Process[] procList = Process.GetProcessesByName(currentProcess.ProcessName);

foreach (Process proc in procList)
{
// Found a running instance
if (proc.Id != currentProcess.Id)
{
// Active the running instance
ShowWindowAsync(proc.MainWindowHandle, WS_SHOWNORMAL);
SetForegroundWindow(proc.MainWindowHandle);

return true;
}
}

return false;
}
}
}
------解决方案--------------------
根据process获取handle,不要用FindWindow

FindWindow只能获取外层可见Handle
------解决方案--------------------
这是我原先vb的代码,看看有没有用。

Module Module1
Sub main()
If RunningInstance() IsNot Nothing Then MsgBox( " ") Else Application.Run(New Form1)
End Sub

Public Function RunningInstance() As Process
Try
Dim current As Process = Process.GetCurrentProcess
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)

For Each tmpProcess As Process In processes
If tmpProcess.Id <> current.Id Then