C# WinForm:
程序中需要一个函数来判断当前是否有全屏程序运行,不是指我的C#程序的窗口是不是全屏,而是判断外界的程序
如我的C#程序开在那 如果你要打开全屏游戏比如魔兽争霸之类的的时候 要在我的C#程序里得知 然后将this.Text="有全屏程序运行";
请问是否有相关的帮助资料或直接可用的API函数等 如果涉及系统钩子的话 希望可以给点示例代码 谢谢
程序中需要一个函数来判断当前是否有全屏程序运行,不是指我的C#程序的窗口是不是全屏,而是判断外界的程序
如我的C#程序开在那 如果你要打开全屏游戏比如魔兽争霸之类的的时候 要在我的C#程序里得知 然后将this.Text="有全屏程序运行";
请问是否有相关的帮助资料或直接可用的API函数等 如果涉及系统钩子的话 希望可以给点示例代码 谢谢
解决方案 »
- 多个textBox1 怎么共用同一个Leave 离开事件
- 基于POP3协议收发邮件系统
- SqlConnection的作用我是这样理解的,大家看对不对
- 关于C#2005资源
- sharpdevelop怎么不显示运行结果
- 在C#中怎样设置可变长度的byte??
- 如何在DataGrid里用正则表达式控制日期显示格式?
- 求以下代码,每个一百分,收到开贴给分
- 如何在程序开始的时候画图?
- TCP与PLC通信,调用libnodave.dll文件,发现发送过程中,出现接收紊乱的情况,百度说是TCP粘包问题,站在上位机的角度来说,如何修改
- socket的SendFile(),怎么接收文件?
- C#Winform让打开的窗体不重复(没事干,发个老掉牙的问题)
//获取窗体大小的API
[DllImport("user32")]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); //RECT结构体定义
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//屏幕大小
Rectangle rect = Screen.PrimaryScreen.Bounds;
foreach (Process p in Process.GetProcesses())
{
//找到想要的进程
if(p.ProcessName == "Maxthon")
{
RECT r;
GetWindowRect(p.MainWindowHandle,out r);
//比较
if((r.Right - r.Left) >= rect.Width && (r.Bottom - r.Top) >= rect.Height)
{
//to do,这种情况肯定已经是全屏了
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
//取得前台窗口句柄函数
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
//取得桌面窗口句柄函数
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
//取得Shell窗口句柄函数
[DllImport("user32.dll")]
private static extern IntPtr GetShellWindow();
//取得窗口大小函数
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowRect(IntPtr hwnd, out RECT rc);
//桌面窗口句柄
private IntPtr desktopHandle; //Window handle for the desktop
//Shell窗口句柄
private IntPtr shellHandle; //Window handle for the shell 因为桌面窗口和Shell窗口也是全屏,要排除在其他全屏程序之外。 //取得桌面和Shell窗口句柄
desktopHandle = GetDesktopWindow();
shellHandle = GetShellWindow(); //取得前台窗口句柄并判断是否全屏
bool runningFullScreen = false;
RECT appBounds;
Rectangle screenBounds;
IntPtr hWnd;
//取得前台窗口
hWnd = GetForegroundWindow();
if (hWnd != null && !hWnd.Equals(IntPtr.Zero))
{
//判断是否桌面或shell
if (!(hWnd.Equals(desktopHandle) ¦ ¦ hWnd.Equals(shellHandle)))
{
//取得窗口大小
GetWindowRect(hWnd, out appBounds);
//判断是否全屏
screenBounds = Screen.FromHandle(hWnd).Bounds;
if ((appBounds.Bottom - appBounds.Top) == screenBounds.Height && (appBounds.Right - appBounds.Left) == screenBounds.Width)
runningFullScreen = true;
}
}
desktopHandle = GetDesktopWindow();方法必须返回值!!!我已经添加了 using System.Runtime.InteropServices;
{
[DllImport("SHELL32", CallingConvention = CallingConvention.StdCall)]
public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData); [DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int RegisterWindowMessage(string msg); } [StructLayout(LayoutKind.Sequential)]
public struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;
public RECT rc;
public IntPtr lParam;
} public enum ABMsg : int
{
ABM_NEW = 0,
ABM_REMOVE,
ABM_QUERYPOS,
ABM_SETPOS,
ABM_GETSTATE,
ABM_GETTASKBARPOS,
ABM_ACTIVATE,
ABM_GETAUTOHIDEBAR,
ABM_SETAUTOHIDEBAR,
ABM_WINDOWPOSCHANGED,
ABM_SETSTATE
} public enum ABNotify : int
{
ABN_STATECHANGE = 0,
ABN_POSCHANGED,
ABN_FULLSCREENAPP,
ABN_WINDOWARRANGE
} public enum ABEdge : int
{
ABE_LEFT = 0,
ABE_TOP,
ABE_RIGHT,
ABE_BOTTOM
}[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom; public override string ToString()
{
return "{left=" + left.ToString() + ", " + "top=" + top.ToString() + ", " +
"right=" + right.ToString() + ", " + "bottom=" + bottom.ToString() + "}";
}
}重载窗口消息处理函数:
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == uCallBackMsg)
{
switch (m.WParam.ToInt32())
{
case (int)ABNotify.ABN_FULLSCREENAPP:
{
if ((int)m.LParam == 1)
this.RunningFullScreenApp = true;
else
this.RunningFullScreenApp = false;
break;
}
default:
break;
}
} base.WndProc(ref m);
}在程序退出时,不要忘了Unregister我们注册的AppBar:
this.RegisterAppBar(true);以上介绍的是C# Winform程序判断是否有全屏窗口程序正在运行,希望队你有所帮助。