zoukankan      html  css  js  c++  java
  • c# winform 应用编程代码总结 10

    35、读写ini文件

            [DllImport("kernel32")]
            private static extern int GetPrivateProfileInt(string lpApplicationName,string lpKeyName,int nDefault,string lpFileName);
            [DllImport("kernel32")]
            private static extern bool GetPrivateProfileString(string lpApplicationName,string lpKeyName,string lpDefault,

                                                                                                  StringBuilderlpRetur nedString,int nSize,string lpFileName);
            [DllImport("kernel32")]
            private static extern bool WritePrivateProfileString(string lpApplicationName,string lpKeyName,string lpString,string lpFileName);
            [DllImport("kernel32")]
            private static extern bool GetPrivateProfileSection(string lpAppName,string lpReturnedString,int nSize,string lpFileName);
            [DllImport("kernel32")]
            private static extern bool WritePrivateProfileSection(string lpAppName,string lpString,string lpFileName);

            public const int MAX_PATH = 256;
            public const string FILE_NAME=".\\test.ini";

            private void Form1_Load(object sender, System.EventArgs e)
            {
                if (File.Exists(FILE_NAME))
                {
                    StringBuilder strCaption=new StringBuilder(256);
                    GetPrivateProfileString("Form","Caption","Default Caption",strCaption,strCaption.Capacity,FILE_NAME);
                    this.Text=strCaption.ToString();
                    int myWidth=GetPrivateProfileInt("Form","Width",this.Width,FILE_NAME);
                    this.Width=myWidth;
                    int myHeight=GetPrivateProfileInt("Form","Height",this.Height,FILE_NAME);
                    this.Height=myHeight;
                    int myLeft=GetPrivateProfileInt("Form","Left",this.Left,FILE_NAME);
                    this.Left=myLeft;
                    int myTop=GetPrivateProfileInt("Form","Top",this.Top,FILE_NAME);
                    this.Top=myTop;
                }
            }

            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                string strCaption=this.Text;
                WritePrivateProfileString("Form","Caption",strCaption,FILE_NAME);
                WritePrivateProfileString("Form","Width",this.Width.ToString(),FILE_NAME);
                WritePrivateProfileString("Form","Height",this.Height.ToString(),FILE_NAME);
                WritePrivateProfileString("Form","Left",this.Left.ToString(),FILE_NAME);
                WritePrivateProfileString("Form","Top",this.Top.ToString(),FILE_NAME);
            }

    36、文件关联

            [STAThread]
            static void Main(string[] args
            {
                if (args.Length == 0)  
                {
                    string FileExt;
                    string FileType;
                    string MIMEType;
                    string ExecName;

                    FileExt=".test";
                    FileType="Test File";
                    MIMEType="text/plain";
                    ExecName=Application.ExecutablePath +" %1";

                    RegistryKey RegKey;
                    RegKey=Registry.ClassesRoot;
                    RegKey=RegKey.CreateSubKey(FileExt);
                    //RegKey.OpenSubKey(FileExt, true);
                    RegKey.SetValue("", FileType);
                    RegKey.SetValue("Content Type", MIMEType);
                    RegKey=RegKey.CreateSubKey("shell\\open\\command");
                    RegKey.SetValue("", ExecName);
                    RegKey.Close();
                    return
                }
                strFile=args[0];
                Application.Run(new Form1());
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
                this.richTextBox1.Dock=DockStyle.Fill;
                this.richTextBox1.LoadFile(strFile,RichTextBoxStreamType.PlainText);
            }

    37、获取指定文件的图标

            [System.Runtime.InteropServices.DllImport("shell32")]
            private static extern IntPtr ExtractAssociatedIcon(IntPtr hInst,string lpIconPath,ref int lpiIcon);
            static IntPtr hIcon;
            private void Form1_Load(object sender, System.EventArgs e)
            {
                int IconIndex=0;
                hIcon=ExtractAssociatedIcon(this.Handle,Application.ExecutablePath,ref IconIndex);
            }

            private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
                Icon icon=Icon.FromHandle(hIcon);
                e.Graphics.DrawIcon(icon,10,10);
            }

    本系列文章是作者学习《Visual C#.NET 应用编程150例》(源码)心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。

  • 相关阅读:
    【摘】DB2程序性能
    动态html标签textarea的readOnly属性(JavaScript)
    Html和Xml 文件中的特殊字符 需要转义【转】
    ping的通telnet不正常 服务器之间连接不稳定
    AJAX 和 JSP 10.5(转)实现进度条【转】
    RedHat上部署was7.0
    JPPF 在Windows Server 2008R2上的配置
    银联贷记卡账务账务计算说明
    mybatis源码分析
    使用Neo4j的apoc插件,实现数据从MySQL抽取到Neo4j
  • 原文地址:https://www.cnblogs.com/syxchina/p/2197276.html
Copyright © 2011-2022 走看看