110,538
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 窗体总在前端显示
/// </summary>
/// <param name="hwnd">窗体的句柄</param>
/// <param name="hWndInsertAfter">窗口句柄。在窗口列表中,窗口hwnd会置于这个窗口句柄的后面</param>
/// <param name="x">窗口新的x坐标</param>
/// <param name="y">窗口的新的y坐标</param>
/// <param name="cx">窗口的宽度</param>
/// <param name="cy">窗口的高度</param>
/// <param name="wFlags"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int SetWindowPos(
IntPtr hwnd,
int hWndInsertAfter,
int x,
int y,
int cx,
int cy,
int wFlags
);
public MostTopWindow()
{
InitializeComponent();
}
private void MostTopWindow_Load(object sender, EventArgs e)
{
SetWindowPos(this.Handle, -1, (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2, this.Width, this.Height, 0);
}
namespace WindowsFormsApplication1
{
partial class HELL
{
/// <summary>
/// 設計工具所需的變數。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清除任何使用中的資源。
/// </summary>
/// <param name="disposing">如果應該處置 Managed 資源則為 true,否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 設計工具產生的程式碼
/// <summary>
/// 此為設計工具支援所需的方法 - 請勿使用程式碼編輯器修改這個方法的內容。
///
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// HELL
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "HELL";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.Text = "HELL";
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class HELL : Form
{
[DllImport("user32.dll", EntryPoint = "GetForegroundWindow")]
public static extern IntPtr GetForegroundWindow(); //获得当前活动窗体的句柄
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体句柄的窗体为活动窗体
[DllImport("User32.dll", EntryPoint = "RemoveMenu")]
private static extern int RemoveMenu(int hMenu, int nPosition, int wFlags); //移除Menu中的選項
[DllImport("User32.dll", EntryPoint = "GetSystemMenu")]
private static extern int GetSystemMenu(int hwnd, int bRevert); //獲取系統Menu
const int MF_BYPOSITION = 0x0400;
IntPtr ThisHandle; //當前Handle
public HELL()
{
InitializeComponent();
ThisHandle = this.Handle;
}
private void Form1_Load(object sender, EventArgs e)
{
//移除Close按鍵
RemoveCloseButtion();
timer1.Interval = 2000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
//取得當前獲取焦點的進程
IntPtr FocusedHandle = GetForegroundWindow();
if (FocusedHandle != ThisHandle) //如果非本進程獲取焦點的進程
{
SetForegroundWindow(ThisHandle); //設置本進程獲取焦點
}
}
//移除關閉按鍵
private void RemoveCloseButtion()
{
int lHwnd = (int)this.Handle ;
RemoveMenu(GetSystemMenu(lHwnd, 0), 6, MF_BYPOSITION);
}
}
}