>> 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. Web负载均衡技术

    Web负载均衡(Load Balancing),简单地说就是给我们的服务器集群分配“工作任务”,而采用恰当的分配方式,对于保护处于后端的Web服务器来说,非常重要. 负载均衡的策略有很多,我们从简单的 ...

  2. LeetCode 707 ——设计链表

    1. 题目 2. 解答 用一个单链表来实现,只有一个头指针.因为不能建立哨兵结点,因此要特别注意是否在头结点处操作. class MyLinkedList { public: struct ListN ...

  3. Redis集群操作手册

    一.原始集群(6节点 3主3从): (1)启动集群: [root@bhz004 ~]# /usr/local/redis/bin/redis-server /usr/local/redis-clust ...

  4. Param指南

    param name标签是在这个播放插件中嵌入的一些功能和播放参数: <param name="playcount" value="1"><! ...

  5. P4018 Roy&October之取石子

    题目背景 Roy和October两人在玩一个取石子的游戏. 题目描述 游戏规则是这样的:共有n个石子,两人每次都只能取 p^kpk 个(p为质数,k为自然数,且 p^kpk 小于等于当前剩余石子数), ...

  6. [Leetcode] Palindrome number 判断回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  7. [Leetcode] 3sum-closest 给定值,最为相近的3数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  8. BZOJ3132 上帝造题的七分钟 【二维树状数组】

    题目 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的 ...

  9. 【BZOJ 1485】[HNOI2009]有趣的数列 卡特兰数

    这个题我是冲着卡特兰数来的所以就没有想到什么dp,当然也没有想到用卡特兰数的原因........... 你只要求出前几项就会发现是个卡特兰数,为什么呢:我们选择地时候要选择奇数位和偶数位,相邻(一对里 ...

  10. CodeForces743E. Vladik and cards 二分+状压dp

    这个题我们可以想象成_---___-----__的一个水柱它具有一遍优一遍行的性质因此可以用来二分最小值len,而每次二分后我们都要验根,we可以把这个水柱想成我们在每个数段里取前一段的那个数后一段有 ...