Cómo arrastrar una imagen

En este tema se muestra cómo arrastrar una imagen en la pantalla. Las funciones de arrastre mueven una imagen sin problemas, en color y sin parpadear el cursor. Se pueden arrastrar imágenes enmascaradas y sin máscara.

Lo que necesita saber

Tecnologías

Requisitos previos

  • C/C++
  • Programación de la interfaz de usuario de Windows

Instrucciones

Paso 1: Iniciar la operación de arrastre.

Use la función ImageList_BeginDrag para iniciar una operación de arrastre.

La función definida por el usuario en el siguiente ejemplo de código de C++ está pensada para llamarse en respuesta a un mensaje de botón del mouse, como WM_LBUTTONDOWN. La función determina si el usuario ha realizado clic en el rectángulo delimitador de la imagen. Si el usuario ha realizado clic, la función captura la entrada del mouse, borra la imagen del área de cliente y calcula la posición del punto activo dentro de la imagen. La función establece que la zona activa coincida con el punto activo del cursor del mouse. A continuación, la función comienza la operación de arrastrar llamando a 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; 
} 

Paso 2: Mover la imagen.

La función ImageList_DragMove mueve la imagen a una nueva ubicación.

La función definida por el usuario en el siguiente ejemplo de código de C++ está pensada para llamarse en respuesta al mensaje WM_MOUSEMOVE . Arrastra la imagen a una nueva ubicación.

// 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; 
} 

Paso 3: Finalizar la operación de arrastre.

La función definida por el usuario en el siguiente ejemplo de código de C++ llama a la función ImageList_EndDrag para finalizar la operación de arrastre. A continuación, llama a la función ImageList_DragLeave para desbloquear la ventana y ocultar la imagen de arrastre, lo que permite actualizar la ventana.

// 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; 
} 

Referencia de listas de imágenes

Acerca de las listas de imágenes

Usar listas de imágenes