共用方式為


如何拖曳影像

本主題示範如何在畫面上拖曳影像。 拖曳函式會以色彩順暢地移動影像,而不會閃爍游標。 可以拖曳已遮罩和未遮罩的影像。

您需要知道的事項

技術

先決條件

  • C/C++
  • Windows 使用者介面程序設計

指示

步驟 1:開始拖曳作業。

使用 ImageList_BeginDrag 函式開始拖曳作業。

下列 C++ 代碼範例中的使用者自訂函式被設計為在接收到滑鼠按鈕按下的訊息時呼叫,例如 WM_LBUTTONDOWN。 函式會判斷使用者是否在影像的邊界矩形內點擊。 如果使用者已點擊,函式會擷取滑鼠輸入,從工作區清除影像,並計算影像中熱點的位置。 函式會將作用點設定為與滑鼠游標作用點一致。 然後,函式會呼叫 ImageList_BeginDrag開始拖曳作業。

// StartDragging - begins dragging a bitmap. 
// Returns TRUE if successful, or FALSE otherwise. 
// hwnd - handle to the window in which the bitmap is dragged. 
// ptCur - coordinates of the cursor. 
// himl - handle to the image list. 
// 
// Global variables 
//     g_rcImage - bounding rectangle of the image to drag. 
//     g_nImage - index of the image. 
//     g_ptHotSpot - location of the image's hot spot. 
//     g_cxBorder and g_cyBorder - width and height of border. 
//     g_cyCaption and g_cyMenu - height of title bar and menu bar. 
extern RECT g_rcImage; 
extern int g_nImage; 
extern POINT g_ptHotSpot; 
 
BOOL StartDragging(HWND hwnd, POINT ptCur, HIMAGELIST himl) 
{ 
    // Return if the cursor is not in the bounding rectangle of 
    // the image. 
    if (!PtInRect(&g_rcImage, ptCur)) 
        return FALSE; 
 
    // Capture the mouse input. 
    SetCapture(hwnd); 
 
    // Erase the image from the client area. 
    InvalidateRect(hwnd, &g_rcImage, TRUE); 
    UpdateWindow(hwnd); 
 
    // Calculate the location of the hot spot, and save it. 
    g_ptHotSpot.x = ptCur.x - g_rcImage.left; 
    g_ptHotSpot.y = ptCur.y - g_rcImage.top; 
 
    // Begin the drag operation. 
    if (!ImageList_BeginDrag(himl, g_nImage, 
            g_ptHotSpot.x, g_ptHotSpot.y)) 
        return FALSE; 
 
    // Set the initial location of the image, and make it visible. 
    // Because the ImageList_DragEnter function expects coordinates to 
    // be relative to the upper-left corner of the given window, the 
    // width of the border, title bar, and menu bar need to be taken 
    // into account. 
    
    ImageList_DragEnter(hwnd, ptCur.x + g_cxBorder, 
        ptCur.y + g_cyBorder + g_cyCaption + g_cyMenu); 
 
    g_fDragging = TRUE; 
 
    return TRUE; 
} 

步驟 2:移動影像。

ImageList_DragMove 函式會將影像移至新位置。

下列 C++ 程式代碼範例中的使用者定義函式用來呼叫以回應 WM_MOUSEMOVE 訊息。 它會將影像拖曳至新的位置。

// MoveTheImage - drags an image to the specified coordinates. 
// Returns TRUE if successful, or FALSE otherwise. 
// ptCur - new coordinates for the image. 
BOOL MoveTheImage(POINT ptCur) 
{ 
    if (!ImageList_DragMove(ptCur.x, ptCur.y)) 
        return FALSE; 
 
    return TRUE; 
} 

步驟 3:結束拖曳作業。

下列C++程式代碼範例中的使用者定義函式會呼叫 ImageList_EndDrag 函式來結束拖曳作業。 然後它會呼叫 ImageList_DragLeave 函式來解除鎖定視窗並隱藏拖曳影像,讓視窗得以更新。

// StopDragging - ends a drag operation and draws the image 
// at its final location. 
// Returns TRUE if successful, or FALSE otherwise. 
// hwnd - handle to the window in which the bitmap is dragged. 
// himl - handle to the image list. 
// ptCur - coordinates of the cursor. 
// 
// Global variable 
//     g_ptHotSpot - location of the image's hot spot. 
 
extern POINT g_ptHotSpot; 
 
BOOL StopDragging(HWND hwnd, HIMAGELIST himl, POINT ptCur) 
{ 
    ImageList_EndDrag(); 
    ImageList_DragLeave(hwnd); 
 
    g_fDragging = FALSE; 
 
    DrawTheImage(hwnd, himl, ptCur.x - g_ptHotSpot.x, 
        ptCur.y - g_ptHotSpot.y); 
 
    ReleaseCapture(); 
    return TRUE; 
} 

影像清單參考資料

關於影像清單

使用影像清單