C# WinForm编程中的一点小收获

 

一:Win Form登录机制的实现
    Main窗体为应用程式主窗体,Login为登录窗体。均为SDI窗体。
    两种实现方式如下:
        1、应用程式入口放在Login窗体,在Login窗体实现登录机制,验证通过则创建Main窗体的实例,并将自身隐藏。
        具体实现:
        ///Step1:验证登录
        ///Step2:通过
            this.hide(); 
            oMain.Show();
        虽然可以实现登录机制,但是Login窗体并没有释放掉,而是被隐藏掉,内存资源未有效利用。这种方式其实是不可取的。
        2、应用程式入口放在Main窗体,在Main函数中创建Login窗体的实例,Login窗体完成登录验证,返回Main窗体,程式继续执行。需要注意的是Login窗体只有验证通过时返回值才为DialogResult.OK,其余时返回DialogResult.None。这样在Main窗体就可以根据返回值判断是否创建Main窗体的实例。
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>

        [STAThread]
        static void Main()
        {
            frmLogin login = new frmLogin();
            login.ShowDialog();
            if(login.DialogResult.Equals(DialogResult.OK))
            {
                login.Close();
                Application.Run(new frmMain());
            }
        }  
二:利用ImageList作为Resource的载体
    Win Form的程式,外部文件是比较烦人的事情,这里采用Resource将外部图片文件加载到程式中。而ImageList是一个不错的选择,使用简单。
    具体实现:
        ///Step1:将图片在可是模式下加载到ImageList中。
        ///Step2:程式中可以采用this.imgLstResource.Images[index]的方式获取其中的Image对象。



About this Entry

This page contains a single entry by Sky published on August 25, 2005 3:21 PM.

.NET下INI配置文件操作类 was the previous entry in this blog.

资源文件的使用与自定义光标的实现 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.