.NET图形鬼影 [英] .NET graphics Ghosting

查看:224
本文介绍了.NET图形鬼影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的,我们正在开发在工作中的新应用程序的示例GUI。语言已经决定对我来说,但我不允许使​​用任何第三方DLL或加载项或任何我需要为了使GUI工作尽可能无缝地。

I'm making a sample GUI for a new application we are developing at work. The language has already been decided for me, but I am allowed to use any 3rd party DLLs or add-ins or whatever I need in order to make the GUI work as seamlessly as possible.

他们想得很MAC / Ubuntu的/ VISTA / Windows 7的喜欢,所以我想出了一些非常有趣的控制和pretty的GUI功能。其中之一是一些成长/缩小按钮,附近的大小增加时,您将鼠标放在他们的(它使用的距离公式计算,它需要通过增加尺寸)的屏幕上方。当你把你的鼠标离开控件,他们退缩了。效果看起来非常专业和花哨,所不同的是有一个重影效果随着按钮退缩向下(和按钮到它,因为它们是固定在最髋右侧)。

They want it very mac/ubuntu/vista/Windows 7-like, so I've come up with some very interesting controls and pretty GUI features. One of which are some growing/shrinking buttons near the top of the screen that increase in size when you mouse over them (it uses the distance formula to calculate the size it needs to increase by). When you take your mouse off of the controls, they shrink back down. The effect looks very professional and flashy, except that there is a ghosting effect as the button shrinks back down (and the buttons to the right of it since they are fixed-at-the-hip).

下面是按钮看起来像在设计师:

Here is what the buttons look like in the designer:

下面是我用来做一些code片段:

Here are some code snippets that I am using to do this:

弹出子按钮的下方,当父徘徊

pops child buttons underneath when parent is hovered

Private Sub buttonPop(ByVal sender As Object, ByVal e As System.EventArgs)
    For Each control As System.Windows.Forms.Control In Me.Controls
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.Y > sender.Location.Y AndAlso control.Location.X >= sender.Location.X AndAlso control.Width < sender.Width AndAlso control.Location.X + control.Width < sender.Location.X + sender.Width Then
            control.Visible = True
        End If
    Next
End Sub

尺寸大按钮后恢复正常鼠标离开

size large buttons back to normal after mouse leaves

