本文介绍了按钮winform C#上的大红十字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am working on windows application and in one of my system it shows red big cross on buttons only when I am using application for long time.
I am using paint event of button for giving it 3D effect.
For that I am using following code:

  [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
        private static extern IntPtr CreateRoundRectRgn
        (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
        );

 private void button1_Paint(object sender, PaintEventArgs e)
        {
          
                var button1 = sender as Button;
                button1.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(2, 2, button1.Width, button1.Height, 2, 2));

                ControlPaint.DrawBorder3D(e.Graphics, ((Control)sender).ClientRectangle, Border3DStyle.Raised);

          
        }

Please provide solution to this porblem. 
Thanks in Advance.





我是什么尝试过:



我试图在paint事件中最后处理图形对象,但它引发了空引用错误。



What I have tried:

I have tried to dispose the graphic object at the end within paint event but it is raising null reference error.

推荐答案

InPtr hRgn = CreateRoundRectRgn(2, 2, button1.Width, button1.Height, 2, 2);
button1.Region = System.Drawing.Region.FromHrgn(hRgn);
ControlPaint.DrawBorder3D(e.Graphics, ((Control)sender).ClientRectangle, Border3DStyle.Raised);
DeleteObject(hRgn);



您还需要添加DllImport:


You'll need to add the DllImport as well:

DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DeleteObject([In] IntPtr hObject);



[edit]拼写错误[/ edit]


[edit]typos[/edit]



这篇关于按钮winform C#上的大红十字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:21