>> c#操作cmd命令

using System.Diagnostics;
private string RunCmd(string command)
{
//实例一个Process类,启动一个独立进程
Process p = new Process(); //Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性: p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.Arguments = "/c " + command; //设定程式执行参数
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口 p.Start(); //启动 //p.StandardInput.WriteLine(command); //也可以用这种方式输入要执行的命令
//p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程式执行的时候会当机 return p.StandardOutput.ReadToEnd(); //从输出流取得命令执行结果 } //例如实现电脑的关机
using System.Diagnostics;
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "shutdown.exe";
ps.Arguments = "-s -t 1";//关机
// ps.Arguments = "-r -t 1";//重启
Process.Start(ps);

>> winForm启动外部.exe文件并传值

启动EXE
string arg1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
string arg2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.WorkingDirectory = Application.StartupPath; //要启动程序路径 p.StartInfo.FileName = "EXE_NAME";//需要启动的程序名
p.StartInfo.Arguments = arg1+" "+arg2;//传递的参数
p.Start();//启动
或者
Process eg = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(exePath,strArgs);//exePath为要启动程序路径,strArgs为要传递的参数
eg.StartInfo = startInfo;
eg.StartInfo.UseShellExecute = false;
eg.Start(); 接收参数 private void Form1_Load(object sender, EventArgs e)
{
String[] CmdArgs= System.Environment.GetCommandLineArgs();
if (CmdArgs.Length > )
{
//参数0是它本身的路径
String arg0 = CmdArgs[].ToString();
String arg1 = CmdArgs[].ToString();
String arg2 = CmdArgs[].ToString(); MessageBox.Show(arg0);//显示这个程序本身路径
MessageBox.Show(arg1);//显示得到的第一个参数
MessageBox.Show(arg2);//显示得到的第二个参数
}
}

>> winForm使用gif图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics; namespace DysncPicTest
{
public partial class Form1 : Form
{
private Image m_imgImage = null;
private EventHandler m_evthdlAnimator = null;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); m_evthdlAnimator = new EventHandler(OnImageAnimate);
Debug.Assert(m_evthdlAnimator != null);
// http://www.cnblogs.com/sosoft/
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (m_imgImage != null)
{
UpdateImage();
e.Graphics.DrawImage(m_imgImage, new Rectangle(, , m_imgImage.Width, m_imgImage.Height));
}
} protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
m_imgImage = Image.FromFile("1.gif"); // 加载测试用的Gif图片
BeginAnimate();
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (m_imgImage != null)
{
StopAnimate();
m_imgImage = null;
}
} private void BeginAnimate()
{
if (m_imgImage == null)
return; if (ImageAnimator.CanAnimate(m_imgImage))
{
ImageAnimator.Animate(m_imgImage,m_evthdlAnimator);
}
} private void StopAnimate()
{
if (m_imgImage == null)
return; if (ImageAnimator.CanAnimate(m_imgImage))
{
ImageAnimator.StopAnimate(m_imgImage,m_evthdlAnimator);
}
} private void UpdateImage()
{
if (m_imgImage == null)
return; if (ImageAnimator.CanAnimate(m_imgImage))
{
ImageAnimator.UpdateFrames(m_imgImage);
}
} private void OnImageAnimate(Object sender,EventArgs e)
{
this.Invalidate();
} private void Form1_Load(object sender, EventArgs e)
{ }
}
}

>> winForm一种重绘tabControl的方法(带有删除功能)

    #region 重绘tabControl
const int CLOSE_SIZE = ; /// <summary>
/// 重绘TabControl方法
/// </summary>
/// <param name="tc">TabControl控件</param>
public static void tabControl_reDraw(TabControl tc)
{
//清空控件
//this.MainTabControl.TabPages.Clear();
//绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样
tc.DrawMode = TabDrawMode.OwnerDrawFixed;
tc.Font = new Font("宋体", ); tc.Padding = new System.Drawing.Point(, ); tc.DrawItem += new DrawItemEventHandler(tabControl_DrawItem);
tc.MouseDown += new MouseEventHandler(tabControl_MouseDown); tc.TabPages.Clear();//清空所有的page
}
static void tabControl_DrawItem(object sender, DrawItemEventArgs e)
{
try
{ TabControl tc = (TabControl)sender;
Rectangle myTabRect = tc.GetTabRect(e.Index); //先添加TabPage属性
e.Graphics.DrawString(tc.TabPages[e.Index].Text,
new Font("宋体", ), SystemBrushes.ControlText, myTabRect.X + , myTabRect.Y + );
//再画一个矩形框
using (Pen p = new Pen(Color.Transparent))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + ), );
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
} //填充矩形框
Color recColor = e.State == DrawItemState.Selected
? Color.DarkOrange : Color.Transparent;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillRectangle(b, myTabRect);
} //画关闭符号
using (Pen objpen = new Pen(Color.Black, ))
{
//=============================================
//自己画X
//"\"线
Point p1 = new Point(myTabRect.X + , myTabRect.Y + );
Point p2 = new Point(myTabRect.X + myTabRect.Width - , myTabRect.Y + myTabRect.Height - );
e.Graphics.DrawLine(objpen, p1, p2);
//"/"线
Point p3 = new Point(myTabRect.X + , myTabRect.Y + myTabRect.Height - );
Point p4 = new Point(myTabRect.X + myTabRect.Width - , myTabRect.Y + );
e.Graphics.DrawLine(objpen, p3, p4); ////=============================================
//使用图片
// Bitmap bt = new Bitmap(Application.StartupPath+"\\Image\\close.png");
//Bitmap bt = new Bitmap(new Image( Application.StartupPath + "\\Image\\close.png",10,10); //Point p5 = new Point(myTabRect.X, 4); //e.Graphics.DrawImage(bt, p5);
//e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, objpen.Brush, p5);
}
e.Graphics.Dispose();
}
catch (Exception)
{ }
}
static void tabControl_MouseDown(object sender, MouseEventArgs e)
{
try
{
TabControl tc = (TabControl)sender;
if (e.Button == MouseButtons.Left)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = tc.GetTabRect(tc.SelectedIndex); myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + ), );
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE; //如果鼠标在区域内就关闭选项卡
bool isClose = x > myTabRect.X && x < myTabRect.Right && y >
myTabRect.Y && y < myTabRect.Bottom;
if (isClose == true)//关闭窗口
{
//tabControl.SelectedTab.Tag
tc.TabPages.Remove(tc.SelectedTab);
//tabPageList.Remove(tc.SelectedTab.Tag.ToString());
//PageListCheck();
}
}
}
catch { }
}
#endregion

