www.pudn.com > Fetion.rar > GroupPortraitForm.cs


namespace Imps.Client.Pc 
{ 
    using Imps.Client; 
    using Imps.Client.Core; 
    using Imps.Client.Pc.BizControls; 
    using Imps.Client.Pc.Controls; 
    using Imps.Client.Pc.Properties; 
    using Imps.Client.Resource; 
    using Imps.Client.Utils; 
    using Imps.Utils; 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Drawing; 
    using System.Drawing.Imaging; 
    using System.IO; 
    using System.Windows.Forms; 
 
    public class GroupPortraitForm : XIMDialog 
    { 
        private List _addPortrait; 
        private string _customImagesDir; 
        private string _defaultImagesDir; 
        private List _delPortrait; 
        private IFrameworkWindow _iFrameworkWnd; 
        private string _initDir; 
        private PersonalGroup _personalGroup; 
        private const string _portraitFileEx = "*.jpg"; 
        private Image _resultImg; 
        private Imps.Client.Core.User _user; 
        private XButton buttonAdd; 
        private XButton buttonCancel; 
        private XButton buttonDel; 
        private XButton buttonOK; 
        private IContainer components; 
        private XLabel label1; 
        private listbox_widget_t lbPortrait; 
        private headiconlist_drawer m_headiconlist_drawer = new headiconlist_drawer(); 
        private vscroll_widget m_headiconlist_vscroll = scroll_maker.instance.vscroll(); 
        private int m_index_of_customicon; 
        private int m_index_of_sysicon; 
        private Panel m_pane; 
        protected OpenFileDialog OpenFileDialogPic; 
        private DisplayPortrait pbMyPortrait; 
 
