C# 项目实战(经典)

C# 项目实战(经典)好久没写原创了,今天心血来潮,打算写一篇,关于特定的知识点之前写过很多,今天呢就写一篇综合性的偏应用的一个小的项目实战.01—重要的知识点本篇内容基于CM框架编写,涉及以下知识点:①CM框架下一个控件附加多个事件:cal:Message.Attach=”[EventMouseRightButtonDown]=[datagrid_MouseRightButton…

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

01

重要的知识点

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

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

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

②datagrid添加行号:

DG.LoadingRow += new EventHandler<DataGridRowEventArgs>(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:

/// <summary>
        /// 图片加载显示完成后释放
        /// </summary>
        /// <param name="imagePath"></param>
        /// <returns></returns>
        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:

/// <summary>
        /// bitmap转换成imagesoure
        /// </summary>
        /// <param name="hObject"></param>
        /// <returns></returns>
        [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并实现互斥

<DataGridTemplateColumn  Header="Radio" MinWidth="50">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <RadioButton IsChecked="{Binding GroupSelect,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" GroupName="Mutex" IsEnabled="{Binding RadioEnabled}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>

02

操作演示

e5a15dbfc61eef6d47bace8f7c418e97.gif

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

小编微信:mm1552923   

公众号:dotNet编程大全    

今天的文章C# 项目实战(经典)分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/8505.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注