C#项目实战

编程开发   © 文章版权由 admin 解释,禁止匿名转载

#楼主# 2022-12-24

写的是一篇综合性的偏应用的一个小的项目实战.

本篇内容基于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;
}

沙发
传影 2022-12-24
④浏览选择文件路径和浏览选择文件

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);
}
}
板凳
传影 2022-12-24
⑤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; }
地板
传影 2022-12-24
⑥datagrid添加RadioButton并实现互斥

评论

登录后才可发表内容
  • 主题

    2

  • 帖子

    44

  • 关注者

    0

TA的新帖
Copyright © 2019 凯特网.   Powered by HYBBS 2.3.4  

Runtime:0.1650s Mem:2075Kb