互动直播 之 全屏播放时 鼠标隐藏功能
目录
想实现的功能和播放器类似,全屏时,鼠标悬停几秒后,自动隐藏起来。
一、鼠标自动隐藏显示
1、鼠标移动时,记录移到时的时间点,并显示鼠标
else if (uMsg == WM_MOUSEMOVE)
{
if (isFullScreen_)
{
mouseKeepStillBeginTime_ = GetCurTimeStamp();
ShowMyCursor();
}
}
int64_t GetCurTimeStamp()
{
timeb time_now;
ftime(&time_now); // 秒数
int64_t cur_timestamp = time_now.time * 1000 + time_now.millitm; // 毫秒
return cur_timestamp;
}
2、设置一个重复循环,每秒判断下
根据当前的时间和之前移动时记录的时间,判断有无超过指定的秒数,来确定是否隐藏
mouse_autoHide_timer_.Cancel();
StdClosure task = [this]() {
if (pExerciseForm&&IsWindowVisible(pExerciseForm->GetHWND()))
{
ShowMyCursor();
}
else{
int64_t now = GetCurTimeStamp();
int64_t dis = now - mouseKeepStillBeginTime_;
if (dis > 1000)
{
HideMyCursor();
}
}
};
nbase::ThreadManager::PostRepeatedTask(kThreadUI, mouse_autoHide_timer_.ToWeakCallback(task), nbase::TimeDelta::FromMilliseconds(1000));
typedef std::function<void(void)> StdClosure;
3、退出全屏时,结束是否隐藏任务判断
isFullScreen_ = false;
mouse_autoHide_timer_.Cancel();
ShowMyCursor();
如果想了解如何控制的话,可以参考下面代码,我这里直接使用了这种方法
class BASE_EXPORT WeakCallbackFlag final : public SupportWeakCallback
{
public:
void Cancel()
{
m_weakFlag.reset();
}
bool HasUsed()
{
return m_weakFlag.use_count() != 0;
}
};
class BASE_EXPORT SupportWeakCallback
{
public:
virtual ~SupportWeakCallback(){};
template<typename CallbackType>
auto ToWeakCallback(const CallbackType& closure)
->WeakCallback<CallbackType>
{
return WeakCallback<CallbackType>(GetWeakFlag(), closure);
}
std::weak_ptr<WeakFlag> GetWeakFlag()
{
if (m_weakFlag.use_count() == 0) {
m_weakFlag.reset((WeakFlag*)NULL);
}
return m_weakFlag;
}
private:
template<typename ReturnValue, typename... Param, typename WeakFlag>
static std::function<ReturnValue(Param...)> ConvertToWeakCallback(
const std::function<ReturnValue(Param...)>& callback, std::weak_ptr<WeakFlag> expiredFlag)
{
auto weakCallback = [expiredFlag, callback](Param... p) {
if (!expiredFlag.expired()) {
return callback(p...);
}
return ReturnValue();
};
return weakCallback;
}
protected:
std::shared_ptr<WeakFlag> m_weakFlag;
};