Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
hi, i'm studying MFC and had a problem with cursor
I create an image control program which can zoom, rotate, panning and swapping images
I tried to change the cursor's shape for each function of my program, but it just work when no image in cell! when there are image in cell, the cursor just disappear.
Can anyone help me out. (sorry for my Eng :( )
Here is part of my code
C++
void CImageEdittingView::SwapMouseDown(int fMouseX, int fMouseY)
{
    

    CImageEdittingDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    if (fMouseX > pDoc->m_layout->GetInitX()&& 
        fMouseX < (pDoc->m_layout->GetInitX() + pDoc->m_layout->GetWidth()))

        if (fMouseY > pDoc->m_layout->GetInitY() &&
            fMouseY < (pDoc->m_layout->GetInitY() + pDoc->m_layout->GetHeigh()))
        {
            HCURSOR hSwapClose = LoadCursorFromFile("res\\cursor_drag_hand.ico");
            ::SetCursor(hSwapClose);
                    
            //SetClassLong(m_hWnd, GCL_HCURSOR,(LONG)hZoomOut);
            ShowCursor(TRUE);
        }
}

void CImageEdittingView::SwapMouseUp(int fMouseX, int fMouseY)
{
    //lay ra layout
    CImageEdittingDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;
    
    if (fMouseX > pDoc->m_layout->GetInitX()&& 
        fMouseX < (pDoc->m_layout->GetInitX() + pDoc->m_layout->GetWidth()))

        if (fMouseY > pDoc->m_layout->GetInitY() &&
            fMouseY < (pDoc->m_layout->GetInitY() + pDoc->m_layout->GetHeigh()))
        {
            HCURSOR hSwapOpen = LoadCursorFromFile("res\\cursor_hand.ico");
            ::SetCursor(hSwapOpen);
                    
            //SetClassLong(m_hWnd, GCL_HCURSOR,(LONG)hZoomOut);
            ShowCursor(TRUE);
        }

    Graphics gr(this->GetDC()->m_hDC);
    
    CLayout* layout = pDoc->m_layout;
    int newIndex = layout->GetCellIndexFromPosition(fMouseX, fMouseY);

    if(newIndex >= 0)
    {
        int oldIndex = layout->GetCurrCellIndex();

        layout->Swap(oldIndex, newIndex);

        layout->GetCell(newIndex)->Draw(&gr, GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT));

        layout->GetCell(oldIndex)->Draw(&gr, GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT));

        layout->GetCurrCell()->DrawCellBorder(&gr, CUltil::clActiveBorderColor, GetScrollPos(SB_HORZ), GetScrollPos(SB_VERT));
    }
}
Posted

If this parameter is NULL, the cursor is removed from the screen[^] :)

Try to map and hold your cursors in the resources:
SetCursor(AfxGetApp()->LoadCursor(ID_MOVING_VIEW));

yourApp.rc :
C++
ID_MOVING_VIEW  CURSOR  "res\\MoveView.CUR"

Please think also about the necessity of ::ShowCursor(..) :)
 
Share this answer
 
v2
You should also be mapping the WM_SETCURSOR message where you should do the real selection of the cursor resource.

What you currently have will cause the cursor to flicker constantly between what you want and the views default cursor.
 
Share this answer
 
You should also be mapping the WM_SETCURSOR message where you should do the real selection of the cursor resource.

What you currently have will cause the cursor to flicker constantly between what you want and the views default cursor.
 
Share this answer
 
Your example is incomplete but I assume that you are trying to create your own custom control and implement some kind of dragging operation inside it. If this is the case, then you should establish mouse capture for the time of the drag operation, and then release the mouse capture at the end of dragging. While your control has the mouse capture you can set a cursor and it wont change till you have the capture even if the cursor is outside of your control because during active mouse capture the operating system wont send any WM_SETCURSOR messages to windows under the cursor. For a complete example check out this tip in which you can find a short and complete example to using mouse capture:
Using SetCapture() and ReleaseCapture() correctly (usually during a drag n' drop operation).[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900