Ziehen eines Bilds

In diesem Thema wird veranschaulicht, wie Sie ein Bild auf den Bildschirm ziehen. Die Ziehfunktionen verschieben ein Bild reibungslos, in Farbe und ohne Blinken des Cursors. Sowohl maskierte als auch unmaskierte Bilder können gezogen werden.

Wichtige Informationen

Technologien

Voraussetzungen

  • C/C++
  • Programmierung der Windows-Benutzeroberfläche

Anweisungen

Schritt 1: Beginnen Sie den Ziehvorgang.

Verwenden Sie die ImageList_BeginDrag-Funktion , um einen Ziehvorgang zu starten.

Die benutzerdefinierte Funktion im folgenden C++-Codebeispiel soll als Reaktion auf eine Meldung mit gedrückter Maustaste aufgerufen werden, z. B. WM_LBUTTONDOWN. Die Funktion bestimmt, ob der Benutzer innerhalb des umgebenden Rechtecks des Bilds geklickt hat. Wenn der Benutzer geklickt hat, erfasst die Funktion die Mauseingabe, löscht das Bild aus dem Clientbereich und berechnet die Position für den Hotspot innerhalb des Bilds. Die Funktion legt fest, dass der Hot Spot mit dem Hotspot des Mauscursors übereinstimmt. Anschließend beginnt die Funktion mit dem Ziehvorgang, indem ImageList_BeginDrag aufgerufen wird.

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

Schritt 2: Verschieben des Bilds.

Die ImageList_DragMove-Funktion verschiebt das Bild an eine neue Position.

Die benutzerdefinierte Funktion im folgenden C++-Codebeispiel soll als Reaktion auf die WM_MOUSEMOVE-Nachricht aufgerufen werden. Das Bild wird an eine neue Position gezogen.

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

Schritt 3: Beenden des Ziehvorgangs.

Die benutzerdefinierte Funktion im folgenden C++-Codebeispiel ruft die funktion ImageList_EndDrag auf, um den Ziehvorgang zu beenden. Anschließend wird die ImageList_DragLeave-Funktion aufgerufen, um das Fenster zu entsperren und das Bild auszublenden, sodass das Fenster aktualisiert werden kann.

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

Referenz zu Bildlisten

Informationen zu Bildlisten

Verwenden von Bildlisten