zoukankan      html  css  js  c++  java
  • c# winform 应用编程代码总结 12

    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();
                }

    image

    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 联系作者本人。

  • 相关阅读:
    技术的极限(8): 集成与分离
    心智与认知(1): 反馈循环(Feedback loop)
    证明与计算(6): 身份认证与授权
    证明与计算(5): 从加密哈希函数到一致性哈希
    技术的极限(6): 密码朋克精神(Cypherpunk Spirit)
    翻译(3): NULL-计算机科学上最糟糕的失误
    工具(5): 极简开发文档编写(How-to)
    证明与计算(3): 二分决策图(Binary Decision Diagram, BDD)
    证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)
    翻译(2): How to Write a 21st Century Proof
  • 原文地址:https://www.cnblogs.com/syxchina/p/2197273.html
Copyright © 2011-2022 走看看