2015-02-23 22 views
-1

我的Windows窗體上有可拖動的圖像。 PNG本身具有透明背景,但它只能在我加載時匹配面板的背景色。還有其他不同顏色的標籤,我希望能夠在拖動時看到。任何人都知道該怎麼做才能做到這一點?Draggable圖像

http://postimg.org/image/d8p4s53pf/

編輯:

試圖使圖片框忠實地TRANSPARANT。拖動的東西已經完成,但背景只是面板的背景,並沒有顯示它通過的控制。

我用過這個,但有點不好。

protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle = 0x00000020; //WS_EX_TRANSPARENT 
      return cp; 
     } 
    } 

    protected override void OnPaintBackground(PaintEventArgs pevent) 
    { 
     //do nothing 
    } 

    protected override void OnMove(EventArgs e) 
    { 
     RecreateHandle(); 
    } 
+0

這不是很清楚你在問什麼。你想知道如何使你的PNG透明,或如何讓你的圖像拖動? – thomasb 2015-02-23 16:06:30

+0

Winforms透明度是一種黑客攻擊,它只需要它背後的任何顏色......如果你發佈了一個鏈接到圖像本身,有人將能夠編輯圖像到你的問題,這可能會讓你的問題更清晰 – Sayse 2015-02-23 16:12:58

+0

這將不行。對於國際象棋棋盤,請考慮在面板背景圖像(或圖像)中設置整個棋盤,並將棋子繪製到表面上。然後,您可以在繪製時使用面板內部的__one__ PictureBox,或者更自然地更新移動件在鼠標移動中的位置。爲此,您需要將移動塊作爲最後一塊放在您的列表中,因此它始終位於頂部。 – TaW 2015-02-23 19:00:09

回答

0

可拖曳Controls因爲WinForms並不難。但一旦他們需要透明度選項是相當受限制:每個必須完全包含在其Parent;這意味着它們都必須是嵌套。在圖形程序中創建一組透明圖層沒有問題。

但是,即使移動一個PictureBox-Pawn跨越其他董事會完全不會與WinForms Controls工作。

但是你的目標很簡單,就是在沒有任何移動控件的情況下做

相反,您可以簡單地將棋子繪製到Paint事件中的控制表面上。我實現了一個初步的解決方案,這裏是Paint事件如此:

List<ChessPiece> pieces = new List<ChessPiece>(); 
int mpIndex = -1;  // index of the moving piece 
Rectangle mmr;  // rectangle where moving piece is drawn 

// board display variables 
int pieceSize = 60; 
int tileSize = 80;  
int borderWidth = 50; 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    foreach(ChessPiece p in pieces) 
    { 
     if (!p.onBoard) continue; // skip the piece? 

     e.Graphics.DrawImage(imageList1.Images[p.pieceIndex], 
          borderWidth + p.col * tileSize, 
          borderWidth + p.row * tileSize); 

    } 
    // if we have a moving piece.. 
    if (mpIndex >= 0 && !pieces[mpIndex].onBoard) 
     e.Graphics.DrawImage(imageList1.Images[pieces[mpIndex].pieceIndex], 
          mmr.Location); 
} 

正如你可以看到它確實不是很長。它使用了一些明顯的變量和一些不太明顯的變量。必須事先準備pieces列表和ImageList。矩形mmr設置在MouseMove

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    if (e.Button == System.Windows.Forms.MouseButtons.Left) 
    { 
     mmr = new Rectangle(e.X - pieceSize/2, 
          e.Y - pieceSize/2, pieceSize, pieceSize); 
     pictureBox1.Invalidate(); // trigger the painting 
    } 
} 

我不會發布MouseUp-Down事件,因爲它們含有大部分代碼,實際上並不屬於移動的透明圖像,但在管理片的名單..

最主要的想法是從一個棋子列表中繪製所有圖像,除非棋子不在棋盤上。移動件是分開繪製的,它的座標不是來自板的位置,而是來自鼠標的位置。

如果在MouseUp事件中我們發現該移動是非法的,我們可以簡單地將其設置爲onBoard而不更改位置並將其繪製在舊位置。

MouseDown我們確定點擊的那一塊,設置它的onBoard=false並設置移動索引mpIndex

作品的類別不長或複雜;

public class ChessPiece 
{ 
    public bool onBoard { get; set; } // is the piece standing on the board? 
    public int row { get; set; }   // row 
    public int col { get; set; }   // column 
    public char piecetype { get; set; } // a little simplistic..not used.. 
    public char color { get; set;}  // .. could be an enumeration! 
    public int pieceIndex { get; set; } // points into imageList with 12 images 

    public ChessPiece(char color, char type, int r, int c, int ind) 
    { onBoard = true; piecetype = type; this.color = color; 
     row = r; col = c; pieceIndex = ind; } 

} 

總而言之的完整代碼是大約180行,未註釋的,但包括60行和單獨沒有任何國際象棋邏輯建立塊..

這裏是前3行的代碼以創建件:

void makePieces() 
{ 
    ChessPiece 
    p = new ChessPiece('W', 'R', 7, 0, 8); pieces.Add(p); 
    p = new ChessPiece('W', 'N', 7, 1, 10); pieces.Add(p); 
    p = new ChessPiece('W', 'B', 7, 2, 9); pieces.Add(p); 
    .. 

所以,移動的透明Png圖形相對於其它Png圖形和一個板圖像是相當簡單.. 這裏是一個屏幕截圖,顯示了黑色的主教在白騎士:

enter image description here

+0

非常好,非常相似,我已經做了。我的拖動功能獲取鼠標位置,然後不斷獲得新的位置。 x = x +(new - old); old = new; 在鼠標上int取消後臺工作,然後將picturebox調整到正確的方塊。 我確實看到你的方法會更有優勢,因爲在無效移動的情況下需要記錄原始位置。我確實獲得了透明度,但閃爍並且更新速度不夠快。 – GaidenFocus 2015-02-24 10:34:50

+0

你現在在拖動一個'PictureBox'嗎?只要不移動的部分不是'控制',拖動就可以工作,但正如你所看到的,根本沒有必要擁有任何'控件'。我使用__only__「PictureBox」。正如我在上面的評論中所寫的那樣,在移動被驗證之前,原始位置不會改變。當然,它不閃爍。其他作品也是PictureBox? – TaW 2015-02-24 10:40:29

+0

我只記得張貼後,我在標籤中存儲位置。如果它無效,它會在標籤中找到原始位置。是的,我有ChessPeice作爲PictureBox的子類。每個peice都是它自己的圖片框,內置了拖動系統(後臺方法可以找到鼠標移動並相應地移動它) – GaidenFocus 2015-02-24 13:27:03