微软官方MSDN原版Win10系统下载

现在位置: 首页  > 系统教程  > 系统帮助

winform如何重写控件

时间:2024-09-20 23:21:19   

大家好,今天Win10系统之家小编给大家分享「winform如何重写控件」的知识,如果能碰巧解决你现在面临的问题,记得收藏本站或分享给你的好友们哟~,现在开始吧!

生意如何小账本专业版-小商户销售和存货记账系列软件最新版本下载

1.WinForm中DataGridView控件重写实现自定表头样式


  单独建一个类:
public class dgvheaderunite
 {
 public string rowvalue;//重新绘制的文本框内容
 public int col1height, col2height;//第一行行高,第二行行高
 private static sortedlist rowspan = new sortedlist();//取得需要重新绘制的单元格
 private static sortedlist valuelist = new sortedlist();
 /// 
 /// 
 /// datagridview合并单元格(横向)
 /// 
 /// 绘制的datagridview 
 /// 绘制单元格的参数(datagridview的cellpainting事件中参数)
 /// 起始单元格在datagridview中的索引号
 /// 结束单元格在datagridview中的索引号
 /// 重新绘制的文本框内容
 public void meragerowspan(datagridview dgv, datagridviewcellpaintingeventargs cellargs, int mincolindex, int maxcolindex)
 {
 if (cellargs.columnindex < mincolindex="" ||="" cellargs.columnindex=""> maxcolindex) return;
 rectangle rect = new rectangle();
 if (rowspan[cellargs.columnindex] == null)
 {
 //首先判断当前单元格是不是需要重绘的单元格
 //保留此单元格的信息,并抹去此单元格的背景
 rect.x = cellargs.cellbounds.x;
 rect.y = cellargs.cellbounds.y;
 rect.width = cellargs.cellbounds.width;
 rect.height = cellargs.cellbounds.height;
 rowspan.add(cellargs.columnindex, rect);
 valuelist.add(cellargs.columnindex, cellargs.value.tostring());
 if (cellargs.columnindex == maxcolindex)
 merageprint(dgv, cellargs, mincolindex, maxcolindex);
 }
 else
 {
 ispostmerage(dgv, cellargs, mincolindex, maxcolindex);
 }
 }
 /// 
 /// 不是初次单元格绘制
 /// 
 /// 
 /// 
 /// 
 /// 
 public void ispostmerage(datagridview dgv, datagridviewcellpaintingeventargs cellargs, int mincolindex, int maxcolindex)
 {
 //比较单元是否有变化
 rectangle rectargs = (rectangle)rowspan[cellargs.columnindex];
 if (rectargs.x != cellargs.cellbounds.x || rectargs.y != cellargs.cellbounds.y
 || rectargs.width != cellargs.cellbounds.width || rectargs.height != cellargs.cellbounds.height
 || valuelist[cellargs.columnindex] != cellargs.value.tostring())
 {
 rectargs.x = cellargs.cellbounds.x;
 rectargs.y = cellargs.cellbounds.y;
 rectargs.width = cellargs.cellbounds.width;
 rectargs.height = cellargs.cellbounds.height;
 rowspan[cellargs.columnindex] = rectargs;
 valuelist[cellargs.columnindex] = cellargs.value.tostring();
 }
 if (cellargs.columnindex == maxcolindex)
 merageprint(dgv, cellargs, mincolindex, maxcolindex);
 //merageprint(dgv, cellargs, mincolindex, maxcolindex);
 }
 //绘制单元格
 private void merageprint(datagridview dgv, datagridviewcellpaintingeventargs cellargs, int mincolindex, int maxcolindex)
 {
 int width = 0;//合并后单元格总宽度
 int height = cellargs.cellbounds.height;//单元格高度
 for (int i = mincolindex; i <= maxcolindex;="" i++)="" width="" +="((rectangle)rowspan[i]).width;" 合并单元格的位置信息="" rectangle="" rebounds="new" rectangle();="" rebounds.x="((rectangle)rowspan[mincolindex]).x;" rebounds.y="((rectangle)rowspan[mincolindex]).y;" rebounds.width="width;" rebounds.height="height;" using="" (brush="" gridbrush="new" solidbrush(dgv.gridcolor),="" backcolorbrush="new" solidbrush(cellargs.cellstyle.backcolor),="" lightbrush="new" solidbrush(systemcolors.controllightlight)="" )="" {="" 抹去原来的cell背景="" cellargs.graphics.fillrectangle(backcolorbrush,="" rebounds);="" using="" (pen="" gridlinepen="new" pen(gridbrush),="" lightlinepen="new" pen(lightbrush))="" {="" 画出边线="" point="" blpoint="new" point(rebounds.left,="" rebounds.bottom="" -="" 1);//底线左边位置="" point="" brpoint="new" point(rebounds.right="" -="" 1,="" rebounds.bottom="" -="" 1);//底线右边位置="" cellargs.graphics.drawline(gridlinepen,="" blpoint,="" brpoint);//下边线="" point="" tlpoint="new" point(rebounds.left,="" rebounds.top);//上边线左边位置="" point="" trpoint="new" point(rebounds.right,="" rebounds.top);//上边线右边位置="" cellargs.graphics.drawline(gridlinepen,="" tlpoint,="" trpoint);="" 上边线="" point="" tlpoint2="new" point(rebounds.left,="" rebounds.top="" +="" 1);//上边线左边位置="" point="" trpoint2="new" point(rebounds.right,="" rebounds.top="" +="" 1);//上边线右边位置="" cellargs.graphics.drawline(lightlinepen,="" tlpoint2,="" trpoint2);="" 上边白线="" point="" mlpoint="new" point(rebounds.left="" +="" 1,="" rebounds.top="" +="" col1height);//中间线左边位置="" point="" mrpoint="new" point(rebounds.right="" -="" 5,="" rebounds.top="" +="" col1height);//中间线右边位置="" cellargs.graphics.drawline(gridlinepen,="" mlpoint,="" mrpoint);//中间线="" point="" mlpoint2="new" point(rebounds.left="" +="" 1,="" rebounds.top="" +="" col1height="" +="" 1);//中间线左边位置="" point="" mrpoint2="new" point(rebounds.right="" -="" 5,="" rebounds.top="" +="" col1height="" +="" 1);//中间线右边位置="" cellargs.graphics.drawline(lightlinepen,="" mlpoint2,="" mrpoint2);//中间白线="" point="" rtpoint1="new" point(rebounds.right="" -="" 2,="" rebounds.top="" +="" 5);//右边线顶部位置="" point="" rbpoint1="new" point(rebounds.right="" -="" 2,="" rebounds.bottom="" -="" 5);//右边线底部位置="" cellargs.graphics.drawline(gridlinepen,="" rtpoint1,="" rbpoint1);="" 右边线="" point="" rtpoint2="new" point(rebounds.right="" -="" 1,="" rebounds.top="" +="" 5);//右边线顶部位置="" point="" rbpoint2="new" point(rebounds.right="" -="" 1,="" rebounds.bottom="" -="" 6);//右边线底部位置="" cellargs.graphics.drawline(lightlinepen,="" rtpoint2,="" rbpoint2);="" 右边白线="" 计算绘制字符串的位置="" sizef="" sf="cellargs.graphics.measurestring(rowvalue," cellargs.cellstyle.font);="" float="" lstr="(width" -="" sf.width)="" 2;="" float="" rstr="(col1height" -="" sf.height)="" 2;="" 画出文本框="" if="" (rowvalue="" !="" )="" {="" cellargs.graphics.drawstring(rowvalue,="" cellargs.cellstyle.font,="" new="" solidbrush(cellargs.cellstyle.forecolor),="" rebounds.left="" +="" lstr,="" rstr="" +="" 3,="" stringformat.genericdefault);="" }="" for="" (int="" i="mincolindex;" i=""></=><= maxcolindex;="" i++)="" {="" sf="cellargs.graphics.measurestring(valuelist[i].tostring()," cellargs.cellstyle.font);="" rectangle="" rect="(rectangle)rowspan[i];" rect.y="" +="col1height;" rect.height="col2height;" stringformat="" strfmt="new" stringformat();="" strfmt.alignment="stringalignment.center;" strfmt.linealignment="stringalignment.center;" if="" (i=""> mincolindex)
 {
 rtpoint1 = new point(rect.left - 2, rebounds.top + col1height + 3);//右边线顶部位置
 rbpoint1 = new point(rect.left - 2, rebounds.bottom - 4);//右边线底部位置
 cellargs.graphics.drawline(gridlinepen, rtpoint1, rbpoint1); //右边线
 rtpoint2 = new point(rect.left - 1, rebounds.top + col1height + 3);//右边线顶部位置
 rbpoint2 = new point(rect.left - 1, rebounds.bottom - 1 - 4);//右边线底部位置
 cellargs.graphics.drawline(lightlinepen, rtpoint2, rbpoint2); //右边白线
 }
 cellargs.graphics.drawstring(valuelist[i].tostring(), cellargs.cellstyle.font,
 new solidbrush(cellargs.cellstyle.forecolor),
 rect,
 strfmt);
 }
 }
 cellargs.handled = true;
 }
 }
