chessweb
chessweb

Reputation: 4645

Change cursor for the duration of a thread

In an MFC application I want to show the wait cursor (hour glass) for as long as a thread is running, but calling

SetCursor(LoadCursor(NULL, IDC_WAIT));

from inside the static ThreadProc member function doesn't have any effect. Any help?

Thanks, RSel

Edit

Figured it out. This is one way to do it:

Call LoadCursor in the constructor:

m_cursor = LoadCursor(NULL, IDC_WAIT);

Call SetCursor right before AfxBeginThread:

SetCursor(m_cursor);
AfxBeginThread( ... );

Overwrite OnSetCursor to prevent the cursor from changing back prematurely:

CMyView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{   
    if (m_thread_is_running)
    {
        return false;
    }
    else
    {
        return CView::OnSetCursor(pWnd, nHitTest, message);
    }
}

Upvotes: 3

Views: 1969

Answers (2)

msteiger
msteiger

Reputation: 2044

I haven't checked, but I think that the cursor is updated every time the mouse moves. So you would either call SetCursor() every time you get a WM_SETCURSOR message or you change the default cursor. Note that you should not call LoadCursor() every time you set the cursor.

The default cursor is set in the WNDCLASS struct of a window.

See WM_SETCURSOR for more details.

Upvotes: 1

Goz
Goz

Reputation: 62323

Call it form the main thread when you start the thread then PostMessage a custom message to the main thread when the thread exits and disable the hour glass on that message.

Upvotes: 0

Related Questions