这是一些C#初学者的疑问,我这里也回复一下,希望能够帮到更多需要的人。
C# 窗体界面:

全部代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //在使用API函数之前先要引用
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//显示隐藏任务栏,就需要首先找到该窗口,需用API——FindWindow 函数,懂得在VB中使用该函数,这里就不难了
//VB中该函数声明:
//Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
//大家可以将其与C#中的声明对比下:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string 类名, string 标题);
//显示窗口所用API——ShowWindow,VB声明如下:
//Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
//而在C#中则为:
[DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
//系统菜单控制所用API以及常量
//GetSystemMenu 函数用于取得系统控制菜单,在VB中声明:
//Private Declare Function GetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
//在C#中则为:
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hwnd, bool bRevert);
//删除指定菜单条目,需要用DeleteMenu 函数,在VB中声明为:
//Private Declare Function DeleteMenu Lib "user32" Alias "DeleteMenu" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
//在C#中则为:
[DllImport("user32.dll")]
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);
//禁用或启用某个菜单条目,需用 EnableMenuItem 函数,在VB中声明为:
//Private Declare Function EnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
//而在C#中为:
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
//删除指定菜单条目时所用消息常量声明:在VB中声明为:
//Private Const SC_MOVE = &HF010 ' 
//Private Const SC_CLOSE = &HF060 ' 
//若在C#中声明,则为:
const uint SC_MOVE = 0xF010; //"移动"菜单
const uint SC_CLOSE = 0xF060;//"关闭"
//下不再赘述,请大家和VB中的相应API以及常量相比较,还是那句话:编程是“精一则通百”,高级会员朋友对此应该理解最深:
const uint MF_BYCOMMAND = 0x00; //按命令方式
const uint MF_GRAYED = 0x01; //变灰
const uint MF_DISABLED = 0x02; //不可用
//声音控制所用API及常量:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
const uint WM_APPCOMMAND = 0x319;
const uint APPCOMMAND_VOLUME_UP = 0x0a;
const uint APPCOMMAND_VOLUME_DOWN = 0x09;
const uint APPCOMMAND_VOLUME_MUTE = 0x08;
//****************************************************************************
private void button1_Click(object sender, EventArgs e) //打开记事本并发送F5 按钮单击事件
{
System.Diagnostics.Process p = new System.Diagnostics.Process();
string strPath = System.Environment.SystemDirectory + @"\NotePad.exe";
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(strPath); //打开记事本程序进程
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; //打开的记事本程序最大化
//startInfo.Arguments = "123.txt"; //创建123.txt 文件
p.StartInfo = startInfo;
p.Start();
p.WaitForInputIdle(1000); //使 Process 组件等待关联进程进入空闲状态
if (p.Responding) //如果进程得到响应
{
DateTime dt = DateTime.Now; //取得当前日期和时间,在这里这一句也可不要,因为在记事本程序中F5就是用来插入当前日期和时间
SendKeys.SendWait("{F5}"); //发送键F5
}
else //如果进程未得到响应
{
p.Kill(); //“杀”掉进程
}
}
private void button2_Click(object sender, EventArgs e) //隐藏任务栏 按钮单击事件
{
IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null); //通过类名找到任务栏,任务栏类名为:Shell_TrayWnd ,使用方法同VB
if (trayHwnd != IntPtr.Zero)
{
ShowWindow(trayHwnd, 0); //隐藏任务栏
}
}
private void button3_Click(object sender, EventArgs e) //显示任务栏 按钮单击事件
{
IntPtr trayHwnd = FindWindow("Shell_TrayWnd", null); //通过类名找到任务栏
if (trayHwnd != IntPtr.Zero)
{
ShowWindow(trayHwnd, 1); //显示任务栏
}
}
private void button4_Click(object sender, EventArgs e) //隐藏系统菜单中的“关闭” 按钮单击事件,效果如图:
//获取程序系统菜单“窗口”的句柄
{
IntPtr hMenu = GetSystemMenu(this.Handle, false);
if (hMenu != IntPtr.Zero)
{
DeleteMenu(hMenu, SC_MOVE, MF_BYCOMMAND); //删除移动菜单,禁用系统菜单中的“移动”
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED); //禁用系统菜单以及右上角X“关闭”
}
}
private void button5_Click(object sender, EventArgs e) //提高音量 按钮单击事件
{
SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
}
private void button7_Click(object sender, EventArgs e) //禁用或启用声音
{
SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);
}
private void button6_Click(object sender, EventArgs e) //降低音量
{
SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);
}
以上在VS2005+XP下测试通过。
葛军