使用API实现的一个增加系统桌面,并且可以任意切换的小程序.
很抱歉地告诉大家,到目前为止,我还没有想到删除桌面的方法,因为现有的程序删除并不能真正删除掉.不过,有些东西大家还是可以相互借鉴的嘛。希望以下代码可以对你有些帮助,那我就很满意了。
以下是所有源码
你可以复制到IDE中,直接编译。哦,对了,为了省事,我没有把关于窗口的代码发上来。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Text;
namespace SwitchDesktop
{
/// <summary>
/// 创建桌面程序
///
/// 由于程序还未完成.所以有许多功能尚未实现.
/// 可以很好地描述使用系统API来实现的一些功能.
///
/// 程序:房客
/// QQ:83849123
/// 日期:2006-3-1
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.NotifyIcon notifyIcon1;
private System.Windows.Forms.ContextMenu SysMenu;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MainMenu MainMenu;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem11;
private System.Windows.Forms.MenuItem menuItem12;
private System.Windows.Forms.MenuItem menuItem13;
private System.Windows.Forms.MenuItem menuItem14;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.MenuItem menuItem16;
private System.Windows.Forms.MenuItem menuItem17;
private System.Windows.Forms.MenuItem menulist;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.listBox1 = new System.Windows.Forms.ListBox();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.SysMenu = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.MainMenu = new System.Windows.Forms.MainMenu();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem11 = new System.Windows.Forms.MenuItem();
this.menuItem12 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.menuItem13 = new System.Windows.Forms.MenuItem();
this.menulist = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem16 = new System.Windows.Forms.MenuItem();
this.menuItem17 = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(224, 100);
this.listBox1.TabIndex = 2;
this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenu = this.SysMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "消息";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.menuItem1_Click);
//
// SysMenu
//
this.SysMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2,
this.menuItem3});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "显示/隐藏(&H)";
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "-";
//
// menuItem3
//
this.menuItem3.Index = 2;
this.menuItem3.Text = "退出(&X)";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// MainMenu
//
this.MainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem4,
this.menuItem5,
this.menulist,
this.menuItem7});
//
// menuItem4
//
this.menuItem4.Index = 0;
this.menuItem4.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem8,
this.menuItem9,
this.menuItem10});
this.menuItem4.Text = "系统(&S)";
//
// menuItem8
//
this.menuItem8.Index = 0;
this.menuItem8.Text = "最小化(&I)";
this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);
//
// menuItem9
//
this.menuItem9.Index = 1;
this.menuItem9.Text = "-";
//
// menuItem10
//
this.menuItem10.Index = 2;
this.menuItem10.Text = "退出(&X)";
this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem11,
this.menuItem12,
this.menuItem14,
this.menuItem13});
this.menuItem5.Text = "操作(&O)";
//
// menuItem11
//
this.menuItem11.Index = 0;
this.menuItem11.Text = "新建(&N)";
this.menuItem11.Click += new System.EventHandler(this.menuItem11_Click);
//
// menuItem12
//
this.menuItem12.Index = 1;
this.menuItem12.Text = "切换(&W)";
this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
//
// menuItem14
//
this.menuItem14.Index = 2;
this.menuItem14.Text = "-";
//
// menuItem13
//
this.menuItem13.Index = 3;
this.menuItem13.Text = "关闭(&C)";
this.menuItem13.Click += new System.EventHandler(this.menuItem13_Click);
//
// menulist
//
this.menulist.Index = 2;
this.menulist.Text = "桌面(&D)";
//
// menuItem7
//
this.menuItem7.Index = 3;
this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem15,
this.menuItem16,
this.menuItem17});
this.menuItem7.Text = "帮助(&H)";
//
// menuItem15
//
this.menuItem15.Index = 0;
this.menuItem15.Text = "帮助(&H)";
//
// menuItem16
//
this.menuItem16.Index = 1;
this.menuItem16.Text = "-";
//
// menuItem17
//
this.menuItem17.Index = 2;
this.menuItem17.Text = "关于(&A)";
this.menuItem17.Click += new System.EventHandler(this.menuItem17_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(240, 118);
this.ContextMenu = this.SysMenu;
this.Controls.Add(this.listBox1);
this.Cursor = System.Windows.Forms.Cursors.Arrow;
this.MaximizeBox = false;
this.Menu = this.MainMenu;
this.Name = "Form1";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Desktop";
this.TopMost = true;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
#region 声明的外部函数
[DllImport("user32", EntryPoint="CreateDesktop")]
public static extern IntPtr CreateDesktop (
string lpszDesktop,
IntPtr lpszDevice,
IntPtr pDevmode,
int dwFlags,
int dwDesiredAccess,
IntPtr lpsa
);
[DllImport("user32", EntryPoint="CloseDesktop")]
public static extern int CloseDesktop (
IntPtr hDesktop
);
[DllImport("user32", EntryPoint="GetThreadDesktop")]
public static extern IntPtr GetThreadDesktop (
int dwThread
);
[DllImport("user32", EntryPoint="SwitchDesktop")]
public static extern int SwitchDesktop (
IntPtr hDesktop
);
[DllImport("user32", EntryPoint="EnumDesktops")]
public static extern bool EnumDesktops (
IntPtr hwinsta,
EnumProc lpEnumFunc,
IntPtr lParam
);
[DllImport("user32", EntryPoint="GetProcessWindowStation")]
public static extern IntPtr GetProcessWindowStation ();
[DllImport("user32", EntryPoint="OpenDesktop")]
public static extern IntPtr OpenDesktop (
string lpszDesktop,
int dwFlags,
Boolean fInherit,
long dwDesiredAccess
);
[DllImport("user32", EntryPoint="RegisterHotKey")]
public static extern int RegisterHotKey (
IntPtr hwnd,
int id,
int fsModifiers,
Keys key
);
[DllImport("user32", EntryPoint="UnregisterHotKey")]
public static extern int UnregisterHotKey (
IntPtr hwnd,
int id
);
[DllImport("kernel32", EntryPoint="CreateProcess")]
public static extern bool CreateProcess (
string lpApplicationName,
string lpCommandLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
int dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDriectory,
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation
);
[DllImport("user32", EntryPoint="GetWindowText")]
public static extern int GetWindowText (
IntPtr hwnd,
StringBuilder lpString,
int cch
);
#endregion
#region 声明结构
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}
[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
#endregion
#region 声明的常量
public const int GENERIC_ALL=0x10000000;
public const int MOD_WIN=0x8;
public const int WM_HOTKEY=0x312;
public const int WM_SYSCOMMAND=0x112;
public const int SC_CLOSE=0xF060;
public const int SC_MINIMIZE=0xF020;
public const int NORMAL_PRIORITY_CLASS=0x20;
private const long DESKTOP_CREATEWINDOW = 0x0002L;
private const long DESKTOP_ENUMERATE = 0x0040L;
private const long DESKTOP_WRITEOBJECTS = 0x0080L;
private const long DESKTOP_SWITCHDESKTOP = 0x0100L;
private const long DESKTOP_CREATEMENU = 0x0004L;
private const long DESKTOP_HOOKCONTROL = 0x0008L;
private const long DESKTOP_READOBJECTS = 0x0001L;
private const long DESKTOP_JOURNALRECORD = 0x0010L;
private const long DESKTOP_JOURNALPLAYBACK = 0x0020L;
private const long AccessRights = DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL | DESKTOP_READOBJECTS;
#endregion
#region 声明的变量
private string strDesktopName = "Another";
private IntPtr hwndNewDesktop;
private IntPtr hwndOldDesktop;
private ArrayList m_winList ;
private bool IsHide = false ;
private int hotkey = 0xffcf;
#endregion
#region 声明委托
public delegate bool EnumProc( string Desktop,IntPtr LParam );
#endregion
#region 自定义方法
private bool NewDesktop()
{
if( strDesktopName != string.Empty )
{
foreach ( object obj in m_winList )
{
if( obj.ToString() == strDesktopName )
{
throw new Exception ("指定的桌面名称已存在!");
}
}
hwndNewDesktop = CreateDesktop( strDesktopName,IntPtr.Zero,IntPtr.Zero,0,GENERIC_ALL,IntPtr.Zero);
StartProcess( "explorer.exe" );
if( StartProcess( "ctfmon.exe" ) == null )
StartProcess( "internat.exe" );
UpdateList();
return true;
}
else
{
throw new Exception( "桌面名称不能为空" );
}
}
private bool GetDesktops()
{
bool flag = false;
IntPtr hwndWStation = GetProcessWindowStation();
if (hwndWStation != IntPtr.Zero)
{
flag = EnumDesktops( hwndWStation,new EnumProc(EnumDesktopProc),IntPtr.Zero );
}
return flag;
}
private bool EnumDesktopProc( string Desktop,IntPtr LParam )
{
if(Desktop.ToString() != "Winlogon" && Desktop.ToString() != "Disconnect")
m_winList.Add( Desktop );
return true;
}
private void UpdateList()
{
m_winList = new ArrayList();
if( GetDesktops() )
{
// ManageArray();
this.listBox1.DataSource = m_winList;
AppendMenu();
RegHotKey();
}
else
{
MessageBox.Show( "GetDesktops Fail!" );
}
}
private bool AppendMenu()
{
bool flag = false;
GetCurrentDesktop();
menulist.MenuItems.Clear();
if( m_winList.Count > 0 )
{
MenuItem menu1 ;
foreach( object obj in m_winList )
{
menu1 = new MenuItem( obj.ToString() );
//menu1.Checked = true;
menu1.Click += new EventHandler(menuItem_Click);
menulist.MenuItems.Add( menu1 );
}
}
return flag;
}
private void menuItem_Click(object sender, System.EventArgs e)
{
listBox1.SelectedItem = ((MenuItem)sender).Text;
Switch();
}
private string GetCurrentDesktop()
{
hwndOldDesktop = GetThreadDesktop( AppDomain.GetCurrentThreadId() );
StringBuilder sb = new StringBuilder( 100 );
GetWindowText( hwndOldDesktop,sb,101 );
string strTemp = sb.ToString();
return strTemp;
}
private Process StartProcess( string strName )
{
STARTUPINFO si = new STARTUPINFO();
si.cb = Marshal.SizeOf(si);
si.lpDesktop = strDesktopName;
//Process p ;
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
bool isOK = CreateProcess( null, strName,IntPtr.Zero,IntPtr.Zero,true,NORMAL_PRIORITY_CLASS,IntPtr.Zero,null,ref si,ref pi );
if( isOK )
return Process.GetProcessById( pi.dwProcessId );
else
return null;
}
private bool RegHotKey()
{
bool flag = false;
if( RegisterHotKey( this.Handle,hotkey,MOD_WIN,Keys.D1) >0 )
flag = true;
return flag;
}
private void UnHotKey()
{
UnregisterHotKey( this.Handle,hotkey );
}
// private void ManageArray()
// {
// foreach ( object obj in m_winList )
// {
// if(obj.ToString() == "Winlogon" || obj.ToString() == "Disconnect")
// m_winList.Remove( obj );
// }
// }
private void Switch()
{
strDesktopName = listBox1.SelectedItem.ToString();
hwndNewDesktop = OpenDesktop( strDesktopName,0,false,AccessRights );
StartProcess( Process.GetCurrentProcess().ProcessName );
SwitchDesktop( hwndNewDesktop );
//Process.GetCurrentProcess().Kill();
Application.Exit();
}
private void ShowWindow()
{
if( this.Opacity != 1.0d )
{
this.Opacity = 1.0d;
IsHide = true;
}
else
{
if( IsHide )
{
Show();
IsHide = false;
}
else
{
Hide();
IsHide = true;
}
}
}
private int CloseDesk()
{
strDesktopName = listBox1.SelectedItem.ToString();
hwndNewDesktop = OpenDesktop( strDesktopName,0,false,AccessRights );
return CloseDesktop( hwndNewDesktop );
}
#endregion
#region 控件方法
private void Form1_Load(object sender, System.EventArgs e)
{
UpdateList();
this.MaximumSize = this.Size;
this.MinimumSize = this.Size;
this.Location = new Point( Screen.PrimaryScreen.WorkingArea.Width - this.Width ,Screen.PrimaryScreen.WorkingArea.Height - this.Height );
this.Opacity = 0.0d;;
}
private void menuItem1_Click(object sender, System.EventArgs e)
{
ShowWindow();
}
private void listBox1_DoubleClick(object sender, System.EventArgs e)
{
if( listBox1.SelectedIndex > -1)
Switch();
}
private void menuItem10_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuItem8_Click(object sender, System.EventArgs e)
{
ShowWindow();
}
private void menuItem12_Click(object sender, System.EventArgs e)
{
if( listBox1.SelectedIndex > -1 )
Switch();
}
private void menuItem17_Click(object sender, System.EventArgs e)
{
About about = new About();
about.ShowDialog( this );
}
private void menuItem11_Click(object sender, System.EventArgs e)
{
DesktopName nameFrom = new DesktopName();
if( nameFrom.ShowDialog( this ) == DialogResult.OK )
{
this.strDesktopName = nameFrom.strDesktopName;
if (!NewDesktop())
MessageBox.Show( "创建失败!" );
}
this.StartPosition = FormStartPosition.Manual;
}
private void menuItem13_Click(object sender, System.EventArgs e)
{
if( CloseDesk()<1 )
MessageBox.Show( "关闭失败!" );
UpdateList();
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
#endregion
protected override void WndProc(ref Message m)
{
if( m.Msg == WM_HOTKEY && m.WParam.ToInt32() == hotkey )
{
ShowWindow();
}
else if( m.Msg == WM_SYSCOMMAND )
{
if( m.WParam.ToInt32() == SC_CLOSE || m.WParam.ToInt32() == SC_MINIMIZE )
{
ShowWindow();
return;
}
}
base.WndProc (ref m);
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace SwitchDesktop
{
/// <summary>
/// DesktopName 的摘要说明。
/// </summary>
public class DesktopName : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public DesktopName()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
public new DialogResult ShowDialog( System.Windows.Forms.IWin32Window owner )
{
base.ShowDialog( owner );
if( IsOK )
return DialogResult.OK;
else
return DialogResult.No;
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 23);
this.button1.TabIndex = 0;
this.button1.Text = "OK";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// DesktopName
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(210, 37);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "DesktopName";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Please Name of New Desktop";
this.ResumeLayout(false);
}
#endregion
private bool IsOK = false;
public string strDesktopName = string.Empty;
private void button1_Click(object sender, System.EventArgs e)
{
if( textBox1.Text.Trim() != string.Empty )
{
IsOK = true;
strDesktopName = textBox1.Text.Trim();
this.Close();
}
else
IsOK = false;
}
}
}
点击这里下载编译好的程序