C# 项目实战(经典)

    好久没写原创了,今天心血来潮,打算写一篇,关于特定的知识点之前写过很多,今天呢就写一篇综合性的偏应用的一个小的项目实战.

01

重要的知识点

本篇内容基于CM框架编写,涉及以下知识点:

① CM框架下一个控件附加多个事件:

cal:Message.Attach="[Event MouseRightButtonDown]=[datagrid_MouseRightButtonDown($source,$eventArgs)];[Event LoadingRow]=[DG_LoadingRow($source,$eventArgs)]"

②datagrid添加行号:

DG.LoadingRow += new EventHandler(DG_LoadingRow);
public void DG_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

③datagrid右键点击添加菜单:

public void datagrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            dGrid = (System.Windows.Controls.DataGrid)sender;
            menu1 = new System.Windows.Controls.ContextMenu();
            System.Windows.Controls.MenuItem menuitemFunc1 = new System.Windows.Controls.MenuItem();
            System.Windows.Controls.MenuItem menuitemFunc2 = new System.Windows.Controls.MenuItem();
            System.Windows.Controls.MenuItem menuitemFunc3 = new System.Windows.Controls.MenuItem();
            menuitemFunc1.Header = "移动到此位置";
            menuitemFunc2.Header = "删除此行信息";
            menuitemFunc3.Header = "导出数据";
            menuitemFunc1.Click += MoveToPostion_Click;
            menuitemFunc2.Click += DeleteRow_Click;
            menuitemFunc3.Click += ExportData_Click;
            menu1.Items.Add(menuitemFunc1);
            menu1.Items.Add(menuitemFunc2);
            menu1.Items.Add(menuitemFunc3);
            menu1.StaysOpen = true;
        }

④浏览选择文件路径和浏览选择文件

public void Load()
        {
            //lblStr = "you hit mine! i am lable";
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "图片|*.jpg;*.jpeg;*.bmp;*.png;*.gif";
            openFileDialog1.FilterIndex = 1;//当前使用第二个过滤字符串
            openFileDialog1.RestoreDirectory = true;//对话框关闭时恢复原目录
            openFileDialog1.Multiselect = false;
            openFileDialog1.Title = "选择文件";
            try
            {
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    System.Windows.MessageBox.Show("你选择了文件" + openFileDialog1.FileName);


                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }


        }
public void BrowseSavePath()
        {
            FolderBrowserDialog browserDialog = new FolderBrowserDialog();
            browserDialog.Description = "请选择路径";
            try
            {
                if (browserDialog.ShowDialog() == DialogResult.OK)
                {
                    if (string.IsNullOrEmpty(browserDialog.SelectedPath))
                    {
                        System.Windows.MessageBox.Show("文件夹路径不能为空");
                        return;
                    }
                    BrowseDataSavePath = browserDialog.SelectedPath;
                    DataExport(BrowseDataSavePath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

⑤wpf图片显示的两种操作方式;

方式1:

/// 
        /// 图片加载显示完成后释放
        /// 
        /// 
        /// 
        public static BitmapImage LoadImageFreeze(string imagePath)
        {
            try
            {
                BitmapImage bitmap = new BitmapImage();
                if (File.Exists(imagePath))
                {
                    bitmap.BeginInit();
                    bitmap.CacheOption = BitmapCacheOption.OnLoad;


                    using (Stream ms = new MemoryStream(File.ReadAllBytes(imagePath)))
                    {
                        bitmap.StreamSource = ms;
                        bitmap.EndInit();
                        bitmap.Freeze();
                    }
                }
                return bitmap;
            }
            catch (Exception)
            {
                return null;
            }
        }

方式2:

/// 
        /// bitmap转换成imagesoure
        /// 
        /// 
        /// 
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern bool DeleteObject(IntPtr hObject);


        public static ImageSource ChangeBitmapToImageSource(Bitmap bitmap)
        {
            IntPtr hBitmap = bitmap.GetHbitmap();
            ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                hBitmap,
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());


            if (!DeleteObject(hBitmap))
            {
                throw new System.ComponentModel.Win32Exception();
            }
            return wpfBitmap;
        }

⑥datagrid添加RadioButton并实现互斥


                            
                                
                                    
                                
                            
                        

02


操作演示

技术群:添加小编微信并备注进群

小编微信:mm1552923   

公众号:dotNet编程大全    

你可能感兴趣的:(c#,java,windows,.net,xml)