吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1469|回复: 6
收起左侧

[C&C++ 原创] C# Winform获取常规控件&容器控件&特殊容器控件 及其子容器属性代码

[复制链接]
gksj 发表于 2023-10-1 16:13

using System.Windows.Forms;

namespace DianZan.sOptions
{
    class SaveOptions
    {
        public SaveOptions(Form wform)
        {
            Forms = wform;
        }

        private Form Forms { get; set; }

        public string ASaveOption(string filePath, FileOptType fileOptType)
        {
            string rStr = null;
            OptionData.LangList.Clear();
            OptionData.CtrList.Clear();
            GetAllControls(Forms);           //调用查找控件方法

            rStr = SaveFile(filePath, fileOptType);//保存配置文件

            OptionData.LangList.Clear();
            OptionData.CtrList.Clear();
            if (rStr != null)
            {
                return rStr;          //回显提示信息
            }
            return "UnKnowError";
        }

        private void GetAllControls(Control FormControl)
        {

            foreach (Control OneControl in FormControl.Controls)
            {
#if DEBUG
                int listNum = FormControl.Controls.Count;

                string contName = OneControl.Name;

                string contText = OneControl.Text;

                int countNum = OneControl.Controls.Count;

#endif
                string typeName = OneControl.GetType().Name;        //获取控件类型

                switch (typeName)
                {
                    case "StatusStrip":
                        GetUnpackStatusStrip(OneControl);                 //针对StatusStrip特殊控件
                        break;

                    default:
                        GetUnpackStandControl(OneControl);
                        break;
                }
            }

        }

        private void GetUnpackCollectControl(Control TwoControl)
        {
            int countNum = TwoControl.Controls.Count;

            if (countNum > 0)
            {
                foreach (Control ThreeControl in TwoControl.Controls)
                {
                    GetUnpackStandControl(ThreeControl);
                }
            }
        }

        private void GetUnpackStandControl(Control OneControl)
        {

            string typeName = OneControl.GetType().Name;        //获取控件类型
#if DEBUG
            string contName = OneControl.Name;

            string contText = OneControl.Text;
#endif
            switch(typeName)
            {
                case "Button":
                    AddOptionInfo.colButton((Button)OneControl);
                    break;

                case "CheckBox":
                    AddOptionInfo.colCheckBoxx((CheckBox)OneControl);
                    break;

                case "ComboBox":
                    AddOptionInfo.colComboBox((ComboBox)OneControl);
                    break;

                case "Label":
                    AddOptionInfo.colLabel((Label)OneControl);
                    break;

                case "TextBox":
                    AddOptionInfo.colTextBox((TextBox)OneControl);
                    break;

                case "GroupBoxExt":
                    AddOptionInfo.colGroupBoxExt((GroupBoxExt)OneControl);
                    GetUnpackCollectControl(OneControl);
                    break;

            }
        }

        private void GetUnpackStatusStrip(Control control)
        {
#if DEBUG
            string typeName = control.GetType().Name;        //获取控件类型

            string contName = control.Name;

            string contText = control.Text;
#endif
            StatusStrip statusStrip = (StatusStrip)control;                                                 //强转类型,目的是获取数据

            AddOptionInfo.colStatusStrip(statusStrip);                                                        //获取必要数据

            ToolStripItemCollection toolStripItemCollection = statusStrip.Items;                            //强转类型,目的是获取数据

            int countNum = toolStripItemCollection.Count;                                                   //判断是否需要拆包(方便调试)

            if (countNum > 0)
            {
                foreach (ToolStripItem tsi in toolStripItemCollection)
                {
                    GetUnpackToolStripItem(tsi);                                                               //调用拆包方法
                }
            }
        }

        private void GetUnpackToolStripItem(ToolStripItem tst)
        {

            string typeName = tst.GetType().Name;        //获取控件类型

            switch(typeName)
            {
                case "ToolStripStatusLabel":
                    AddOptionInfo.colToolStripStatusLabel((ToolStripStatusLabel)tst);
                    return;         //1级          无需拆包,文本框

                case "ToolStripProgressBar":
                    AddOptionInfo.colToolStripProgressBar((ToolStripProgressBar)tst);
                    return;         //1级          无需拆包,进度条

                case "ToolStripSplitButton":
                    GetUnpackToolStripSplitButton(tst);
                    return;         //1级          拆包,图片+文本菜单按钮    包内容ToolStripDropDownButton

                case "ToolStripDropDownButton":
                    GetUnpackToolStripDropDownButton(tst);
                    return;         //1级-多级   需要递归查找   拆包,单文本菜单按钮   包内容ToolStripDropDownButton

            }

        }

        private void GetUnpackToolStripDropDownButton(ToolStripItem dropDownButton)
        {
#if DEBUG
            string typeName = dropDownButton.GetType().Name;        //获取控件类型

            string contName = dropDownButton.Name;

            string contText = dropDownButton.Text;
#endif
            ToolStripDropDownButton toolStripDropDownButton = (ToolStripDropDownButton)dropDownButton;      //强转类型,目的是获取数据

            AddOptionInfo.colToolStripDropDownButton(toolStripDropDownButton);                                //获取必要数据

            ToolStripItemCollection toolStripItemCollection = toolStripDropDownButton.DropDownItems;        //获取集合

            int countNum = toolStripItemCollection.Count;                                                   //判断是否需要拆包(方便调试)

            if (countNum > 0)
            {
                foreach (ToolStripItem toolStripItem in toolStripItemCollection)
                {
                    GetUnpackToolStripMenuItem(toolStripItem);                                           //调用拆包方法
                }
            }
        }