        public GroupPortraitForm(IFrameworkWindow wnd, string DefaultImagesDir, string CustomImagesDir, PersonalGroup group) 
        { 
            this._personalGroup = group; 
            this._defaultImagesDir = DefaultImagesDir; 
            this._customImagesDir = CustomImagesDir; 
            this.m_headiconlist_drawer.item_height = 0x24; 
            this.lbPortrait = new listbox_widget_t(); 
            this.InitializeComponent(); 
            this.lbPortrait.FormattingEnabled = true; 
            this.lbPortrait.Name = "lbPortrait"; 
            this.lbPortrait.Location = new System.Drawing.Point(this.m_pane.Left + 5, this.buttonAdd.Location.Y); 
            this.lbPortrait.Size = new Size((this.pbMyPortrait.Left - this.m_pane.Left) - 10, this.m_pane.Height - (this.buttonAdd.Location.Y * 2)); 
            this.lbPortrait.TabIndex = 1; 
            this.lbPortrait.BorderStyle = BorderStyle.FixedSingle; 
            this.lbPortrait.compare_func = new listbox_widget.compare_func_type(this.compare_func); 
            this._iFrameworkWnd = wnd; 
            this._user = wnd.AccountManager.CurrentUser; 
            this._addPortrait = new List(); 
            this._delPortrait = new List(); 
            this._initDir = ImpsPathInfo.StartupPath; 
            if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures))) 
            { 
                this._initDir = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); 
            } 
            else if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Personal))) 
            { 
                this._initDir = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
            } 
            this.lbPortrait.SelectedIndexChanged += new EventHandler(this.lbPortrait_SelectedIndexChanged); 
            this.lbPortrait.drawer = this.m_headiconlist_drawer; 
            this.OpenFileDialogPic = new OpenFileDialog(); 
            this.OpenFileDialogPic.Multiselect = false; 
            this.OpenFileDialogPic.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; 
            this.OpenFileDialogPic.InitialDirectory = this._initDir; 
            this.m_pane.Controls.Add(this.lbPortrait); 
            this.m_headiconlist_vscroll.host = this.lbPortrait; 
            this.m_pane.Controls.Add(this.m_headiconlist_vscroll); 
            base.ResumeLayout(false); 
            base.PerformLayout(); 
        } 
 
        private void buttonAdd_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                if (this.OpenFileDialogPic.ShowDialog(this) == DialogResult.OK) 
                { 
                    string fileName = this.OpenFileDialogPic.FileName; 
                    string text = fileName.Substring(fileName.LastIndexOf(@"\") + 1, (fileName.LastIndexOf(".") - fileName.LastIndexOf(@"\")) - 1); 
                    Image icon = ImageHelper.TryGetSquareThumbnailImage(fileName, 0x60); 
                    if (icon == null) 
                    { 
                        this._iFrameworkWnd.UnifiedMessageBox.ShowInfo("未能成功添加群形象:形象文件格式错误"); 
                    } 
                    else 
                    { 
                        int num = 0; 
                        foreach (object obj2 in this.lbPortrait.Items) 
                        { 
                            if (obj2 is headicon) 
                            { 
                                headicon headicon = (headicon) obj2; 
                                if (headicon.iconpath.ToLower().CompareTo(fileName.ToLower()) == 0) 
                                { 
                                    this.lbPortrait.SelectedIndex = num; 
                                    this._iFrameworkWnd.UnifiedMessageBox.ShowInfo("已经存在和当前路径相同的文件"); 
                                    return; 
                                } 
                            } 
                            num++; 
                        } 
                        headicon headicon2 = new headicon(icon, text); 
                        headicon2.iconpath = fileName.ToLower(); 
                        this.lbPortrait.Sorted = false; 
                        int num2 = this.lbPortrait.add(headicon2); 
                        this.lbPortrait.SelectedIndex = num2; 
                        this._addPortrait.Add(headicon2); 
                    } 
                } 
            } 
            catch (ArgumentException) 
            { 
                this._iFrameworkWnd.UnifiedMessageBox.ShowError("无效的图片文件或者文件已损坏,请重新选择!"); 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteException(exception); 
            } 
        } 
 
        private void buttonCancel_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                base.Close(); 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteException(exception); 
            } 
        } 
 
        private void buttonDel_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                if (this.lbPortrait.SelectedIndex != -1) 
                { 
                    headicon item = this.lbPortrait.get_selected_item(); 
                    if (item != null) 
                    { 
                        this._delPortrait.Add(item); 
                        int num = this.lbPortrait.get_selected_index(); 
                        if (num < this.m_index_of_customicon) 
                        { 
                            this.m_index_of_customicon--; 
                        } 
                        this.lbPortrait.Items.Remove(item); 
                        if (num < this.lbPortrait.Items.Count) 
                        { 
                            this.lbPortrait.SelectedIndex = num; 
                        } 
                        else if (num == (this.m_index_of_customicon + 1)) 
                        { 
                            this.lbPortrait.SelectedIndex = this.m_index_of_customicon - 1; 
                        } 
                        else if (0 < num) 
                        { 
                            this.lbPortrait.SelectedIndex = num - 1; 
                        } 
                    } 
                } 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteException(exception); 
            } 
        } 
 
        private void buttonOK_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                if (this._delPortrait.Count > 0) 
                { 
                    foreach (headicon headicon in this._delPortrait) 
                    { 
                        string path = this._customImagesDir + headicon.text + ".dat"; 
                        if (File.Exists(path)) 
                        { 
                            headicon.icon.Dispose(); 
                            File.Delete(path); 
                        } 
                    } 
                } 
                if (this._user.Configuration.UserSetting.SaveMyInfo && (this._addPortrait.Count > 0)) 
                { 
                    if (!Directory.Exists(this._customImagesDir)) 
                    { 
                        Directory.CreateDirectory(this._customImagesDir); 
                    } 
                    foreach (headicon headicon2 in this._addPortrait) 
                    { 
                        string str2 = this._customImagesDir + headicon2.text + ".dat"; 
                        if (File.Exists(str2)) 
                        { 
                            int num = 1; 
                            while (true) 
                            { 
                                str2 = (this._customImagesDir + headicon2.text) + num.ToString() + ".dat"; 
                                if (!File.Exists(str2)) 
                                { 
                                    break; 
                                } 
                                num++; 
                            } 
                        } 
                        int width = 0x60; 
                        using (Bitmap bitmap = new Bitmap(width, width)) 
                        { 
                            using (Graphics graphics = Graphics.FromImage(bitmap)) 
                            { 
                                graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, width); 
                                graphics.DrawImage(headicon2.icon, 0, 0); 
                                bitmap.Save(str2, ImageHelper.GetImageCodecInfo(ImageFormat.Jpeg), ImageHelper.JpegParms()); 
                            } 
                        } 
                        headicon2.iconpath = str2; 
                    } 
                } 
                headicon headicon3 = this.lbPortrait.get_selected_item(); 
                Image image = new Bitmap(headicon3.icon); 
                headicon3.icon.Dispose(); 
                this.ResultImg = image; 
                base.Close(); 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteException(exception); 
                base.Close(); 
            } 
        } 
 
        private bool compare_func(object headicon1, object headicon2) 
        { 
            if (!(headicon1 is headicon) || !(headicon2 is headicon)) 
            { 
                return false; 
            } 
            headicon headicon = (headicon) headicon1; 
            headicon headicon3 = (headicon) headicon2; 
            return (0 > headicon.text.CompareTo(headicon3.text)); 
        } 
 
        protected override void Dispose(bool disposing) 
        { 
            if (disposing && (this.components != null)) 
            { 
                this.components.Dispose(); 
            } 
            base.Dispose(disposing); 
        } 
 
        private void GetCustomPortraits() 
        { 
            if (Directory.Exists(this._customImagesDir)) 
            { 
                FileInfo[] files = new DirectoryInfo(this._customImagesDir).GetFiles("*.dat"); 
                if (files.Length > 0) 
                { 
                    foreach (FileInfo info2 in files) 
                    { 
                        if (info2.Length > 0L) 
                        { 
                            Image image = ImpsResources.LoadImage(info2.FullName); 
                            if (image != null) 
                            { 
                                Image icon = ImageHelper.TryGetSquareThumbnailImage(image, 0x60); 
                                int num = info2.Name.LastIndexOf('.'); 
                                headicon headicon = new headicon(icon, info2.Name.Substring(0, (-1 == num) ? info2.Name.Length : num)); 
                                headicon.iconpath = info2.FullName.ToLower(); 
                                this.lbPortrait.add(headicon); 
                            } 
                        } 
                    } 
                } 
            } 
        } 
 
        private void GetDefaultPortraits() 
        { 
            Imps.Client.Resource.Portrait[] defaultGroupPortraits = ImpsPortrait.GetDefaultGroupPortraits(); 
            if (defaultGroupPortraits != null) 
            { 
                for (int i = 0; i < defaultGroupPortraits.Length; i++) 
                { 
                    headicon headicon = new headicon(defaultGroupPortraits[i].Image, defaultGroupPortraits[i].Description); 
                    headicon.iconpath = defaultGroupPortraits[i].Description; 
                    this.lbPortrait.add(headicon); 
                } 
            } 
        } 
 
        public FileInfo[] GetInitPortraits() 
        { 
            FileInfo[] files = null; 
            if (Directory.Exists(this._defaultImagesDir)) 
            { 
                files = new DirectoryInfo(this._defaultImagesDir).GetFiles("*.jpg"); 
            } 
            return files; 
        } 
 
        private void InitializeComponent() 
        { 
            ComponentResourceManager manager = new ComponentResourceManager(typeof(GroupPortraitForm)); 
            this.buttonAdd = new XButton(); 
            this.buttonDel = new XButton(); 
            this.buttonOK = new XButton(); 
            this.buttonCancel = new XButton(); 
            this.label1 = new XLabel(); 
            this.m_pane = new Panel(); 
            this.pbMyPortrait = new DisplayPortrait(); 
            this.m_pane.SuspendLayout(); 
            base.SuspendLayout(); 
            this.buttonAdd.AutoArrangementX = true; 
            this.buttonAdd.AutoSizeToImage = false; 
            this.buttonAdd.BackColor = Color.Transparent; 
            this.buttonAdd.BackgroundImage = (Image) manager.GetObject("buttonAdd.BackgroundImage"); 
            this.buttonAdd.BackgroundImageDisable = (Image) manager.GetObject("buttonAdd.BackgroundImageDisable"); 
            this.buttonAdd.BackgroundImageDown = (Image) manager.GetObject("buttonAdd.BackgroundImageDown"); 
            this.buttonAdd.BackgroundImageHover = (Image) manager.GetObject("buttonAdd.BackgroundImageHover"); 
            this.buttonAdd.ChangeSkin = false; 
            this.buttonAdd.Location = new System.Drawing.Point(0xe8, 9); 
            this.buttonAdd.Name = "buttonAdd"; 
            this.buttonAdd.Size = new Size(0x4b, 0x15); 
            this.buttonAdd.TabIndex = 2; 
            this.buttonAdd.Text = "添加..."; 
            this.buttonAdd.UseVisualStyleBackColor = false; 
            this.buttonAdd.Click += new EventHandler(this.buttonAdd_Click); 
            this.buttonDel.AutoArrangementX = true; 
            this.buttonDel.AutoSizeToImage = false; 
            this.buttonDel.BackColor = Color.Transparent; 
            this.buttonDel.BackgroundImage = (Image) manager.GetObject("buttonDel.BackgroundImage"); 
            this.buttonDel.BackgroundImageDisable = (Image) manager.GetObject("buttonDel.BackgroundImageDisable"); 
            this.buttonDel.BackgroundImageDown = (Image) manager.GetObject("buttonDel.BackgroundImageDown"); 
            this.buttonDel.BackgroundImageHover = (Image) manager.GetObject("buttonDel.BackgroundImageHover"); 
            this.buttonDel.ChangeSkin = false; 
            this.buttonDel.Enabled = false; 
            this.buttonDel.Location = new System.Drawing.Point(0xe8, 0x26); 
            this.buttonDel.Name = "buttonDel"; 
            this.buttonDel.Size = new Size(0x4b, 0x15); 
            this.buttonDel.TabIndex = 3; 
            this.buttonDel.Text = "删除"; 
            this.buttonDel.UseVisualStyleBackColor = false; 
            this.buttonDel.Click += new EventHandler(this.buttonDel_Click); 
            this.buttonOK.Anchor = AnchorStyles.Right | AnchorStyles.Bottom; 
            this.buttonOK.AutoArrangementX = true; 
            this.buttonOK.AutoSizeToImage = false; 
            this.buttonOK.BackColor = Color.Transparent; 
            this.buttonOK.BackgroundImageDisable = null; 
            this.buttonOK.BackgroundImageDown = null; 
            this.buttonOK.BackgroundImageHover = null; 
            this.buttonOK.CausesValidation = false; 
            this.buttonOK.ChangeSkin = true; 
            this.buttonOK.DialogResult = DialogResult.OK; 
            this.buttonOK.Enabled = false; 
            this.buttonOK.Location = new System.Drawing.Point(0xa2, 0x17b); 
            this.buttonOK.Name = "buttonOK"; 
            this.buttonOK.Size = new Size(0x4b, 0x17); 
            this.buttonOK.TabIndex = 4; 
            this.buttonOK.Text = "确定"; 
            this.buttonOK.UseVisualStyleBackColor = false; 
            this.buttonOK.Click += new EventHandler(this.buttonOK_Click); 
            this.buttonCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom; 
            this.buttonCancel.AutoArrangementX = true; 
            this.buttonCancel.AutoSizeToImage = false; 
            this.buttonCancel.BackColor = Color.Transparent; 
            this.buttonCancel.BackgroundImageDisable = null; 
            this.buttonCancel.BackgroundImageDown = null; 
            this.buttonCancel.BackgroundImageHover = null; 
            this.buttonCancel.ChangeSkin = true; 
            this.buttonCancel.DialogResult = DialogResult.Cancel; 
            this.buttonCancel.Location = new System.Drawing.Point(0xf5, 0x17b); 
            this.buttonCancel.Name = "buttonCancel"; 
            this.buttonCancel.Size = new Size(0x4b, 0x17); 
            this.buttonCancel.TabIndex = 5; 
            this.buttonCancel.Text = "取消"; 
            this.buttonCancel.UseVisualStyleBackColor = false; 
            this.buttonCancel.Click += new EventHandler(this.buttonCancel_Click); 
            this.label1.AutoSize = true; 
            this.label1.BorderColor = Color.Empty; 
            this.label1.ButtonBorderStyle = ButtonBorderStyle.None; 
            this.label1.Location = new System.Drawing.Point(0xd1, 0x55); 
            this.label1.Name = "label1"; 
            this.label1.Size = new Size(0x1f, 13); 
            this.label1.TabIndex = 6; 
            this.label1.Text = "预览"; 
            this.m_pane.BackColor = Color.Transparent; 
            this.m_pane.Controls.Add(this.pbMyPortrait); 
            this.m_pane.Controls.Add(this.buttonAdd); 
            this.m_pane.Controls.Add(this.label1); 
            this.m_pane.Controls.Add(this.buttonDel); 
            this.m_pane.Location = new System.Drawing.Point(6, 0x18); 
            this.m_pane.Name = "m_pane"; 
            this.m_pane.Size = new Size(0x144, 0x15d); 
            this.m_pane.TabIndex = 7; 
            this.pbMyPortrait.BackColor = Color.White; 
            this.pbMyPortrait.BorderColor = Color.FromArgb(0xa4, 170, 220); 
            this.pbMyPortrait.Image = Resources.PGDefault; 
            this.pbMyPortrait.Location = new System.Drawing.Point(0xd4, 0x68); 
            this.pbMyPortrait.Name = "pbMyPortrait"; 
            this.pbMyPortrait.ShowSecondBorder = false; 
            this.pbMyPortrait.Size = new Size(0x66, 0x66); 
            this.pbMyPortrait.TabIndex = 7; 
            this.pbMyPortrait.ToolTipText = ""; 
            this.pbMyPortrait.UseHandCursor = false; 
            base.AutoScaleDimensions = new SizeF(6f, 13f); 
            base.AutoScaleMode = AutoScaleMode.Font; 
            base.BaseHeight = 0x1b7; 
            base.BaseWidth = 0x15f; 
            base.ClientSize = new Size(0x159, 0x19d); 
            base.Controls.Add(this.m_pane); 
            base.Controls.Add(this.buttonOK); 
            base.Controls.Add(this.buttonCancel); 
            base.DisplayLocation = new System.Drawing.Point(0x12, 0x26); 
            base.Icon = (Icon) manager.GetObject("$this.Icon"); 
            base.MinimizeBox = false; 
            base.Name = "GroupPortraitForm"; 
            base.Padding = new Padding(6, 0x18, 0x13, 8); 
            base.ShowInTaskbar = false; 
            base.StartPosition = FormStartPosition.CenterParent; 
            base.Text = "群形象"; 
            base.CancelButton = this.buttonCancel; 
            base.Shown += new EventHandler(this.PortraitForm_Shown); 
            this.m_pane.ResumeLayout(false); 
            this.m_pane.PerformLayout(); 
            base.ResumeLayout(false); 
        } 
 
        private void lbPortrait_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            headicon headicon = this.lbPortrait.get_selected_item(); 
            if (headicon == null) 
            { 
                this.buttonDel.Enabled = false; 
                this.buttonOK.Enabled = false; 
            } 
            else 
            { 
                if (this.lbPortrait.get_selected_index() <= this.m_index_of_customicon) 
                { 
                    this.buttonDel.Enabled = false; 
                } 
                else 
                { 
                    this.buttonDel.Enabled = true; 
                } 
                this.pbMyPortrait.Image = headicon.icon; 
                this.label1.Visible = true; 
                this.buttonOK.Enabled = true; 
            } 
        } 
 
        protected override void OnClosing(CancelEventArgs e) 
        { 
            this.lbPortrait.drawer = null; 
            base.OnClosing(e); 
        } 
 
        protected override void OnCreateControl() 
        { 
            this.lbPortrait.Location = new System.Drawing.Point(8, this.buttonAdd.Location.Y); 
            base.OnCreateControl(); 
        } 
 
        private void PortraitForm_Shown(object sender, EventArgs e) 
        { 
            try 
            { 
                this.m_index_of_sysicon = this.lbPortrait.add_separator("系统默认群形象"); 
                this.GetDefaultPortraits(); 
                this.lbPortrait.SelectedIndex = 0; 
                this.m_index_of_customicon = this.lbPortrait.Items.Count; 
                this.m_index_of_customicon = this.lbPortrait.add_separator("自定义群形象"); 
                this.GetCustomPortraits(); 
                this.label1.Visible = true; 
                this.lbPortrait.Sorted = true; 
                if (this._personalGroup != null) 
                { 
                    this.pbMyPortrait.Image = (Image) this._personalGroup.GroupInfo.Portrait; 
                } 
                else 
                { 
                    this.pbMyPortrait.Image = ImpsPortrait.GetDefaultGroupPortrait(0x60, 0x60); 
                } 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteException(exception); 
            } 
        } 
 
        private void uploadPortrait_ImpsError(object sender, ImpsErrorEventArgs e) 
        { 
            try 
            { 
                AsyncBizOperation operation = (AsyncBizOperation) sender; 
                object[] contextForUi = (object[]) operation.ContextForUi; 
                IFrameworkWindow framework = (IFrameworkWindow) contextForUi[0]; 
                Form owner = (Form) contextForUi[1]; 
                UiErrorHelper.ShowImpsError(framework, owner, e); 
            } 
            catch (Exception exception) 
            { 
                ClientLogger.WriteException("上传群形象", exception); 
            } 
        } 
 
        public Image ResultImg 
        { 
            get 
            { 
                return this._resultImg; 
            } 
            set 
            { 
                this._resultImg = value; 
            } 
        } 
    } 
}