Private Sub shrinkpop(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim oldSize As Size = sender.Size
    sender.Size = New Size(60, 60)
    For Each control As System.Windows.Forms.Control In Me.Controls
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.X > sender.Location.X AndAlso (Not control.Location.X = control.Location.X + (sender.size.width - oldSize.Width)) Then

            control.Location = New Point(control.Location.X + (sender.size.width - oldSize.Width), control.Location.Y)

        End If
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.Y > sender.Location.Y AndAlso control.Location.X = sender.Location.X AndAlso control.Width < sender.Width Then
            control.Location = New Point(control.Location.X, control.Location.Y + (sender.size.height - oldSize.Height))
            If Windows.Forms.Control.MousePosition.X < control.Location.X Or Windows.Forms.Control.MousePosition.X > control.Location.X + control.Width Then
                control.Visible = False
            End If
        End If
    Next
End Sub

增长的基础上的按钮,鼠标的位置大命令按钮,大小发生在鼠标移动

increase size of large command buttons based on the mouse location in the button, happens on mouse move

    Private Sub buttonMouseMovement(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Dim oldSize As Size = sender.Size
    Dim middle As Point = New Point(30, 30)
    Dim adder As Double = Math.Pow(Math.Pow(middle.X - e.X, 2) + Math.Pow(middle.Y - e.Y, 2), 0.5)
    Dim total As Double = Math.Pow(1800, 0.5)

    adder = (1 - (adder / total)) * 20

    If Not (sender.size.width = 60 + adder And sender.size.height = 60 + adder) Then
        sender.Size = New Size(60 + adder, 60 + adder)
    End If
    For Each control As System.Windows.Forms.Control In Me.Controls
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.X > sender.Location.X AndAlso (Not control.Location.X = control.Location.X + (sender.size.width - oldSize.Width)) Then
            control.Location = New Point(control.Location.X + (sender.size.width - oldSize.Width), control.Location.Y)
        End If
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.Y > sender.Location.Y AndAlso control.Location.X >= sender.Location.X AndAlso control.Width < sender.Width AndAlso control.Location.X + control.Width < sender.Location.X + sender.Width AndAlso (Not control.Location.Y = control.Location.Y + (sender.size.height - oldSize.Height)) Then
            control.Location = New Point(control.Location.X, control.Location.Y + (sender.size.height - oldSize.Height))
        End If
    Next
End Sub

更小的命令按钮增加尺寸

increase size of smaller command buttons

Private Sub SmallButtonMouseMovement(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Dim oldSize As Size = sender.Size
    Dim middle As Point = New Point(22.5, 22.5)
    Dim adder As Double = Math.Pow(Math.Pow(middle.X - e.X, 2) + Math.Pow(middle.Y - e.Y, 2), 0.5)
    Dim total As Double = Math.Pow(1012.5, 0.5)

    adder = (1 - (adder / total)) * 15

    If Not (sender.size.Width = 45 + adder And sender.size.height = 45 + adder) Then
        sender.Size = New Size(45 + adder, 45 + adder)
    End If
    For Each control As System.Windows.Forms.Control In Me.Controls
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.Y > sender.Location.Y AndAlso control.Location.X = sender.location.X AndAlso (Not control.Location.Y = control.Location.Y + (sender.size.height - oldSize.Height)) Then
            control.Location = New Point(control.Location.X, control.Location.Y + (sender.size.height - oldSize.Height))
        End If
    Next
End Sub

下降使命令按钮回到正确的位置,并隐藏他们,如果相应的

decrease puts command buttons back to correct location and hides them if appropriate

    Private Sub SmallShrinkPop(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim oldsize As Size = sender.Size
    If Not (sender.size.width = 45 AndAlso sender.size.height = 45) Then
        sender.size = New Size(45, 45)
    End If

    Dim ChildCounter As Integer = 0

    For Each control As System.Windows.Forms.Control In Me.Controls
        If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.X = sender.location.X AndAlso control.Width = sender.width AndAlso control.Location.Y > sender.location.y Then
            ChildCounter += 1
            control.Location = New Point(control.Location.X, control.Location.Y + (sender.size.height - oldsize.Height))
            If Windows.Forms.Control.MousePosition.X < control.Location.X Or Windows.Forms.Control.MousePosition.X > control.Location.X + control.Width Then
                sender.visible = False
                control.Visible = False
            End If
        End If
    Next
    If (ChildCounter = 0 AndAlso Windows.Forms.Control.MousePosition.Y > sender.Location.Y + sender.Height) Or (Windows.Forms.Control.MousePosition.X < sender.Location.X Or Windows.Forms.Control.MousePosition.X > sender.Location.X + sender.Width) Then
        sender.visible = False
        For Each control As System.Windows.Forms.Control In Me.Controls
            If control.GetType.ToString = "Glass.GlassButton" AndAlso control.Location.X = sender.location.x AndAlso control.Width = sender.width Then
                control.Visible = False
            End If
        Next
    End If
End Sub

我所知道的:

What I know:

  1. 如果表格没有一个背景图片,我不会有重影的问题。
  2. 如果这只是一个正常的按钮,我画,我可能不会有重影的问题。

我所做的,以及如何我试图修复它:

What I've done, and how I've tried to fix it:

  1. 确保表单的doublebuffering打开(这是)
  2. 使用bufferedGraphics类手动doublebuffering(没有帮助或使病情加重)
  3. 说服设计师,它不需要背景图像或pretty的玻璃按钮(不走)
  4. 运行的Invalidate()上载的形式矩形(没有帮助)
  5. 运行刷新()的形式(定重影,但现在整个屏幕闪烁,因为它重新加载图片)
  6. 坐在我的房间的角落,轻声哭到自己(帮助紧张,也没有解决问题)

我所寻找的是这些问题的答案:

What I am looking for are the answers to these questions:

  1. 没有人有任何想法如何摆脱我所描述的重影?我应该专注于不经常改变的大小?我应该把重点放在缓冲背景图像?
  2. 在使用这里还有其他的技术我应该是什么?是否有ActiveX控件,这将是在这个比.NET用户的继承者呢?是否有可能作出的DirectX用户控制使用显卡绘制自己?
  3. 有什么别的我没有想到在这里?

~~~~~~~~~~~~~~~~~~~~~~~~~更新1:11/17/2009上午09点21 ~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~ Update 1: 11/17/2009 9:21 AM ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我先检查,看看他们是否需要检查什么新的价值将是VS什么,他们已经是(code以上更改)重绘改进的绘制方法的效率。这修复了一些重影的,但核心问题仍然有待解决。

I've improved the efficiency of the draw methods by first checking to see if they need to be redrawn by checking what the new values will be vs what they already are(code changed above). This fixes some of the ghosting, however the core problem still remains to be solved.

推荐答案

有您的问题可能速战速决。复制粘贴此code到表单:

There is a possible quick fix for your problem. Paste this code into your form:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      this.IsMdiContainer = true; 
      foreach (Control ctl in this.Controls) {
        if (ctl is MdiClient) {
          ctl.BackgroundImage = Properties.Resources.SampleImage;
          break;
        }
      }
    }
    protected override CreateParams CreateParams {
      get {
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
        return cp;
      }
    }
  }

在XP SP1及以上的风格标志的作品。它的双缓冲你的整个形式,不只是每个人的控制,并应消除你看到重影效果。

The style flag works on XP SP1 and up. It double-buffers your entire form, not just each individual control, and should eliminate the ghosting effect you see.

这篇关于.NET图形鬼影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