在程序里这样调用:
private void dgv1_cellpainting(object sender, datagridviewcellpaintingeventargs e)
 {
 if (e.rowindex == -1 && ((e.columnindex == 2) || (e.columnindex == 3)))
 {
 e.cellstyle.font = new font(dgvpay.defaultcellstyle.font, fontstyle.bold);
 e.cellstyle.wrapmode = datagridviewtristate.true;
 dgvheaderunite headerunite = new dgvheaderunite();
 headerunite.rowvalue = "预算/结算成本";
 headerunite.col1height = 18;
 headerunite.col2height = 36;
 headerunite.meragerowspan(dgvpay, e, 2, 3);
 }
 if (e.rowindex == -1 && ((e.columnindex == 4) || (e.columnindex == 5) || (e.columnindex == 6)))
 {
 e.cellstyle.font = new font(dgvpay.defaultcellstyle.font, fontstyle.bold);
 e.cellstyle.wrapmode = datagridviewtristate.true;
 dgvheaderunite headerunite = new dgvheaderunite();
 headerunite.rowvalue = "付款明细";
 headerunite.col1height = 18;
 headerunite.col2height = 36;
 headerunite.meragerowspan(dgvpay, e, 4, 6);
 }
 }
你可以试试</=>

以上就是关于「winform如何重写控件」的全部内容,本文讲解到这里啦,希望对大家有所帮助。如果你还想了解更多这方面的信息,记得收藏关注本站~

