| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 604 人关注过本帖
标题:屏蔽健值的小窍门
只看楼主 加入收藏
赵博闻
Rank: 1
等 级:新手上路
帖 子:47
专家分:0
注 册:2007-9-20
收藏
 问题点数:0 回复次数:0 
屏蔽健值的小窍门
问题描述:在点击播放按钮前[font=Century]ListCtrl[/font]中的选项可以移动,可使用上下健和鼠标。当播放按钮点击时上下健,鼠标不可用,当点击停止按钮时以上操作恢复。

主要问题:
[font=宋体]屏蔽健值函数
[bold]virtual[/bold] [bold]BOOL[/bold] [bold]PreTranslateMessage([/bold] [bold]MSG*[/bold] [italic]pMsg[/italic] [bold]);[/bold],它是一个虚函数,主要意思是在收到系统消息时做出处理,继承与[font=Century]CWindThread[/font]类,在系统响应自定义消息之前动作。如果返回值是[font=Century]True[/font],表示系统已经响应了该消息,则之后的处理都不进行。反之,返回[font=Century]False[/font],表示还没有响应,要交到后面的响应事件。
实现步骤:
[font=Century]1[/font]由向导产生该函数
[font=Century]BOOL CDVD_ViewerDlg::PreTranslateMessage(MSG* pMsg) [/font]
[font=Century]{[/font]

[font=宋体]if (pMsg->message == WM_KEYDOWN){
[/font]
[font=Century]
switch (pMsg->wParam){
[/font]
[font=Century]
case VK_RETURN:
//
Enter
[/font]屏蔽
[font=Century]
return true;
[/font]
[font=Century]

case VK_UP:
[/font]
[font=Century]
if (IsPlayStart)
[/font]
[font=Century]
{
[/font]
[font=Century]
return true;
[/font]
[font=Century]
}
[/font]
[font=Century]
else if (IsPlayStart==false)
[/font]
[font=Century]
{
[/font]
[font=Century]
return false;
[/font]
[font=Century]
}
[/font]
[font=Century]
case VK_DOWN:
[/font]
[font=Century]
if (IsPlayStart)
[/font]
[font=Century]{[/font]
[font=Century]
return true;
[/font]
[font=Century]
}
[/font]
[font=Century]else if (IsPlayStart==false) [/font]
[font=Century]
{
[/font]
[font=Century]
return false;
[/font]
[font=Century]
}
[/font]
[font=Century]
default:
[/font]
[font=Century]
break;
[/font]
[font=Century]
}
[/font]
[font=Century]
}
[/font]
[font=Century]}[/font]
[bold]这里面还有个问题[/bold]:在网上查到鼠标左右健的虚拟健值
VK_LBUTTON               01                    Left mouse button
VK_RBUTTON              02                    Right mouse button
但使用case VK_LBUTTON:

return true;