>> winForm禁用鼠标滚轮

//实现一,禁用全局的鼠标滚轮
public partial class Form1 : Form, IMessageFilter
{
public Form1()
{
InitializeComponent();
} #region IMessageFilter 成员 public bool PreFilterMessage(ref Message m)
{
if (m.Msg == )
{
return true;
}
else
{
return false;
}
} #endregion private void Form1_Load(object sender, EventArgs e)
{
Application.AddMessageFilter(this);
}
} //实现二,禁用ComBoBox的鼠标滚轮
class comBoBoxEx : System.Windows.Forms.ComboBox
{
public bool isWheel = false;
public string strComB = null;
protected override void OnMouseWheel(System.Windows.Forms.MouseEventArgs e)
{
strComB = Text;
isWheel = true;
} protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseDown(e);
isWheel = false; } protected override void OnSelectedIndexChanged(EventArgs e)
{ base.OnSelectedIndexChanged(e);
if (isWheel)
{
Text = strComB;
}
}
}
//实现三,禁用单个控件的鼠标滚轮(未验证)
private void Form1_Load(object sender, EventArgs e)
{
numericUpDown1.MouseWheel += new MouseEventHandler(numericUpDown1_MouseWheel);
} //取消滚轮事件
void numericUpDown1_MouseWheel(object sender, MouseEventArgs e)
{
HandledMouseEventArgs h = e as HandledMouseEventArgs;
if (h != null)
{
h.Handled = true;
}
}

Winform常用操作的更多相关文章

  1. Winform常用开发模式第一篇

    Winform常用开发模式第一篇 上一篇博客最后我提到“异步编程模型”(APM),之后本来打算整理一下这方面的材料然后总结一下写篇文章与诸位分享,后来在整理的过程中不断的延伸不断地扩展,发现完全偏离了 ...

  2. 【三】用Markdown写blog的常用操作

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

  3. php模拟数据库常用操作效果

    test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...

  4. Mac OS X常用操作入门指南

    前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右)            =鼠标左键 control+按下        ...

  5. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...

  6. nodejs配置及cmd常用操作

    一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...

  7. Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理

    摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■  详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...

  8. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  9. byte数据的常用操作函数[转发]

    /// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...

随机推荐

  1. npm安装不成功的原因

    使用npm安装electron不成功的解决方法 使用npm安装electron不成功的解决方法 根据官网提供的electron的npm安装方法,使用下面的命令进行安装,结果不成功 npm instal ...

  2. UGUI 代码 动态添加 Event Trigger 的事件

    Additionally, if you need more than just the events provided by default, I'd suggest instead attachi ...

  3. Java 多态方法构造器执行方法

    我们参考下面这个例子: 读者可以提前考虑一下,这段程序的输出会是什么. public class Polymorphism { /** * 创建一个类A * 该类中有一个方法draw,以及一个构造方法 ...

  4. lintcode-62-搜索旋转排序数组

    62-搜索旋转排序数组 假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2).给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引 ...

  5. 201621044079 week07-JAVA GUI类

    作业07-Java GUI编程 1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模 ...

  6. 【WebService】——SOAP、WSDL和UDDI

    WebService的三要素:SOAP.WSDL和UDDI.soap用来描述传递信息的格式,wsdl描述如何访问具体的接口,uddi管理.分发查询WebService. 1.SOAP SOAP Sim ...

  7. PHP与webserver【简书看到的】

    很久以前,人们造出来一个机器人,它的英文名字叫web server,中文名叫网页服务器.(为了简写,下文称web server为server) server的工作很简单,就是做内容的分发. 初期的se ...

  8. SQL 取数值小数后两位,但不四舍五入

    select round('1.67789',2,1) /* 1.67*/

  9. fckeditor的简单使用

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEnco ...

  10. Hiberante可配置参数

    ###################### ### Query Language ### ###################### ## define query language consta ...