C# 语法小记

2015-09-17· 3156 次浏览
## 保持窗体最前 ```cs this.TopMost = true; ``` ## 将字符串S转化为Keys.S ```cs Keys s = (Keys)Enum.Parse(typeof(Keys), "S"); ``` ## picBox 上标签透明(窗体设计器) ```cs this.Controls.Add(this.pictureBox1); pictureBox1.Controls.Add(label1); label1.BackColor = Color.Transparent; ``` ## 毛玻璃窗体 ```cs [StructLayout(LayoutKind.Sequential)] public struct MARGINS { public int Left; public int Right; public int Top; public int Bottom; } [DllImport("dwmapi.dll", PreserveSig = false)] static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins); [DllImport("dwmapi.dll", PreserveSig = false)] static extern bool DwmIsCompositionEnabled(); protected override void OnLoad(EventArgs e) { if (DwmIsCompositionEnabled()) { MARGINS m = new MARGINS(); m.Right = m.Left = m.Top = this.Width + this.Height; DwmExtendFrameIntoClientArea(this.Handle, ref m); } base.OnLoad(e); } protected override void OnPaintBackground(PaintEventArgs e) { base.OnPaintBackground(e); if (DwmIsCompositionEnabled()) { e.Graphics.Clear(Color.Black); } } ``` ## 窗体侧边栏隐藏 ```cs private void timerShowHide_Tick(object sender, EventArgs e) { if (this.WindowState == FormWindowState.Normal) { Point cursorPoint; cursorPoint = new Point(Cursor.Position.X, Cursor.Position.Y - 5); //获取鼠标在屏幕的坐标点 if (this.Left == -3 - this.Width)//当窗体在左面隐藏时 cursorPoint = new Point(Cursor.Position.X - 5, Cursor.Position.Y - 5); Rectangle Rects = new Rectangle(this.Left - 8, this.Top - 8, this.Left + this.Width, this.Top + this.Height);//存储当前窗体在屏幕的所在区域 bool prInRect = PtInRect(ref Rects, cursorPoint); if (prInRect) {//当鼠标在当前窗体内 if (this.Top < 0)//窗体的Top属性小于0 this.Top = 0; else if (this.Left < 0)//窗体的Left属性小于0 this.Left = 0; else if (this.Right > Screen.PrimaryScreen.WorkingArea.Width)//窗体的Right属性大于屏幕宽度 this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width; } else { if (this.Top < 5) //当窗体的上边框与屏幕的顶端的距离小于5时 this.Top = -3 - this.Height; //将窗体隐藏到屏幕的顶端 else if (this.Left < 5) //当窗体的左边框与屏幕的左端的距离小于5时 this.Left = -3 - this.Width; //将窗体隐藏到屏幕的左端 else if (this.Right > Screen.PrimaryScreen.WorkingArea.Width - 5)//当窗体的右边框与屏幕的右端的距离小于5时 this.Left = Screen.PrimaryScreen.WorkingArea.Width + 3;//将窗体隐藏到屏幕的右端 } } } ``` ## 进程唯一 ```cs private void RunAProcessAtOnce() { System.Diagnostics.Process[] pProcesses = System.Diagnostics.Process.GetProcessesByName( System.Diagnostics.Process.GetCurrentProcess().ProcessName); if (pProcesses.Length > 1) { Application.Exit(); return; } } ``` ## 鼠标拖动 ```cs //全局变量 Point oldPoint = new Point(0, 0); bool mouseDown = false; //鼠标事件 void Form1_MouseMove(object sender, MouseEventArgs e) { if (mouseDown) { this.Left += (e.X - oldPoint.X); this.Top += (e.Y - oldPoint.Y); left = this.Left; top = this.Top; } } void Form1_MouseDown(object sender, MouseEventArgs e) { oldPoint = e.Location; mouseDown = true; } void Form1_MouseUp(object sender, MouseEventArgs e) { mouseDown = false; } ``` ## Form1窗体初始化不可见 ```cs WindowState : Minimized; ShowInTaskbar : false; Load 事件 :this.visable = false; ``` ## datagridview 某列不允许为空 ```cs if (this.ShowHostKey.CurrentCell.OwningColumn.HeaderText != "名称") { if (this.ShowHostKey.Rows[e.RowIndex].Cells["name"].Value == null) { MessageBox.Show("名称不允许为空!"); e.Cancel = true; } } ``` ## 判断是不是文件(夹)/文件(夹)是否存在 ```cs if(!Directory.Exists("c:\hh"))//如果是文件夹的话 Directory.CreateDirectory("c:\hh"); if(!File.Exists("c:\hh"))//如果是文件的话 File.Create("c:\hh\1.txt"); ``` ## 通过注册表判定是否是第一次运行程序 并添加启动项 ```cs //引用 using System; using System.Diagnostics; using System.Windows.Forms; using Microsoft.Win32; /// /// 添加到注册表 /// /// 执行序号 public void Register(int id) { Path = Process.GetCurrentProcess().MainModule.FileName;//获取程序路径 String xpath = Path.Substring(0, Path.LastIndexOf(@"\"));//XStart 路径 RegistryKey hklm = Registry.LocalMachine;//注册表Machine 列表下 RegistryKey hksoft = Registry.CurrentUser;//注册表User 列表下 RegistryKey run = hklm.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");//访问Run RegistryKey xStart = hksoft.CreateSubKey(@"SOFTWARE\XStart");//新建 XStart 项 switch (id) { case 1: //添加开机启动 if (run != null) run.SetValue("XStart", Path); break; case 2: //删除开机启动 会产生不存在XStart键值异常 try { if (run != null) run.DeleteValue("XStart"); } catch { MessageBox.Show("未设置开机启动!"); } break; case 3: //通过查看是否含有XStart注册表项判断是否是第一次启动,如果是 设置启动项并添加注册表 //当然你也可以进行许多其他的操作 不过,这种方式如果手动删除注册表会让程序认为是第一次启动 //但是相对来说还是比较安全的方式,毕竟大多数人不会自己去操作注册表 if (xStart.GetValue("Path") == null) { Register(1); xStart.SetValue("Path", xpath); } else { //如果更换程序位置 String path = xStart.GetValue("Path").ToString(); if (!(path.Equals(xpath))) { //如果已存在启动项,更新注册表 if(run.GetValue("XStart") != null) Register(1); xStart.SetValue("Path", xpath); //更新XStart 注册表 } } //判断是否开机启动 if (run != null && run.GetValue("XStart") != null) { String R = run.GetValue("XStart").ToString(); if (R.Equals(Process.GetCurrentProcess().MainModule.FileName)) //ischecked = true; //这里是判断是否开机启动 可以用于“设置”里面的“开机启动”属性是否打钩,或者其余用途 } break; } hklm.Close(); hksoft.Close(); if (run != null) run.Close(); if (xStart != null) xStart.Close(); } ```