码迷,mamicode.com
首页 > Windows程序 > 详细

Winform常用操作

时间:2016-01-11 18:21:34      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

>> 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);
View Code

>> 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 > 1)
{
//参数0是它本身的路径
String arg0 = CmdArgs[0].ToString();
String arg1 = CmdArgs[1].ToString();
String arg2 = CmdArgs[2].ToString();

MessageBox.Show(arg0);//显示这个程序本身路径
MessageBox.Show(arg1);//显示得到的第一个参数
MessageBox.Show(arg2);//显示得到的第二个参数
}
}
View Code

 >> 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(100, 100, 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)
        {

        }
    }
}
View Code

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

技术分享
    #region 重绘tabControl
        const int CLOSE_SIZE = 15;
      
        /// <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("宋体", 12);
         
            tc.Padding = new System.Drawing.Point(15, 5);

            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("宋体", 12), SystemBrushes.ControlText, myTabRect.X + 0, myTabRect.Y + 5);
                //再画一个矩形框   
                using (Pen p = new Pen(Color.Transparent))
                {
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 5);
                    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, 2))
                {
                    //=============================================   
                    //自己画X   
                    //"\"线   
                    Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
                    Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
                    e.Graphics.DrawLine(objpen, p1, p2);
                    //"/"线   
                    Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
                    Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
                    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 + 3), 2);
                    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
View Code

 >> winForm禁用鼠标滚轮

技术分享
//实现一,禁用全局的鼠标滚轮
public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
    }

    #region IMessageFilter 成员

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 522)
        {
            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;
            }
        }
View Code

 

Winform常用操作

标签:

原文地址:http://www.cnblogs.com/eye-like/p/5121899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!