Winform常用操作
2024-10-15 13:54:10
>> 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;
}
}
最新文章
- Vue2.0 + Element-UI + WebAPI实践:简易个人记账系统
- vim IDE平台-打造属于自己的配置
- 【转】七年IT经验的七个总结
- Swift3.0语言教程使用Unicode范式标准化获取字符串
- Nginx SSL配置过程
- HTML5简略介绍
- java和javascript中this区别的浅探讨
- Objective-C 【关于导入类(@class 和 #import的区别)】
- .net单元测试——常用测试方式(异常模拟、返回值测试、参数测试、数据库访问代码测试)
- jQuery学习之过滤选择器
- myeclipse2015复制项目需要修改的地方
- 你是否也在学习ES6 Promise时遇到过这个问题?
- springBoot之配置文件的读取以及过滤器和拦截器的使用
- APIO 2014
- Socket层实现系列 — 信号驱动的异步等待
- TypeError: &#39;range&#39; object does not support item assignment处理方法
- mini2440串口使用
- 13、cookie
- hdu 1864 最大报销额【01背包】
- PCI 设备调试手段