43、修改系统的右键菜单
if(args.Length>0)
{
Console.Write(args[0]);
Console.Read();
}
else
{
Process p=Process.GetCurrentProcess();
AssemblyName an=new AssemblyName();
RegistryKey RegKey;
RegKey=Registry.ClassesRoot;
RegKey=RegKey.CreateSubKey("*\\shell\\MyApp");
RegKey.SetValue("","What is this?");
RegKey=Registry.ClassesRoot;
RegKey=RegKey.CreateSubKey("*\\shell\\MyApp\\command");
RegKey.SetValue("",p.MainWindowTitle+" %1");
RegKey.Close();
}
44、屏蔽消息
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
MessageBox.Show("haha");
}
public bool PreFilterMessage(ref System.Windows.Forms.Message m)
{
if (m.Msg >= 513 && m.Msg <= 515)
{
Console.WriteLine("Processing the messages : " + m.Msg);
return true;
}
return false;
}
private void Form1_Load(object sender, System.EventArgs e)
{
Application.AddMessageFilter(this);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Application.RemoveMessageFilter(this);
}
45、使程序只能够运行一个
[StructLayout( LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int GetLastError();
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);
[System.Runtime.InteropServices.DllImport("kernel32")]
private static extern int ReleaseMutex(IntPtr hMutex);
const int ERROR_ALREADY_EXISTS = 0183;
[STAThread]
static void Main()
{
IntPtr hMutex;
hMutex=CreateMutex(null,false,"test");
if (GetLastError()!=ERROR_ALREADY_EXISTS)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("本程序只允许同时运行一个");
ReleaseMutex(hMutex);
}
}
46、设置系统时间
[DllImport( "Kernel32.dll" )]
public static extern void GetLocalTime(SystemTime st );
[DllImport( "Kernel32.dll" )]
public static extern void SetLocalTime(SystemTime st );
[StructLayout( LayoutKind.Sequential)]
public class SystemTime
{
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMilliseconds;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
SystemTime st = new SystemTime();
GetLocalTime(st);
this.Text="The Date and Time is: " ;
this.Text=this.Text+st.wHour.ToString()+":";
this.Text=this.Text+st.wMinute.ToString()+":";
this.Text=this.Text+st.wSecond.ToString()+".";
this.Text=this.Text+st.wMilliseconds.ToString();
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.timer1.Interval=100;
this.timer1.Enabled=true;
this.dateTimePicker1.Value=DateTime.Now;
}
private void button1_Click(object sender, System.EventArgs e)
{
SystemTime st = new SystemTime();
st.wYear=(ushort)this.dateTimePicker1.Value.Year;
st.wMonth=(ushort)this.dateTimePicker1.Value.Month;
st.wDay=(ushort)this.dateTimePicker1.Value.Day;
st.wHour=(ushort)this.dateTimePicker1.Value.Hour;
st.wMinute=(ushort)this.dateTimePicker1.Value.Minute;
st.wSecond=(ushort)this.dateTimePicker1.Value.Second;
SetLocalTime(st);
}
作者:syxChina
本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。