C#代码理解和学习

C#代码理解和学习,c#,.net,c#-4.0,C#,.net,C# 4.0,我是通过潜入C#来学习C#的。我只是被阻止了。我有下面的代码,它有一个菜单项Acquire。当我选择它时,它运行Acquire fine。但我希望在应用程序加载/启动后立即运行acquire 我在哪里做必要的改变 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using

我是通过潜入C#来学习C#的。我只是被阻止了。我有下面的代码,它有一个菜单项Acquire。当我选择它时,它运行Acquire fine。但我希望在应用程序加载/启动后立即运行acquire

我在哪里做必要的改变

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Runtime.InteropServices;

using WIALib; // namespace of imported WIA Scripting COM component

namespace Wia
{
    /// <summary> MainForm for this WIA sample </summary>
    public class MainForm : System.Windows.Forms.Form
    {
        private System.Windows.Forms.MainMenu mainMenu;
        private System.Windows.Forms.MenuItem menuTopFile;
        private System.Windows.Forms.MenuItem menuFileAcquire;
        private System.Windows.Forms.MenuItem menuFileSaveAs;
        private System.Windows.Forms.MenuItem menuFileSep1;
        private System.Windows.Forms.MenuItem menuFileExit;
        private System.Windows.Forms.PictureBox pictureBox;
        private IContainer components;

