2011-06-16 35 views
2

我必須爲c#winform項目創建一個圖形界面。有一個背景圖片和一組小的透明圖片。用戶必須能夠將這些小圖像放在背景上,選擇它們並自由移動(我也計算它們之間的距離,但這是另一步!)。用c編寫的圖形用戶界面#

我知道我可以做財產以後這樣的:

Flicker free drawing using GDI+ and C#

我也發現了這一點:

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

我的問題是:有沒有更好的或者最簡單的解決方案來實現這一目標?

更新

現在圖像是矩形。如果圖像重疊,沒有問題!

如果小圖像出現問題,我可以用簡單的圓圈(DrawEllipse)切換。重要的一點是用戶可以隨時點擊並移動它們。

+0

圖像是矩形的嗎?他們如何在出現ov重疊的情況下表現出行爲? – 2011-06-16 09:13:00

回答

1

Windows Presentation Fo Foundation(WPF)可能是一個更好的解決方案。它比GDI +更傾向於圖形化,而且由於DirectX支持,速度也更快。

2

我已經找到了解決方案,如何消除從圖片盒閃爍,沒有找到令人滿意的東西......我結束了從XNA框架和精神使用一些2DVector。它工作得很好:)

http://www.xnadevelopment.com/給出了一個很好的解釋如何使用它,它是在遊戲的上下文中解釋。

0

如果你不想要flickr,你最好的選擇是DirectX/XNA/OpenGL。嘗試找到適用於您的應用程序的精靈2d框架。

0

如果您使用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); 
    } 
} 

我希望有助於,大衛。

+0

這是我正在使用的代碼,我還沒有爲你測試它的問題,但它應該工作! – David 2011-06-16 09:49:08