




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
在这里我们将实现的是C#创建不规则窗体的几种方式,包括自定义窗体,不规则图形等等。希望对大家有所帮助。AD: 现在,C#创建不规则窗体不是一件难事,下面总结一下:一、自定义窗体一般为规则的图形,如圆、椭圆等。做法:重写Form1_Paint事件(Form1是窗体的名字),最简单的一种情况如下:1. System.Drawing.Drawing2D.GraphicsPathshape=newSystem.Drawing.Drawing2D.GraphicsPath(); 2. shape.AddEllipse(0,0,this.Height,this.Width); 3. this.Region=newRegion(shape);即重绘窗体的规则。二、利用背景图片实现1.设置窗体的背景图片,其中背景图片是24位(不包括24)以下的位图(BMP图片),并且要设置TansparencyKey的值,一般为你背景图片的背景色,即创建不规则图片时的底色,一般设为你图片中没有的颜色。这种做法的不好的地方就是背景图片一定要16位或者更低的,而且还要确保客户端的显示。如果监视器的颜色深度设置大于 24 位,则不管 TransparencyKey 属性是如何设置的,窗体的非透明部分都会产生显示问题。若要避免出现这种问题,请确保“显示”控制面板中的监视器颜色深度的设置小于 24 位。当开发具有这种透明功能的应用程序时,请牢记应使您的用户意识到此问题。实现步骤如下:1新建windows application2选择窗体,找到BackgroundImage属性,点击打开新的窗口,选择下面的导入资源文件,选择你的不规则的BMP图片3找到窗体的TansparencyKey,将它设置为你背景图片的背景色(如黄色)4找到窗体的FormBorderStyle,将其设置为none,即不显示标题栏5运行2. 跟背景图片一样的图形,不过是动态加载,遍历位图以实现不规则窗体。它的原理是这样的,在Form的load事件中写方法使得窗体的描绘区域发生改变。实现步骤如下:1建立winform应用程序2找到窗体的Load事件,双击进行编辑3编写方法,主要的代码如下:1. classBitmapRegion 2. 3. publicBitmapRegion() 4. 5. 6. 7. / 8. /Createandapplytheregiononthesuppliedcontrol 9. /创建支持位图区域的控件(目前有button和form) 10. / 11. /TheControlobjecttoapplytheregionto控件 12. /TheBitmapobjecttocreatetheregionfrom位图 13. publicstaticvoidCreateControlRegion(Controlcontrol,Bitmapbitmap) 14. 15. /Returnifcontrolandbitmaparenull 16. /判断是否存在控件和位图 17. if(control=null|bitmap=null) 18. return; 19. 20. /Setourcontrolssizetobethesameasthebitmap 21. /设置控件大小为位图大小 22. control.Width=bitmap.Width; 23. control.Height=bitmap.Height; 24. /CheckifwearedealingwithFormhere 25. /当控件是form时 26. if(controlisSystem.Windows.Forms.Form) 27. 28. /CasttoaFormobject 29. /强制转换为FORM 30. Formform=(Form)control; 31. /Setourformssizetobealittlelargerthatthebitmapjust 32. /incasetheformsborderstyleisnotsettononeinthefirstplace 33. /当FORM的边界FormBorderStyle不为NONE时,应将FORM的大小设置成比位图大小稍大一点 34. form.Width=control.Width; 35. form.Height=control.Height; 36. /Noborder 37. /没有边界 38. form.FormBorderStyle=FormBorderStyle.None; 39. /Setbitmapasthebackgroundimage 40. /将位图设置成窗体背景图片 41. form.BackgroundImage=bitmap; 42. /Calculatethegraphicspathbasedonthebitmapsupplied 43. /计算位图中不透明部分的边界 44. GraphicsPathgraphicsPath=CalculateControlGraphicsPath(bitmap); 45. /Applynewregion 46. /应用新的区域 47. form.Region=newRegion(graphicsPath); 48. 49. /CheckifwearedealingwithButtonhere 50. /当控件是button时 51. elseif(controlisSystem.Windows.Forms.Button) 52. 53. /Casttoabuttonobject 54. /强制转换为button 55. Buttonbutton=(Button)control; 56. /Donotshowbuttontext 57. /不显示buttontext 58. button.Text=; 59. 60. /Changecursortohandwhenoverbutton 61. /改变cursor的style 62. button.Cursor=Cursors.Hand; 63. /Setbackgroundimageofbutton 64. /设置button的背景图片 65. button.BackgroundImage=bitmap; 66. 67. /Calculatethegraphicspathbasedonthebitmapsupplied 68. /计算位图中不透明部分的边界 69. GraphicsPathgraphicsPath=CalculateControlGraphicsPath(bitmap); 70. /Applynewregion 71. /应用新的区域 72. button.Region=newRegion(graphicsPath); 73. 74. 75. / 76. /Calculatethegraphicspaththatrepresentingthefigureinthebitmap 77. /excludingthetransparentcolorwhichisthetopleftpixel. 78. /计算位图中不透明部分的边界 79. / 80. /TheBitmapobjecttocalculateourgraphicspathfrom 81. /Calculatedgraphicspath 82. privatestaticGraphicsPathCalculateControlGraphicsPath(Bitmapbitmap) 83. 84. /CreateGraphicsPathforourbitmapcalculation 85. /创建GraphicsPath 86. GraphicsPathgraphicsPath=newGraphicsPath(); 87. /Usethetopleftpixelasourtransparentcolor 88. /使用左上角的一点的颜色作为我们透明色 89. ColorcolorTransparent=bitmap.GetPixel(0,0); 90. /Thisistostorethecolumnvaluewhereanopaquepixelisfirstfound. 91. /Thisvaluewilldeterminewherewestartscanningfortrailingopaquepixels. 92. /第一个找到点的X 93. intcolOpaquePixel=0; 94. /Gothroughallrows(Yaxis) 95. /偏历所有行(Y方向) 96. for(introw=0;rowbitmap.Height;row+) 97. 98. /Resetvalue 99. /重设 100. colOpaquePixel=0; 101. /Gothroughallcolumns(Xaxis) 102. /偏历所有列(X方向) 103. for(intcol=0;colbitmap.Width;col+) 104. 105. /Ifthisisanopaquepixel,markitandsearchforanymoretrailingbehind 106. /如果是不需要透明处理的点则标记,然后继续偏历 107. if(bitmap.GetPixel(col,row)!=colorTransparent) 108. 109. /Opaquepixelfound,markcurrentposition 110. /记录当前 111. colOpaquePixel=col; 112. /Createanothervariabletosetthecurrentpixelposition 113. /建立新变量来记录当前点 114. intcolNext=col; 115. /Startingfromcurrentfoundopaquepixel,searchforanymoreopaquepixels 116. /trailingbehind,untilatransparentpixelisfoundorminimumwidthisreached 117. /从找到的不透明点开始,继续寻找不透明点,一直到找到或则达到图片宽度 118. for(colNext=colOpaquePixel;colNextbitmap.Width;colNext+) 119. if(bitmap.GetPixel(colNext,row)=colorTransparent) 120. break; 121. /Formarectangleforlineofopaquepixelsfoundandaddittoourgraphicspath 122. /将不透明点加到graphicspath 123. graphicsPath.AddRectangle(newRectangle(colOpaquePixel,row,colNext-colOpaquePixel,1); 124. /Noneedtoscanthelineofopaquepixelsjustfound 125. col=colNext; 126. 127. 128. 129. /Returncalculatedgraphicspath 130. returngraphicsPath; 131. 132. 4运行三、调用类库实现主要就是根据一些坐标,然后根据这些坐标绘制窗体代码如下:1. publicForm3() 2. 3. InitializeComponent(); 4. /创建不规则窗体 5. POINTAPIpoin; 6. poin=newPOINTAPI5; 7. poin0.x=90; 8. poin0.y=90; 9. poin1.x=this.Width; 10. poin1.y=0; 11. poin2.x=Width; 12. poin2.y=this.Height/2; 13. poin3.x=Width/2; 14. poin3.y=Height/2; 15. poin4.x=0; 16. poin4.y=Width; 17. Booleanflag=true; 18. IntPtrhRgn=CreatePolygonRgn(refpoin0,8,1); 19. SetWindowRgn(this.Handle,hRgn,refflag); 20. this.BackColor=Color.BurlyWood; 21. 22. StructLayout(LayoutKind.Sequential) 23. privatestructPOINTAPI 24. 25. internalintx; 26. internalinty; 27. 28. DllImport(gdi32.dll) 29. privatestaticexternIntPtrCreatePolygonRgn(refPOINTAPIlpPoint,intnCount,intnPolyFillMode); 30. DllImport(user32.dll) 31. privatestaticexternIntPtrSetWindowRgn(IntP
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 难点解析人教版八年级上册物理《物态变化》综合训练试卷(含答案详解)
- 护理礼仪期考试题及答案
- 考点解析-苏科版九年级物理上册《简单机械和功》综合测评试卷(含答案详解)
- 湖南对口高考试题及答案
- 重难点解析人教版八年级上册物理《机械运动》章节测试练习题(含答案解析)
- 2025教师编制考试真题及答案
- 考点攻克苏科版八年级物理下册《从粒子到宇宙》单元测评试卷(含答案详解)
- 考点解析-苏科版八年级物理上册《光现象》专题练习试卷(含答案详解版)
- 驻外预算员招聘考试题库及答案
- 2025年病历书写规范管理试题及答案
- 2025年度河北省招聘社区工作者考试模拟卷及答案
- 2025-2026学年辽师大版(三起)(2024)小学英语四年级上册(全册)教学设计(附目录)
- 医院护工消毒隔离培训
- (正式版)DB42∕T 1857-2022 《齐口裂腹鱼人工繁殖技术规范》
- 宅基地入股合作协议合同范本
- 谢好网金字塔教学课件
- 神东选煤管理办法
- 2025年保密教育线上培训试题参考答案
- 2025至2030SDWAN路由器行业项目调研及市场前景预测评估报告
- 人教版二年级数学上册第二单元 1~6的表内乘法素养达标卷(A)(含答案)
- 退休聘用保安协议书范本
评论
0/150
提交评论