首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么获得隐藏窗体句柄?(coredll.dll找不到)

2012-01-07 
如何获得隐藏窗体句柄?(coredll.dll找不到)我的用意是,运行程序单实例化,再次点击程序图标,激活已运行程序

如何获得隐藏窗体句柄?(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


If Reflection.Assembly.GetExecutingAssembly().Location.Replace( "/ ", "\ ") = current.MainModule.FileName Then
Return tmpProcess
End If
End If
Next

Return Nothing
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error ")
End
End Try
End Function
End Module
[解决办法]
学习
[解决办法]
http://blog.csdn.net/impeller/archive/2007/05/28/1628152.aspx
最好的办法是首次打开时,就在内存中记录主窗口的句柄,再次打开时从内存中读取句柄,然后激活原先的那个。
利用CreateFileMapping那一套Api来操作。
[解决办法]
Process not just one handle, make it clear.
[解决办法]
判断窗体位置,如果它的边框已经接近屏幕边框,就让它缩进去。重新设置它的位置

热点排行