不能屏蔽。我怀疑可能是鼠标事件较特殊要不然向导里也不会有那么多关于鼠标的函数或消息。因此采用了以下方法:
由向导建立函数:
[font=Century]void CMyListCtrl::OnLButtonDown(UINT nFlags, CPoint point) [/font]
[font=Century]{[/font]
[font=Century]
// TODO: Add your message handler code here and/or call default
[/font]
[font=Century]
if (m_IsPlayStart) {
[/font]
[font=Century]
return;
[/font]
[font=Century]
}
[/font]
[font=Century]
else{
[/font]
[font=Century]
CListCtrl::OnLButtonDown(nFlags, point);
[/font]
[font=Century]
}
[/font]
[font=Century]}[/font]
在此处有选择的控制这条命令[font=Century]CListCtrl::OnLButtonDown(nFlags, point);[/font]
该命令出现表示返回鼠标点击事件,没有时表示什么都不作。
[font=Century]2[/font]为了有选择的控制我使用了自定义消息,其实也可以直接在函数中附值,只是觉得消息封装性较好,以后如果控制条件增加如有好几个按钮或控件状态改变时都要达成目标条件,或需要在按钮状态变化时加入更多的功能,则只种方法就能简化代码。
[bold]原理:[/bold][bold]定义[/bold][bold][font=Century]BOOL m_IsPlayStart; [/font][/bold][bold]判断其状态,在指定动作方即播放按钮发送一个消息,消息处理函数中将[/bold][bold][font=Century]m_IsPlayStart = TRUE[/font][/bold][bold];则屏蔽上下箭头,和鼠标。在停止按钮中发送消息到窗口将[/bold][bold][font=Century]m_IsPlayStart = FALSE[/font][/bold][bold];恢复上下箭头,和鼠标。[/bold][bold][/bold]
[bold]实现:[/bold][bold][/bold]
[bold][font=Century]A [/font][/bold][bold]定义自定消息:[/bold][bold][/bold]
[bold](。[/bold][bold][font=Century]H[/font][/bold][bold][/bold][bold][/bold]
[bold][font=Century]#define WM_ISPLAYBTNDOWN WM_USER+30[/font][/bold]
[bold][font=Century]#define WM_ISPLAYBTNUP WM_USER+31[/font][/bold]
[bold]影射:[/bold][bold][/bold]
[bold][font=Century]
afx_msg void OnIsPlayBtnDown(WPARAM wParam,LPARAM lParam);
[/font][/bold]
[bold][font=Century]
afx_msg void OnIsPlayBtnUp(WPARAM wParam,LPARAM lParam);
[/font][/bold]
[bold][font=Century]
//}}AFX_MSG
[/font][/bold]
[bold][font=Century]
DECLARE_MESSAGE_MAP()
//
[/font][/bold][bold]一定要在该处上方填写[/bold][bold][/bold]
[bold](。[/bold][bold][font=Century]Cpp[/font][/bold][bold][/bold][bold][/bold]
[bold][font=Century]
ON_MESSAGE(WM_ISPLAYBTNDOWN,OnIsPlayBtnDown)
[/font][/bold]
[bold][font=Century]
ON_MESSAGE(WM_ISPLAYBTNUP,OnIsPlayBtnUp)
[/font][/bold]
[bold][font=Century]
//}}AFX_MSG_MAP
[/font][/bold]
[bold][font=Century]END_MESSAGE_MAP()
//
[/font][/bold][bold]一定要在该处上方填写[/bold][bold][/bold]
[bold][font=Century]B [/font][/bold][bold]消息处理函数[/bold][bold][/bold]
[bold][font=Century]void CDVD_ViewerDlg::OnIsPlayBtnDown(WPARAM wParam,LPARAM lParam)[/font][/bold]
[bold][font=Century]{//[/font][/bold][bold]测试使用的对话框消息正确发送时弹出[/bold][bold][/bold]
[bold][font=Century]//
AfxMessageBox("Player Btn is Down !",MB_OK|MB_RIGHT);
[/font][/bold]
[bold][font=Century]
IsPlayStart =true;
[/font][/bold]
[bold][font=Century]
//
[/font][/bold][bold]这里用了两种方法处理后面会细说原委[/bold][bold][font=Century]2[/font][/bold]
[bold][font=Century]
m_ListCtrlRec.m_IsPlayStart = m_ListCtrlRec.GetBtnPlayState(IsPlayStart);
[/font][/bold]
[bold][font=Century]
return ;
[/font][/bold]
[bold][font=Century]}[/font][/bold]
[bold][font=Century]void CDVD_ViewerDlg::OnIsPlayBtnUp(WPARAM wParam,LPARAM lParam)[/font][/bold]
[bold][font=Century]{//[/font][/bold][bold]测试使用的对话框消息正确发送时弹出[/bold][bold][/bold]
[bold][font=Century]//
AfxMessageBox("Puase Btn is Down !!",MB_OK|MB_RIGHT);
[/font][/bold]
[bold][font=Century]
IsPlayStart = false;
[/font][/bold]
[bold][font=Century]
//
[/font][/bold][bold]这里用了两种方法处理后面会细说原委[/bold][bold][font=Century]1[/font][/bold]
[bold][font=Century]
m_ListCtrlRec.m_IsPlayStart = FALSE;
[/font][/bold]
[bold][font=Century]
return;
[/font][/bold]
[bold][font=Century]}[/font][/bold]
[bold][font=Century]C [/font][/bold][bold]在按钮事件中发送消息[/bold][bold][/bold]
[bold]播放按钮:[/bold][bold][/bold]
[bold][font=Century]
BOOL IsPlayBtnDown;
[/font][/bold]
[bold][font=Century]
IsPlayBtnDown = PostMessage(WM_ISPLAYBTNDOWN,1,2);//
[/font][/bold][bold]后面两个参数可以任意写,真正有用的实地一个消息名,如果你的消息没有过多操作,可以认为后两个参数[/bold][bold][/bold]
[bold][font=Century]wParaw,lParaw[/font][/bold][bold],是占位符(只是个不够准确的理解其实一个是单字节,一个是指针)[/bold][bold][/bold]
[bold][font=Century]
if (!IsPlayBtnDown) {
[/font][/bold]
[bold][font=Century]
AfxMessageBox(GetLastError());
[/font][/bold]
[bold][font=Century]
}
[/font][/bold]
[bold]停止按钮:[/bold][bold][/bold]
[bold][font=Century]
BOOL IsPlayBtnUp;
[/font][/bold]
[bold][font=Century]
IsPlayBtnUp = PostMessage(WM_ISPLAYBTNUP,1,2);
[/font][/bold]
[bold][font=Century]
if (!IsPlayBtnUp) {
[/font][/bold]
[bold][font=Century]
AfxMessageBox(GetLastError());
[/font][/bold]
[bold][font=Century]
}
[/font][/bold]
[bold]这个消息好像在发广播,所以不用考虑截获因为是直接发给窗口。而且所带信息很少[/bold][bold][/bold]
[bold]至此好像已经实现功能了,但还是有问题。[/bold][bold][/bold]
[bold]就是鼠标事件[/bold][font=Century]OnLButtonDown[/font],直接在窗口类中产生无法实现效果。我猜测可能是此时鼠标消息已经被其他函数响应了,([bold]如果有更好的解释请回复,谢谢!![/bold])
所以自定义了一个继承与[font=Century]ListCtrl[/font]的类[font=Century]CMyListCtrl[/font],在此类中加入则可以
消息响应中的两种方法,第一种很简单
在窗口类中包含:[font=Century]#include [/font][font=Century]CMyListCtrl[/font][font=Century]h[/font]
[bold][font=Century]m_ListCtrlRec.m_IsPlayStart = FALSE;[/font][/bold]
[bold][font=Century]m_ListCtrlRec[/font][/bold][bold]为控件参数,可由向导生成([/bold][bold][font=Century]Member Varibles[/font][/bold][bold])即[/bold][font=Century]CMyListCtrl
[bold]m_ListCtrlRec[/bold]
[/font]
[bold][font=Century]m_IsPlayStart[/font][/bold][bold]为在[/bold][font=Century]CMyListCtrl[/font][font=Century]H[/font]定义的[font=Century]BOOL[/font]变量。
第二种有点麻烦:
简述原理:
窗口类[font=Century]A[/font]包含[font=Century]CListCtrl[/font][font=Century]B[/font]。并在[font=Century]A[/font]中定义[font=Century]Bool a[/font];想在[font=Century]a[/font]改变状态时在[font=Century]B[/font]中也能用到
则在[font=Century]B[/font]中定义[font=Century]Bool b[/font];初始化[font=Century]b=true[/font]
写一个函数入口参数为[font=Century]IsPlay[/font],然后再[font=Century]A[/font]中调用该函数,把[font=Century]a[/font]传入,然后在函数中将[font=Century]b=a[/font];返回[font=Century]b[/font]。在消息处理中付给[font=Century]B[/font]的参数:[bold][font=Century]m_ListCtrlRec.m_IsPlayStart = m_ListCtrlRec.GetBtnPlayState(IsPlayStart);[/font][/bold]
[bold][font=Century]B[/font][/bold][bold]中的函数:[/bold][bold][/bold]
[bold][font=Century]BOOL CMyListCtrl::GetBtnPlayState(BOOL IsPlay)[/font][/bold]
[bold][font=Century]{[/font][/bold]
[bold][font=Century]
m_IsPlayStart = IsPlay;
[/font][/bold]
[bold][font=Century]
return m_IsPlayStart;
[/font][/bold]
[bold][font=Century]}[/font][/bold]
[bold]综上:完成,主要注意[/bold][bold]PreTranslateMessage[/bold][bold][/bold][bold][font=Century]OnLButtonDown[/font][/bold][bold]的灵活处理。[/bold][bold][/bold]
[/font]
搜索更多相关主题的帖子: 窍门 
2007-12-07 14:34
快速回复:屏蔽健值的小窍门
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039905 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved