MouseButtonDown在对话框中的子控件上,第一次和最后一次都不起作用 [英] MouseButtonDown ona child control in dialog, not wrkng first and last time

查看:124
本文介绍了MouseButtonDown在对话框中的子控件上,第一次和最后一次都不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:
一个对话框,具有各种其他控件以及列表框控件::

LBS_MULTIPLESEL
ON_LBN_SELCHANGE用于获取最新选择的控件的索引.
如果用户按住ControlKey,则会发生其他多项选择,它的作用类似于普通的多项选择列表框.

因此,只要通过选择一个新项目(未选中的项目)来发生ON_LBN_SELCHANGE,如果用户按住控制键并同时选择了新项目,则旧的选择将保留.

每当通过选择一个已选择的项目而发生ON_LBN_SELCHANGE时,如果用户按住控制键并且单击的项目也被取消选择,则保留旧选择(或者)如果用户不按住控制键和单击的项目则清除旧选择被选中.

问题:
根据MFC多重选择列表框的工作,当我们选择一个新项目时,只要我们在已选择的项目上选择它的取消选择,旧项目就保持选中状态.这些发生在MouseDown事件本身中.

在这些鼠标按下事件之后,发生鼠标按下事件,该事件触发ON_LBN_SELCHANGE(我正在使用它,因为它在这里提供了新单击或锚定的项目的正确索引),现在这是我取消选择已选择的项目或选择新物品.因此,很明显,我正在做与MFC在Mouse Down事件中所做的相反的事情.

因此,如果用户没有按住控制键,那么我正在执行鼠标按下事件正在执行的反向操作.

简介:
这很烦人,让我们举个例子,用户单击鼠标并按住3秒钟,然后单击鼠标向上,因此,如果他选择一个新项目并按住鼠标,则仍然选中了旧项目(MFC),他认为两者都被选中,现在当他释放鼠标时,我正在取消选择旧鼠标(由ON_LBN_SELCHANGE完成).

同样,如果他单击一个已选择的项目,则该项目将在鼠标按下(MFC)时被选择,而我将其选择回(ON_LBN_SELCHANGE)

解决方法或我的解决方案:
在ListBox中捕获鼠标按下事件并相应地处理情况.

Scenario:
A dialog, has various other controls along with List Box control::

LBS_MULTIPLESEL
ON_LBN_SELCHANGE is used to get the index of the latest selected control.
If the user hold the ControlKey, multiple selection occurs else, it acts like a normal multiple selection list box.

so whenever ON_LBN_SELCHANGE occurs by selecting a new item(non selected item), the old selection is retained if the user holds control key and new item is also selected.

whenever ON_LBN_SELCHANGE occurs by selecting an already selected item, the old selections are retained if the user holds control key and the clicked item is also deselected (or) the old selections are cleared off if the user doesn''t hold control key and clicked item is selected.

Problem:
according to working of a MFC Multiple selection listbox, when we select a new item, the old item remains selected, whenever we select on already selected item its deselected. these were happening in the MouseDown events itself.

After these mouse down event, comes the mouse up event which triggers the ON_LBN_SELCHANGE(i am using this because it gives the correct index of the newly clicked or anchored item here), now this is where i am deselecting the already selected item or selecting the new item. So clearly i am doing reverse of what MFC is doing in the Mouse Down event.

So i the user doesn''t hold the control key i am doing the reverse action what mouse down event is doing.

Brief:
This is annoying, lets take an example where user click the mouse down and holds for 3 seconds and then clicks mouse up, so if he selects a new item, and holds the mouse down the old item is still selected (MFC) and he thinks both were selected, now when he releases the mouse i am deselecting the old one (done by ON_LBN_SELCHANGE).

Similarly if he clicks on an already selected item, the item is deselcted in mouse down (MFC) and i am selecting it back (ON_LBN_SELCHANGE)

Workaround or my solution:
Capture the mouse down event in the ListBox and handle the situation accordingly.

BOOL CLGDlg::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_LBUTTONDOWN)
	{
		CWnd* pWnd = GetFocus();
		if (pWnd->GetDlgCtrlID() == IDC_LNGNAMEBOX)
		{
			if(GetKeyState(VK_CONTROL) < 0)
			{
				//dont do anything since its working fine
			}
			else
			{
			        int SelectedItem;
				SelectedItem = m_LngNameList.GetAnchorIndex();
				m_LngNameList.SetSel(SelectedItem, 0);
			}
		}
	}
}



这是第一次也不是最后一次,因为第一次单击焦点不在列表框上,或者当单击远离焦点时焦点仍在列表框中.

有任何建议吗?



This is not working for the first time and last time, since the focus is not on the list box the first time i click it or the focus is still in the listbox when i click away from it.

Any suggestions please?

推荐答案

如果要在PreTranslateMessage()中执行此操作,则另一种确定被单击窗口的方法是查看窗口的位置.单击并查看它是否位于按钮的位置内,因为该信息位于MSG*参数中.

If you want to do this within PreTranslateMessage(), then another way to identify the window that was clicked on is to look at the location of the click and see if it''s within the location of the button, since that information is in the MSG* parameter.

//MSG definition
typedef struct tagMSG {     // msg
   HWND hwnd;
   UINT message;
   WPARAM wParam;
   LPARAM lParam;
   DWORD time;
   POINT pt;
} MSG;



使用GetDlgItem()获取按钮窗口的句柄,然后使用GetWindowRect()查找其位置与点击位置进行比较.



Use GetDlgItem() to get a handle to the window of the button, then GetWindowRect() to find its location to compare with the location of the click.


这篇关于MouseButtonDown在对话框中的子控件上,第一次和最后一次都不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