Win10系统之家文②章,转载请联系本站网管!】

相关文章

  • winform如何重写控件

    winform如何重写控件

    1.WinForm中DataGridView控件重写实现自定表头样式单独建一个类:publicclassdgvheaderunite{publicstringrowvalue;//重新绘制的文本框内容publicintcol1height,col2height;//第一行行高,第二行行高privatestaticsor...
  • 如何安装 activex 控件

    如何安装 activex 控件

    1.如何添加ActiveX控件请打开需要添加ActiveX控件的工作表。显示“控件工具箱”,指向“视图”菜单上的“工具栏”,再单击“控件工具箱”。单击要添加的控件。在工作表上要放置控件的位置单击。将控件拖动到所需的大小。如果要添加...
  • excel表单控件如何使用

    excel表单控件如何使用

    1.Excel表单控件和ACTIVEX控件有什么区别表单控件表单控件是与早期版本的Excel(从Excel5.0版开始)兼容的原始控件。表单控件还适于在XLM宏工作表中使用。如果您希望在不使用VBA代码的情况下轻松引用单元格数据并与其进...
  • java如何重写hashcode

    java如何重写hashcode

    1.java关于方法重写hashcodeequals如何重写toString方法既然Object是所有类的顶级父类,那么在Object中定义的方法所有的类都具备。其中之一就是toStirng()方法。StringtoString():该方法java希望我们重写时返回一个字...