        public MainForm()
        {
            //
            // Required for Windows Form Designer support
            //

            InitializeComponent();

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                DisposeImage();

                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
            this.mainMenu = new System.Windows.Forms.MainMenu(this.components);
            this.menuTopFile = new System.Windows.Forms.MenuItem();
            this.menuFileAcquire = new System.Windows.Forms.MenuItem();
            this.menuFileSaveAs = new System.Windows.Forms.MenuItem();
            this.menuFileSep1 = new System.Windows.Forms.MenuItem();
            this.menuFileExit = new System.Windows.Forms.MenuItem();
            this.pictureBox = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
            this.SuspendLayout();
            //
            // mainMenu
            //
            this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuTopFile});
            //
            // menuTopFile
            //
            this.menuTopFile.Index = 0;
            this.menuTopFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuFileAcquire,
            this.menuFileSaveAs,
            this.menuFileSep1,
            this.menuFileExit});
            this.menuTopFile.Text = "&File";
            //
            // menuFileAcquire
            //
            this.menuFileAcquire.Index = 0;
            this.menuFileAcquire.Text = "Acquire...";
            this.menuFileAcquire.Click += new System.EventHandler(this.menuFileAcquire_Click);
            //
            // menuFileSaveAs
            //
            this.menuFileSaveAs.Enabled = false;
            this.menuFileSaveAs.Index = 1;
            this.menuFileSaveAs.Text = "Save &As...";
            this.menuFileSaveAs.Click += new System.EventHandler(this.menuFileSaveAs_Click);
            //
            // menuFileSep1
            //
            this.menuFileSep1.Index = 2;
            this.menuFileSep1.Text = "-";
            //
            // menuFileExit
            //
            this.menuFileExit.Index = 3;
            this.menuFileExit.Text = "E&xit";
            this.menuFileExit.Click += new System.EventHandler(this.menuFileExit_Click);
            //
            // pictureBox
            //
            this.pictureBox.Location = new System.Drawing.Point(8, 8);
            this.pictureBox.Name = "pictureBox";
            this.pictureBox.Size = new System.Drawing.Size(488, 357);
            this.pictureBox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox.TabIndex = 0;
            this.pictureBox.TabStop = false;
            //
            // MainForm
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.AutoScroll = true;
            this.BackColor = System.Drawing.SystemColors.Window;
            this.ClientSize = new System.Drawing.Size(520, 377);
            this.Controls.Add(this.pictureBox);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Menu = this.mainMenu;
            this.MinimumSize = new System.Drawing.Size(256, 256);
            this.Name = "MainForm";
            this.Text = "Scanning Device";
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        /// <summary> The main entry point for the application. </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new MainForm());
        }

        private void menuFileExit_Click( object sender, System.EventArgs e )
        {
            Close();
        }

        private void menuFileAcquire_Click( object sender, System.EventArgs e )
        {
            WiaClass        wiaManager    = null;        // WIA manager COM object
            CollectionClass    wiaDevs        = null;        // WIA devices collection COM object
            ItemClass        wiaRoot        = null;        // WIA root device COM object
            CollectionClass    wiaPics        = null;        // WIA collection COM object
            ItemClass        wiaItem        = null;        // WIA image COM object

            try {
                wiaManager = new WiaClass();        // create COM instance of WIA manager

                wiaDevs = wiaManager.Devices as CollectionClass;            // call Wia.Devices to get all devices
                if( (wiaDevs == null) || (wiaDevs.Count == 0) )
                {
                    MessageBox.Show( this, "No WIA devices found!", "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop );
                    Application.Exit();
                    return;
                }

                object selectUsingUI = System.Reflection.Missing.Value;            // = Nothing
                wiaRoot = (ItemClass) wiaManager.Create( ref selectUsingUI );    // let user select device
                if( wiaRoot == null )                                            // nothing to do
                    return;

                // this call shows the common WIA dialog to let the user select a picture:
                wiaPics = wiaRoot.GetItemsFromUI( WiaFlag.SingleImage, WiaIntent.ImageTypeColor ) as CollectionClass;
                if( wiaPics == null )
                    return;

                bool takeFirst = true;                        // this sample uses only one single picture
                foreach( object wiaObj in wiaPics )            // enumerate all the pictures the user selected
                {
                    if( takeFirst )
                    {
                        DisposeImage();                        // remove previous picture
                        wiaItem = (ItemClass) Marshal.CreateWrapperOfType( wiaObj, typeof(ItemClass) );
                        imageFileName = Path.GetTempFileName();                // create temporary file for image
                        Cursor.Current = Cursors.WaitCursor;                // could take some time
                        this.Refresh();
                        wiaItem.Transfer( imageFileName, false );            // transfer picture to our temporary file
                        pictureBox.Image = Image.FromFile( imageFileName );    // create Image instance from file
                        menuFileSaveAs.Enabled = true;                        // enable "Save as" menu entry
                        takeFirst = false;                                    // first and only one done.
                    }
                    Marshal.ReleaseComObject( wiaObj );                    // release enumerated COM object
                }
            }
            catch( Exception ee ) {
                MessageBox.Show( this, "Acquire from WIA Imaging failed\r\n" + ee.Message, "WIA", MessageBoxButtons.OK, MessageBoxIcon.Stop );
                Application.Exit();
            }
            finally {
                if( wiaItem != null )
                    Marshal.ReleaseComObject( wiaItem );        // release WIA image COM object
                if( wiaPics != null )
                    Marshal.ReleaseComObject( wiaPics );        // release WIA collection COM object
                if( wiaRoot != null )
                    Marshal.ReleaseComObject( wiaRoot );        // release WIA root device COM object
                if( wiaDevs != null )
                    Marshal.ReleaseComObject( wiaDevs );        // release WIA devices collection COM object
                if( wiaManager != null )
                    Marshal.ReleaseComObject( wiaManager );        // release WIA manager COM object
                Cursor.Current = Cursors.Default;                // restore cursor
            }
        }

            /// <summary> User selected menu entry to save image. </summary>
        private void menuFileSaveAs_Click( object sender, System.EventArgs e )
        {
            if( pictureBox.Image == null )                // no bitmap exists
                return;

            SaveFileDialog sd = new SaveFileDialog();
            sd.Title = "Save Image As...";
            sd.FileName = "temp.bmp";
            sd.Filter = "Bitmap file (*.bmp)|*.bmp";    // bmp bitmap file format
            if( sd.ShowDialog() != DialogResult.OK )
                return;

            pictureBox.Image.Save( sd.FileName );        // save to file
        }

        /// <summary> Remove image from screen, dispose object and delete the temporary file. </summary>
        private void DisposeImage()
        {
            menuFileSaveAs.Enabled = false;                // disable "Save As" menu entry
            Image oldImg = pictureBox.Image;
            pictureBox.Image = null;                    // empty picture box
            if( oldImg != null )
                oldImg.Dispose();                        // dispose old image (free memory, unlock file)

            if( imageFileName != null ) {                // try to delete the temporary image file
                try {
                    File.Delete( imageFileName );
                }
                catch( Exception )
                { }
            }
        }

        /// <summary> temporary image file. </summary>
        private string imageFileName;
    }
}
使用系统;
使用系统图;
使用系统集合;
使用系统组件模型;
使用System.Windows.Forms;
使用系统数据;
使用System.IO;
使用System.Runtime.InteropServices;
使用WIALib;//导入的WIA脚本COM组件的命名空间
名称空间Wia
{
///此WIA示例的主窗体
公共类主窗体:System.Windows.Forms.Form
{
private System.Windows.Forms.main菜单main菜单;
private System.Windows.Forms.MenuItem菜单文件;
private System.Windows.Forms.MenuItem menuFileAcquire;
private System.Windows.Forms.MenuItem menuFileSaveAs;
private System.Windows.Forms.MenuItem menuFileSep1;
private System.Windows.Forms.MenuItem menuFileExit;
private System.Windows.Forms.PictureBox PictureBox;
私人IContainer组件;
公共表格(
{
//
//需要Windows窗体设计器支持
//
初始化组件();
}
/// 
///清理所有正在使用的资源。
/// 
受保护的覆盖无效处置(布尔处置)
{
如果(处置)
{
处置();
if(组件!=null)
{
组件。Dispose();
}
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.components=new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager资源=新的System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.main菜单=新系统.Windows.Forms.main菜单(this.components);
this.menuTopFile=new System.Windows.Forms.MenuItem();
this.menuFileAcquire=new System.Windows.Forms.MenuItem();
this.menuFileSaveAs=new System.Windows.Forms.MenuItem();
this.menuFileSep1=new System.Windows.Forms.MenuItem();
this.menuFileExit=new System.Windows.Forms.MenuItem();
this.pictureBox=new System.Windows.Forms.pictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
这个.SuspendLayout();
//
//主菜单
//
this.main menu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{
此文件为.menuTopFile});
//
//menuTopFile
//
this.menuTopFile.Index=0;
this.menuTopFile.MenuItems.AddRange(新System.Windows.Forms.MenuItem[]{
这是menuFileAcquire,
这个.menuFileSaveAs,
这是menuFileSep1,
此参数为.menuFileExit});
this.menuTopFile.Text=“&File”;
//
//menuFileAcquire
//
this.menuFileAcquire.Index=0;
this.menuFileAcquire.Text=“Acquire…”;
this.menuFileAcquire.Click+=新建System.EventHandler(this.menuFileAcquire\u Click);
//
//menuFileSaveAs
//
this.menuFileSaveAs.Enabled=false;
this.menuFileSaveAs.Index=1;
this.menuFileSaveAs.Text=“另存为…”;
this.menuFileSaveAs.Click+=new System.EventHandler(this.menuFileSaveAs\u Click);
//
//menuFileSep1
//
this.menuFileSep1.Index=2;
this.menuFileSep1.Text=“-”;
//
//菜单文件出口
//
this.menuFileExit.Index=3;
this.menuFileExit.Text=“E&xit”;
this.menuFileExit.Click+=new System.EventHandler(this.menuFileExit\u Click);
//
//图片盒
//
this.pictureBox.Location=新系统.图纸.点(8,8);
this.pictureBox.Name=“pictureBox”;
this.pictureBox.Size=新系统.Drawing.Size(488357);
this.pictureBox.SizeMode=System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox.TabIndex=0;
this.pictureBox.TabStop=false;
//
//主要形式
//
this.AutoScaleBaseSize=新系统.图纸.尺寸(5,13);
this.AutoScroll=true;
this.BackColor=System.Drawing.SystemColors.Window;
this.ClientSize=新系统.Drawing.Size(520377);
this.Controls.Add(this.pictureBox);
this.Icon=((System.Drawing.Icon)(resources.GetObject($this.Icon));
this.Menu=this.main菜单;
this.MinimumSize=新系统.图纸.尺寸(256256);
this.Name=“MainForm”;
此.Text=“扫描设备”;
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).EndInit();
此选项为.resume布局(false);
这个。执行布局();
}
#端区
///应用程序的主要入口点。
[状态线程]
静态void Main()
{
运行(新的MainForm());
}
私有无效菜单退出\单击(对象发送者,System.EventArgs e)
{
Close();
}
私有无效菜单获取\单击(对象发送者,System.EventArgs e)
{
WiaClass wiaManager=null;//WIA管理器COM对象
CollectionClass wiaDevs=null;