Winform常用操作
>> 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常用操作的更多相关文章
- Winform常用开发模式第一篇
Winform常用开发模式第一篇 上一篇博客最后我提到“异步编程模型”(APM),之后本来打算整理一下这方面的材料然后总结一下写篇文章与诸位分享,后来在整理的过程中不断的延伸不断地扩展,发现完全偏离了 ...
- 【三】用Markdown写blog的常用操作
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...
- php模拟数据库常用操作效果
test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...
- Mac OS X常用操作入门指南
前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右) =鼠标左键 control+按下 ...
- mysql常用操作语句
mysql常用操作语句 1.mysql -u root -p 2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...
- nodejs配置及cmd常用操作
一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...
- Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理
摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■ 详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
随机推荐
- LeetCode 33——搜索旋转排序数组
1. 题目 2. 解答 2.1. 方法一 直接进行二分查找,在判断查找方向的时候详细分类. 当 nums[mid] < target 时, 若 nums[left] <= nums[mid ...
- 条件随机场CRF
条件随机场(CRF)是给定一组输入随机变量X的条件下另一组输出随机变量Y的条件概率分布模型,其特点是假设输出随机变量构成马尔科夫随机场.实际上是定义在时序数据上的对数线性模型.条件随机场属于判别模型. ...
- UVA 11880 Ball in a Rectangle(数学+平面几何)
Input: Standard Input Output: Standard Output � There is a rectangle on the cartesian plane, with bo ...
- Spring Data学习(一):初识
目录 前言 添加Spring Data 配置pom.xml 配置数据库相关信息(application.properties) 配置数据库信息 配置自动根据实体类在数据库创建表 创建User.java ...
- 健康领域今年开始井喷了,养老地产和私人医生这两个领域目测成为下一轮BAT在健康领域布局的竞争方向
医疗行业做了六年多的时间,今年到了井喷的阶段,腾讯先是入股了丁香园,然后又一亿美金融资挂号网,春雨医生获得5000万美元的C轮融资,这是要上市的节奏.. 从互联网战略上,健康网和医疗网都是做资料刚开始 ...
- 【bzoj3379】[Usaco2004 Open]Turning in Homework 交作业 区间dp
题目描述 数轴上有C个点,每个点有一个坐标和一个访问时间,必须在这个时间后到达这个点才算访问完成.可以在某个位置停留.每在数轴上走一个单位长度消耗一个单位的时间,问:访问所有点并最终到B花费的最小时间 ...
- [Leetcode] 3sum 三数和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- Angular Cookie 读写
var app = angular.module('Mywind',['ui.router']) app.controller('Myautumn',function($scope,$http,$fi ...
- VC++使用CImage在内存中Jpeg转换Bmp图片
VC++中Jpeg与Bmp图片格式互转应该是会经常遇到,Jpeg相比Bmp在图片大小上有很大优势. 本文重点介绍使用现有的CImage类在内存中进行转换,不需要保存为文件,也不需要引入第三方库. Li ...
- 使用命令wsimport生成WebService客户端
使用命令wsimport生成WebService客户端 wsimpost命令有几个重要的参数: -keep:是否生成java源文件 -d:指定输出目录 -s:指定源代码输出目录 -p ...