带有c#的图形用户界面

本文关键字:图形用户界面 带有 | 更新日期: 2023-09-27 17:59:21

我必须为c#winform项目创建一个图形界面。有一个背景图像和一组小的透明图片。用户必须能够将这些小图像放在背景上,选择它们并自由移动(我还必须计算它们之间的距离,但这是另一步!)。

我知道我可以做这样的事情:

使用GDI+和C#的无闪烁绘图

我还发现了这个:

http://cs-sdl.sourceforge.net/

我的问题是:有更好或最简单的解决方案来实现这一点吗?

更新

现在图像是矩形的。如果图像重叠就没有问题!

如果小图像是个问题,我可以用简单的圆圈(DrawEllipse)进行切换。重要的一点是用户可以随时点击并移动它们。

带有c#的图形用户界面

我一直在寻找如何消除图片框中闪烁的解决方案,但没有找到令人满意的东西。。。我最终使用了XNA框架和spirits中的一些2DVector。它运行良好:)

http://www.xnadevelopment.com/给出了如何使用它的一个很好的解释,它是在游戏上下文中解释的。

Windows Presentation Fo基金会(WPF)可能是一个更好的解决方案。它比GDI+更倾向于图形化,而且速度也快得多,因为它由DirectX提供支持。

如果你不想要flickr,你最好的选择是DirectX/XNA/ONGGL。试着为你的应用程序找到一个带有精灵的2d框架。

如果要使用WPF,则应使用Canvas作为容器控件。对于图像,您必须在代码隐藏文件中添加这些事件处理程序:

private bool IsDragging = false;
private System.Windows.Point LastPosition;
private void MyImage_MouseDown(object sender, MouseButtonEventArgs e)
{
    // Get the right MyImage
    Image MyImage = sender as Image;
    // Capture the mouse
    if (!MyImage.IsMouseCaptured)
    {
        MyImage.CaptureMouse();
    }
    // Turn the drag mode on
    IsDragging = true;
    // Set the current mouse position to the last position before the mouse was moved
    LastPosition = e.GetPosition(SelectionCanvas);
    // Set this event to handled
    e.Handled = true;
}
private void MyImage_MouseUp(object sender, MouseButtonEventArgs e)
{
    // Get the right MyImage
    Image MyImage = sender as Image;
    // Release the mouse
    if (MyImage.IsMouseCaptured)
    {
        MyImage.ReleaseMouseCapture();
    }
    // Turn the drag mode off
    IsDragging = false;
    // Set this event to handled
    e.Handled = true;
}
private void MyImage_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
    // Get the right MyImage
    Image MyImage = sender as Image;
    // Move the MyImage only when the drag move mode is on
    if (IsDragging)
    {
        // Calculate the offset of the mouse movement
        double xOffset = LastPosition.X - e.GetPosition(SelectionCanvas).X;
        double yOffset = LastPosition.Y - e.GetPosition(SelectionCanvas).Y;
        // Move the MyImage
        Canvas.SetLeft(MyImage, (Canvas.GetLeft(MyImage) - xOffset >= 0.0) && (Canvas.GetLeft(MyImage) + MyImage.Width - xOffset <= SelectionCanvas.ActualWidth) ? Canvas.GetLeft(MyImage) - xOffset : Canvas.GetLeft(MyImage));
        Canvas.SetTop(MyImage, (Canvas.GetTop(MyImage) - yOffset >= 0.0) && (Canvas.GetTop(MyImage) + MyImage.Height - yOffset <= SelectionCanvas.ActualHeight) ? Canvas.GetTop(MyImage) - yOffset : Canvas.GetTop(MyImage));
        // Set the current mouse position as the last position for next mouse movement
        LastPosition = e.GetPosition(SelectionCanvas);
    }
}

我希望这能有所帮助,大卫。