System.Diagnostics.Process p = new System.Diagnostics.Process();//创建进程
// p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
// p.Start();//开始进程
有没有办法 把进程SoftBoard 像设置窗体TopMost=true一样 把这个进程设置成处于其他窗体之上
// p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
// p.Start();//开始进程
有没有办法 把进程SoftBoard 像设置窗体TopMost=true一样 把这个进程设置成处于其他窗体之上
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
{
RECT _Rect =new RECT(); GetWindowRect(窗体句柄, ref _Rect); SetWindowPos(窗体句柄, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);
} [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags); [DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}这样看看
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern int GetWindowThreadProcessId(
int hwnd,
out int lpdwProcessId
);hWnd = FindWindow(null, "SoftBoard");
if (hWnd == 0)
{//没启动}
else
{
GetWindowThreadProcessId(hWnd, out id);
}
{
private static Hashtable processWnd = null; public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam); static User32API()
{
if (processWnd == null)
{
processWnd = new Hashtable();
}
} [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam); [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd); [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId); [DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd); [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode); public static IntPtr GetCurrentWindowHandle()
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
object objWnd = processWnd[uiPid]; if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
{
return ptrWnd;
}
else
{
ptrWnd = IntPtr.Zero;
}
} bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
// 枚举窗口返回 false 并且没有错误号时表明获取成功
if (!bResult && Marshal.GetLastWin32Error() == 0)
{
objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
}
} return ptrWnd;
} private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
uint uiPid = 0; if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) // 找到进程对应的主窗口句柄
{
processWnd[uiPid] = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
} return true;
}
}
p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
p.Start();//开始进程
int hWnd = FindWindow(null, "SoftBoard");
if (hWnd == 0)
{ }
else
{
GetWindowThreadProcessId(hWnd, out id);
}
RECT _Rect = new RECT();
GetWindowRect(hWnd, ref _Rect); SetWindowPos(hWnd, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);不是这样啊?
p.MainWindowHandle不行 还是不能让窗体处于最前端
这里要写任务管理器里 应用程序的名字,而不是进程名
p.WaitForInputIdle();
我实际用zege的代码测试过。是可以的。我估计你代码执行太快。获取窗体句柄的时候窗体还没注册号。所以结果不对。
新建控制台程序后。贴如下代码。可以看到效果:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;namespace CSharpConsole06
{ class Program
{
static void Main(string[] args)
{
Process p = Process.Start("Notepad.exe");
p.WaitForInputIdle();
RECT _Rect = new RECT();
bool res = GetWindowRect(p.MainWindowHandle, ref _Rect);
res = SetWindowPos(p.MainWindowHandle, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0); Console.ReadKey();
} [DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int Width, int Height, uint flags); [DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private static void TestChar()
{
string digit = "零壹贰叁肆伍陆柒捌玖";
string Str = "1224";
foreach (char c in Str)
{
Console.Write(digit[c - '0']);
}
}
}
}
if (hWnd == 0)
{ }
else
{
GetWindowThreadProcessId(hWnd, out id);
}
RECT _Rect = new RECT();
GetWindowRect(这里要传intPtr类型, ref _Rect); SetWindowPos(这里要传intPtr类型, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);写好了 那怎么传到里面去呢 这里是个int类型 传到方法里要是个IntPtr的类型
p.StartInfo.FileName = "SoftBoard.exe";//进程打开文件名
p.Start();//开始进程
p.WaitForInputIdle();
RECT _Rect = new RECT();
GetWindowRect(p.MainWindowHandle, ref _Rect); SetWindowPos(p.MainWindowHandle, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);按照你说的做了 但是还是不行 因为我浏览器窗体也是处于最顶端的 我想是不是 2个窗体都处于最顶端导致冲突了
p.WaitForInputIdle();
RECT _Rect = new RECT();
GetWindowRect(p.MainWindowHandle, ref _Rect);
SetWindowPos(p.MainWindowHandle, new IntPtr(-1), _Rect.left, _Rect.top, _Rect.right - _Rect.left, _Rect.bottom - _Rect.top, 0);还是不行 我郁闷啊
加上
[DllImport("user32.dll", EntryPoint = "FindWindow")]
public static extern int FindWindow(
string lpClassName,
string lpWindowName
);
[DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern int GetWindowThreadProcessId(
int hwnd,
out int lpdwProcessId
);hWnd = FindWindow(null, "SoftBoard");
if (hWnd == 0)
{//没启动}
else
{
GetWindowThreadProcessId(hWnd, out id);
}