using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using WinAPI;
namespace ryCommon
{
///
/// 窗体操作类
///
public class RyForm
{
///
/// 设置Comobox的行间距
///
///
///
public static void CmbBind(ComboBox list, int itemHeight)
{
list.DropDownStyle = ComboBoxStyle.DropDownList;
list.ItemHeight = itemHeight;
list.DrawMode = DrawMode.OwnerDrawFixed;
list.DrawItem += new DrawItemEventHandler(delegate (object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(list.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y + 3);
});
}
///
/// 移除关闭按钮。返回值,非零表示成功,零表示失败。
///
/// 窗口的句柄
/// 是否成功
public static int RemoveXButton(IntPtr iHWND)
{
int iSysMenu;
const int MF_BYCOMMAND = 0x400; //0x400-关闭
iSysMenu = User32.GetSystemMenu(iHWND, 0);
return User32.RemoveMenu(iSysMenu, 6, MF_BYCOMMAND);
}
///
/// 把窗体放到最前
///
///
public static void BringToTop(IntPtr iHWND)
{
User32.SetForegroundWindow(iHWND);
}
///
/// 显示窗体
///
///
///
///
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool ShowWindow(HandleRef hWnd, int nCmdShow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
private const int HWND_TOPMOST = -1;
private const int HWND_NOTOPMOST = -2;
private const int SWP_NOACTIVATE = 0x10;
private const int SWP_NOMOVE = 0x2;
private const int SWP_NOSIZE = 0x1;
///
/// 设置窗体是否置顶
///
///
///
public static void SetTopMost(IntPtr iHWND,bool TopMost)
{
SetWindowPos(iHWND, (TopMost?HWND_TOPMOST: HWND_NOTOPMOST), 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
///
/// 显示没有焦点的窗口
///
///
public static void ShowNoActiveWindow(Form frm)
{
ShowWindow(new HandleRef(frm, frm.Handle), 4);
}
///
/// 根据句柄获取窗口文本
///
///
///
public static string GetWinText(IntPtr Handle)
{
StringBuilder WinTitle = new StringBuilder(200);
int len = User32.GetWindowText(Handle.ToInt32(), WinTitle, 200);
return WinTitle.ToString();
}
///
/// 根据句柄获取窗口类
///
///
///
public static string GetWinClass(IntPtr Handle)
{
StringBuilder WinClass = new StringBuilder(200);
int len = User32.GetClassName(Handle.ToInt32(), WinClass, 200);
return WinClass.ToString();
}
///
/// 获取当前活动的窗口句柄
///
///
public static IntPtr GetActiveWindow()
{
return User32.GetActiveWindow();
}
///
/// 设置为当前活动窗口
///
///
///
public static IntPtr SetActiveWindow(IntPtr hwnd)
{
return User32.SetActiveWindow(hwnd);
}
///
/// 根据窗口句柄来获得进程id
///
///
///
public static int GetProcessId(IntPtr hwnd)
{
User32.GetWindowThreadProcessId(hwnd, out int ID);
return ID;
}
///
/// 显示窗体
///
///
public static void Show(Form frm)
{
bool topmost = frm.TopMost;
frm.BringToFront();
frm.TopMost = true;
frm.TopMost = topmost;
frm.Show();
}
///
/// 设置窗体父窗体为桌面,不会随着显示桌面而最小化,但无法设置窗体透明度
///
///
public static void SetDesktopForm(IntPtr handle)
{
//在构造函数中或者Load中
IntPtr hDeskTop = WinAPI.User32.FindWindow("Progman", null);
WinAPI.User32.SetParent(handle, hDeskTop);
}
///
/// 结束进程
///
///
///
public static long KillPorc(int pid)
{
return WinAPI.Kernel32.TerminateProcess(pid,1);
}
///
/// 打开窗体,只打开一个实例(非模态)
///
///
///
public static Form ShowOne(Form type)
{
try
{
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType() == type.GetType())
{
frm.WindowState = FormWindowState.Normal;
frm.BringToFront();
return frm;
}
}
type.StartPosition = FormStartPosition.CenterScreen;
type.Show();
return type;
}
catch { }
return null;
}
}
}