        private void GetUnpackToolStripMenuItem(ToolStripItem menuItem)
        {
#if DEBUG
            string typeName = menuItem.GetType().Name;                                              //获取控件类型

            string contName = menuItem.Name;

            string contText = menuItem.Text;
#endif
            ToolStripMenuItem toolStripMenuItem = (ToolStripMenuItem)menuItem;                      //强转类型,目的是获取数据

            AddOptionInfo.colToolStripMenuItem(toolStripMenuItem);                                    //获取必要数据

            ToolStripItemCollection toolStripItemCollection = toolStripMenuItem.DropDownItems;      //获取集合

            int countNum = toolStripItemCollection.Count;                                           //判断是否需要拆包(方便调试)

            if (countNum > 0)
            {
                foreach (ToolStripItem toolStripItem in toolStripItemCollection)
                {
                    GetUnpackToolStripMenuItem(toolStripItem);                                                     //调用拆包方法
                }
            }
        }

        private void GetUnpackToolStripSplitButton(ToolStripItem splitButton)
        {
#if DEBUG
            string typeName = splitButton.GetType().Name;                                              //获取控件类型

            string contName = splitButton.Name;

            string contText = splitButton.Text;
#endif
            ToolStripSplitButton toolStripSplitButton = (ToolStripSplitButton)splitButton;          //强转类型,目的是获取数据

            AddOptionInfo.colToolStripSplitButton(toolStripSplitButton);                              //获取必要数据

            ToolStripItemCollection toolStripItemCollection = toolStripSplitButton.DropDownItems;   //获取集合

            int countNum = toolStripItemCollection.Count;                                           //判断是否需要拆包(方便调试)

            if(countNum>0)
            {
                foreach (ToolStripItem toolStripItem in toolStripItemCollection)
                {
                    GetUnpackToolStripMenuItem(toolStripItem);                                                     //调用拆包方法
                }
            }
        }

        private string SaveFile(string filePath, FileOptType fileOptType)
        {
            string rStr = null;

            switch (fileOptType)
            {
                case FileOptType.WriteDefault:
                    rStr = OptionFile.aOptFile(fileOptType);
                    break;
                case FileOptType.WriteLang:
                    rStr = OptionFile.aOptFile(fileOptType);
                    break;
                case FileOptType.WriteOption:
                    rStr = OptionFile.aOptFile(filePath, fileOptType);
                    break;
            }

            return rStr;
        }
    }
}
using System.Windows.Forms;

namespace DianZan.sOptions
{
    class AddOptionInfo
    {
        public static void colButton(Button cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colCheckBoxx(CheckBox cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Checked;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colComboBox(ComboBox cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colGroupBoxExt(GroupBoxExt cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colLabel(Label cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colStatusStrip(StatusStrip cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colTextBox(TextBox cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colToolStripItem(ToolStripItem cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colToolStripDropDownButton(ToolStripDropDownButton cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colToolStripMenuItem(ToolStripMenuItem cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colToolStripStatusLabel(ToolStripStatusLabel cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colToolStripProgressBar(ToolStripProgressBar cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        public static void colToolStripSplitButton(ToolStripSplitButton cl)
        {
            object cTag = cl.Tag;
            string cName = cl.Name;
            string cText = cl.Text;
            object cValue = cl.Text;
            AddData(cTag, cName, cText, cValue);
        }

        private static void AddData(object cTag,string cName,string cText,object cValue)
        {
            //Type typec = con.GetType();
            bool LangSave = false;
            bool OptSave = false;
            if (cTag != null && cTag.Equals("Lang"))
            {
                LangSave = true;                                         //是否包含保存配置参数标志
            }
            if (cTag != null && cTag.Equals("Opt"))
            {
                OptSave = true;                                         //是否包含保存配置参数标志
            }
            if (cTag != null && cTag.Equals("LangOpt"))
            {
                LangSave = true;
                OptSave = true;
            }

            LangOption langOption = new LangOption();
            CtrOption ctrOption = new CtrOption();

            if (LangSave)
            {
                langOption.Name = cName;
                langOption.Text = cText;
                OptionData.LangList.Add(langOption);                //添加语言文件
            }
            if (OptSave)
            {
                ctrOption.Name = cName;
                ctrOption.Value = cValue;
                OptionData.CtrList.Add(ctrOption);                  //添加语言文件
            }

        }
    }
}

虽然方法比较笨,但是最终还是搞定了

免费评分

参与人数 2吾爱币 +8 热心值 +2 收起 理由
笙若 + 1 + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| gksj 发表于 2023-10-1 20:55
fih 发表于 2023-10-1 20:37
可以的,通用的Winform遍历窗体控件,加载和保存配置文件。

这是保存,加载的我还没搞定呢
保存相对简单,只要根据调试信息里面的数据内容写对应代码就行了.
保存用3天,我估计加载的代码要300天,太难了
bulabula_a 发表于 2023-10-1 17:36
fih 发表于 2023-10-1 20:37
可以的,通用的Winform遍历窗体控件,加载和保存配置文件。
yu520 发表于 2023-10-13 08:46
这是啥意思,没怎么看懂,可以详细说一下嘛
tfyicheng 发表于 2023-11-14 10:57
厉害,先mark
wuhao121 发表于 2023-11-16 09:38
C#比C++简单,using自动就可以引用了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-3 09:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表