Mouse Pointer Disappears When You Press F4 to Open the ComboBox Control (326254)



The information in this article applies to:

  • Microsoft Platform Software Development Kit (SDK) 1.0, when used with:
    • the operating system: Microsoft Windows XP
    • the operating system: Microsoft Windows 2000
    • the operating system: Microsoft Windows NT 4.0
    • the operating system: Microsoft Windows Millennium Edition
    • the operating system: Microsoft Windows 98

This article was previously published under Q326254

SYMPTOMS

When a standard ComboBox appears in a Windows application, the mouse pointer disappears when the following sequence of events occurs:
  • First, you click in the ComboBox edit field.
  • Next, you type some text.
  • Last, you press F4 to cause the ComboBox to open (drop down).
If you press F4 and then move the mouse, the pointer reappears.

This behavior does not occur if, after you start to type, you move the mouse and then press F4.

NOTE: The mouse pointer is referred to as a "cursor" in code.

CAUSE

This behavior occurs because the edit portion of the ComboBox control hides the pointer while you type and displays the pointer when the mouse moves.

When the ComboBox control is dropped down, the ComboBox captures the mouse to use its input to select the items that are in the ComboBox. Therefore, the edit portion of the ComboBox control does not receive the mouse message so that it can display the pointer again.

RESOLUTION

To prevent his behavior, you can cause the mouse pointer to appear when you press the F4 key:
  1. Handle the message CBN_DROPDOWN.
  2. Call SendMessage(WM_SETCURSOR,0,0) from inside the dropdown handler function. This causes the pointer to appear when the ComboBox opens.

Examples

A Truncated Example from an MFC Dialog-based Application

CCombofixDlg::CCombofixDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCombofixDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCombofixDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a DestroyIcon in Win32 after this
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCombofixDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCombofixDlg)
	// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCombofixDlg, CDialog)
	//{{AFX_MSG_MAP(CCombofixDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_DROPDOWN(IDC_COMBOMAIN, OnDropdownCombomain)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CCombofixDlg message handlers
void CCombofixDlg::OnDropdownCombomain() 
{
	SendMessage(WM_SETCURSOR,0,0);	
}
				

A Simple Windows Program

#include <windows.h>
#include <commctrl.h>


LPSTR lpClassName = "TestWindow";	//Class of the window
HWND hWndMain,hWndComboBox;
HINSTANCE hInst;
MSG msg;

WNDCLASSEX MyWnd,MyCtrl;


LRESULT CALLBACK MyWndProc(HWND hWnd,
			UINT msg,
			WPARAM wParam,
			LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance,
		HINSTANCE hPrevInstance,
		LPSTR lpCmdLine,
		int nShow)
{

	hInst = hInstance;

	ZeroMemory(&MyWnd,sizeof(MyWnd));

	MyWnd.cbSize = sizeof(MyWnd);
	MyWnd.lpfnWndProc =  MyWndProc;
	MyWnd.hInstance = hInstance;
	MyWnd.lpszClassName = lpClassName;
	MyWnd.hbrBackground =(HBRUSH)(COLOR_WINDOW+1);
	MyWnd.style = CS_VREDRAW |CS_HREDRAW;

	RegisterClassEx(&MyWnd);
	//Make the main window 
	hWndMain=CreateWindowEx(0,lpClassName,"Combo Control Test",
			WS_OVERLAPPEDWINDOW | WS_VISIBLE,
			CW_USEDEFAULT,CW_USEDEFAULT,
			CW_USEDEFAULT,CW_USEDEFAULT,
                                NULL,NULL,hInstance,NULL);

	//Now for the ComboBox control. 
	hWndComboBox = CreateWindowEx(0,"COMBOBOX","CTL",
				CBS_DROPDOWN | WS_CHILD |WS_VISIBLE,
				100,100,100,100,hWndMain,1234,
				hInstance,NULL);
	SendMessage(hWndComboBox, CB_ADDSTRING, 0, "hello world"); 
	UpdateWindow(hWndComboBox);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	
	return msg.wParam;

}


LRESULT CALLBACK MyWndProc(HWND hWnd,
			UINT msg,
			WPARAM wParam,
			LPARAM lParam)
{
	
	PAINTSTRUCT ps;

	switch (msg)
	{
		case WM_COMMAND:

			if(lParam == hWndComboBox)
			{
	//This is a command from the Combo box check to 
        //see if it is CBN_DROPDOWN, and if so, to 
        //force the cursor (pointer) to appear.
				if(HIWORD(wParam) == CBN_DROPDOWN)
					SendMessage(hWndMain,WM_SETCURSOR,0,0);
			}

			break;

		case WM_PAINT:
			BeginPaint(hWndMain,&ps);
			EndPaint(hWndMain,&ps);
	
			break;

		case WM_SETFOCUS:

			break;

		case WM_SIZE:
		
			break;

		case WM_CREATE:
					
			break;
		case WM_CLOSE:

			DestroyWindow(hWnd);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);


			break;				
	
		default :
			return DefWindowProc(hWnd,msg,wParam,lParam);
	}

	return 0;

}
				

Modification Type:MinorLast Reviewed:7/11/2005
Keywords:kbpending kbprb KB326254