MFC渐入渐出框实现方式二
类似360消息弹出框,实现方式一见http://blog.csdn.net/segen_jaa/article/details/7848598。
本文采用另外的API实现渐入渐出效果。
主要API:SetLayeredWindowAttributes。
实现功能:
采用管理器控制消息框每次只显示一个。
消息框独立显示在右下角,不随主窗口放大缩小变化。
鼠标进入消息框区域,渐入渐出效果停止。
1、消息框实现
创建对话框类CMsgTipDlg,设置对话框属性。
Tool Window:true。设置对话框为消息框,任务栏上将没有图标。
Topmost:true。设置对话框置顶。
MsgTipDlg.h。
- #pragma once
- // CMsgTipDlg dialog
- class CMsgTipMng;
- class CMsgTipDlg : public CDialog
- {
- DECLARE_DYNAMIC(CMsgTipDlg)
- public:
- CMsgTipDlg(CMsgTipMng* pTipMng, const CString& strTipInfo, CWnd* pParent = NULL); // standard constructor
- virtual ~CMsgTipDlg();
- // Dialog Data
- enum { IDD = IDD_MCMSG_DLG };
- void ShowMsgWindow();
- int GetTipID() const
- {
- return m_nTipID;
- }
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- virtual BOOL OnInitDialog();
- virtual void OnCancel();
- virtual void PostNcDestroy();
- virtual BOOL PreTranslateMessage(MSG* pMsg);
- afx_msg void OnTimer(UINT_PTR nIDEvent);
- afx_msg void OnBnClickedOk();
- afx_msg void OnBnClickedCancel();
- afx_msg void OnMouseMove(UINT nFlags, CPoint point);
- DECLARE_MESSAGE_MAP()
- private:
- void InitDlgPosition();
- private:
- CMsgTipMng* m_pTipMng;
- CString m_strTipInfo;
- int m_nTipID;
- BYTE m_bAlpha;//淡入淡出透明效果
- };
MsgTipDlg.cpp。
- // MCMsgTipDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "mcmsgtip_demo.h"
- #include "MsgTipDlg.h"
- #include "MsgTipMng.h"
- const UINT_PTR BLAND_IN = 1;
- const UINT_PTR DLG_DELAY = 2;
- const UINT_PTR BLAND_OUT = 3;
- const UINT IN_ELAPSE = 50;
- const UINT DELAY_ELAPSE = 5000;
- const UINT OUT_ELAPSE = 50;
- //淡入淡出跨度
- const BYTE BLAND_SPAN = 15;
- //淡入淡出最小值
- const BYTE BLAND_MIN = 0;
- //淡入淡出最大值
- const BYTE BLAND_MAX = 255;
- //淡入淡出颜色值设置
- const COLORREF BLAND_COLOR = 0;
- // CMsgTipDlg dialog
- IMPLEMENT_DYNAMIC(CMsgTipDlg, CDialog)
- CMsgTipDlg::CMsgTipDlg(CMsgTipMng* pTipMng, const CString& strTipInfo, CWnd* pParent)
- : CDialog(CMsgTipDlg::IDD, pParent)
- , m_pTipMng(pTipMng)
- , m_strTipInfo(strTipInfo)
- , m_nTipID(0)
- , m_bAlpha(0)
- {
- static int s_nTipID = 0;
- ++s_nTipID;
- m_nTipID = s_nTipID;
- }
- CMsgTipDlg::~CMsgTipDlg()
- {
- }
- void CMsgTipDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- }
- BEGIN_MESSAGE_MAP(CMsgTipDlg, CDialog)
- ON_WM_TIMER()
- ON_WM_MOUSEMOVE()
- ON_BN_CLICKED(IDOK, &CMsgTipDlg::OnBnClickedOk)
- ON_BN_CLICKED(IDCANCEL, &CMsgTipDlg::OnBnClickedCancel)
- END_MESSAGE_MAP()
- // CMsgTipDlg message handlers
- void CMsgTipDlg::ShowMsgWindow()
- {
- HWND hActiveHwnd = ::GetActiveWindow();
- Create(IDD, GetDesktopWindow());
- ShowWindow(SW_HIDE);
- ShowWindow(SW_SHOWNOACTIVATE);
- if (hActiveHwnd != NULL)
- {
- ::SetActiveWindow(hActiveHwnd);
- }
- }
- BOOL CMsgTipDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // TODO: Add extra initialization here
- SetDlgItemText(IDC_TIP_INFO, m_strTipInfo);
- InitDlgPosition();
- //设置窗口可淡入淡出
- ModifyStyleEx(NULL, WS_EX_LAYERED);
- //消息渐入渐出效果
- SetTimer(BLAND_IN, IN_ELAPSE, NULL);
- return TRUE;
- }
- void CMsgTipDlg::InitDlgPosition()
- {
- CRect rectInit;
- GetWindowRect(&rectInit);
- RECT rect;
- SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
- int cy = rect.bottom-rect.top;
- int cx = rect.right-rect.left;
- int nx = rect.right - rectInit.Width();
- int ny = cy - rectInit.Height();
- rectInit.MoveToXY(nx, ny);
- MoveWindow(rectInit);
- }
- void CMsgTipDlg::OnTimer(UINT_PTR nIDEvent)
- {
- RECT rect;
- SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);
- int cy = rect.bottom-rect.top;
- int cx = rect.right-rect.left;
- CRect rectTip;
- GetWindowRect(&rectTip);
- switch (nIDEvent)
- {
- case BLAND_IN:
- {
- if (m_bAlpha > (BLAND_MAX - BLAND_SPAN))
- {
- m_bAlpha = BLAND_MAX;
- }
- else
- {
- m_bAlpha += BLAND_SPAN;
- }
- SetLayeredWindowAttributes(BLAND_COLOR, m_bAlpha, LWA_ALPHA);
- if (BLAND_MAX == m_bAlpha)
- {
- KillTimer(BLAND_IN);
- SetTimer(DLG_DELAY, DELAY_ELAPSE, NULL);
- }
- break;
- }
- case DLG_DELAY:
- {
- KillTimer(DLG_DELAY);
- SetTimer(BLAND_OUT, OUT_ELAPSE, NULL);
- break;
- }
- case BLAND_OUT:
- {
- if (m_bAlpha < BLAND_SPAN)
- {
- m_bAlpha = BLAND_MIN;
- }
- else
- {
- m_bAlpha -= BLAND_SPAN;
- }
- SetLayeredWindowAttributes(BLAND_COLOR, m_bAlpha, LWA_ALPHA);
- if (BLAND_MIN == m_bAlpha)
- {
- KillTimer(BLAND_OUT);
- PostMessage(WM_CLOSE);
- }
- break;
- }
- }
- CDialog::OnTimer(nIDEvent);
- }
- void CMsgTipDlg::OnCancel()
- {
- DestroyWindow();
- }
- void CMsgTipDlg::PostNcDestroy()
- {
- CDialog::PostNcDestroy();
- //窗口销毁时,删除该对象
- m_pTipMng->RemoveTipWindow(m_nTipID);
- }
- void CMsgTipDlg::OnBnClickedOk()
- {
- OnCancel();
- //::MessageBox(AfxGetMainWnd()->GetSafeHwnd(), _T("提示框的反馈-是"), _T("提示"), MB_OK);
- }
- void CMsgTipDlg::OnBnClickedCancel()
- {
- OnCancel();
- }
- BOOL CMsgTipDlg::PreTranslateMessage(MSG* pMsg)
- {
- //对话框屏蔽Enter和ESC键
- if (WM_KEYDOWN == pMsg->message)
- {
- if ( (VK_RETURN == pMsg->wParam)
- || (VK_ESCAPE == pMsg->wParam))
- {
- return TRUE;
- }
- }
- return CDialog::PreTranslateMessage(pMsg);
- }
- void CMsgTipDlg::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CRect rect;
- GetClientRect(&rect);
- //显示对话框
- if (m_bAlpha < BLAND_MAX)
- {
- KillTimer(BLAND_IN);
- KillTimer(DLG_DELAY);
- KillTimer(BLAND_OUT);
- m_bAlpha = BLAND_MAX;
- SetLayeredWindowAttributes(BLAND_COLOR, m_bAlpha, LWA_ALPHA);
- //继续等待
- SetTimer(DLG_DELAY, DELAY_ELAPSE, NULL);
- }
- CDialog::OnMouseMove(nFlags, point);
- }
2、消息框管理器
消息框管理器功能:控制每次只弹出一个消息框。
MsgTipMng.h。
- /*
- @brief 消息提示管理器
- @date 2012-08-10
- */
- #pragma once
- #include "MsgTipDlg.h"
- #include <vector>
- #include <algorithm>
- using namespace std;
- class CMsgTipMng
- {
- public:
- CMsgTipMng(void);
- ~CMsgTipMng(void);
- void AddTipWindow(const CString& strTipInfo);
- void RemoveTipWindow(int nTipID);
- private:
- void ShowTipWindow();
- private:
- vector<CMsgTipDlg*> m_vTipVct;
- bool m_bInShow;//是否有消息框弹出
- };
MsgTipMng.cpp。
- #include "StdAfx.h"
- #include "mcmsgtip_demo.h"
- #include "MsgTipMng.h"
- class vcttip_finder
- {
- public:
- vcttip_finder(int nTipID)
- : m_nTipID(nTipID)
- {
- }
- bool operator()(const CMsgTipDlg* pTipDlg)
- {
- if (NULL == pTipDlg)
- {
- return false;
- }
- int nInTipID = pTipDlg->GetTipID();
- return (m_nTipID == nInTipID);
- }
- private:
- int m_nTipID;
- };
- CMsgTipMng::CMsgTipMng(void)
- : m_bInShow(false)
- {
- }
- CMsgTipMng::~CMsgTipMng(void)
- {
- }
- void CMsgTipMng::AddTipWindow(const CString& strTipInfo)
- {
- m_vTipVct.push_back(new CMsgTipDlg(this, strTipInfo));
- ShowTipWindow();
- }
- void CMsgTipMng::RemoveTipWindow(int nTipID)
- {
- vector<CMsgTipDlg*>::iterator vIter =
- find_if(m_vTipVct.begin(), m_vTipVct.end(), vcttip_finder(nTipID));
- if (vIter == m_vTipVct.end())
- {
- return;
- }
- m_vTipVct.erase(vIter);
- m_bInShow = false;
- ShowTipWindow();
- }
- void CMsgTipMng::ShowTipWindow()
- {
- if (m_vTipVct.empty())
- {
- return;
- }
- if (m_bInShow)
- {
- return;
- }
- m_bInShow = true;
- m_vTipVct[0]->ShowMsgWindow();
- }
3、消息框显示
m_pTipMng为成员变量,类型CMsgTipMng*。
显示对话框:
m_pTipMng->AddTipWindow(_T("Hello World!"));
MFC渐入渐出框实现方式二的更多相关文章
- MFC上下浮动与渐入渐出消息提示框实现
类似QQ与360软件,消息提示有两种.上下浮动.渐入渐出. 1.上下浮动提示框实现 机制,定时器响应上下浮动消息. 主要API:MoveWindow. 源码如下UpDownTipDlg.h.UpDow ...
- 解决Jquery mobile点击较长文本body的时候Header和footer会渐入渐出的问题
在做一个Phonegap+Jqm工程的时候,出现了如题的问题,相信很多人都遇到过Jquerymobile点击body时候header和footer会闪烁的显示和隐藏问题,fixed却并不能真 ...
- NSIS:实现程序窗口逐渐透明的渐入渐出效果
原文NSIS:实现程序窗口逐渐透明的渐入渐出效果 需要修改版的插件(支持timer功能): MUI:InstallOptions.dll MUI2:nsDialogs.dll 以及system插件,( ...
- [Unity3D]Unity3D游戏开发之Logo渐入渐出效果的实现
---------------------------------------------------------------------------------------------------- ...
- js原生实现div渐入渐出
jq对渐入渐出进行封装,简单的使用连个方法就可以实现.fadeIn(),fadeOut();如果我们界面没有使用jq那么原生怎么实现呢? 我们讲解一下,这个原理.当我们要实现渐入的时候,首先是让隐藏的 ...
- Windows Phone 几种弹出框提示方式
首先,我们需要在网络上下载一个Coding4Fun 然后,引用 using Coding4Fun.Phone.Controls.Toolkit; using Codin ...
- 以“图片渐入渐出”为例讲述jQuery插件的具体实现
首先声明,此代码以网友“斯迈欧”原创作为此例的讲解: 在这之前我们先看看我们要做的效果是什么样的: 解析下面的样式:我们要图片在过“一定时间”后自动切换,在右下角处有小方块似数字1,2,3,4,这些数 ...
- 转 android 从底部弹出一个popuwindow,渐入渐出效果。我这里是用在购物车需要选择购买选项的操作。
最近要改客户端,需要实现一个从底部弹出的popuwindow,像我这种渣渣android技术,能整出popuwindow但是整不出动画,百度之,记录一下. 从下面这个地址转的 http://blog. ...
- js渐隐渐入渐出效果 fadeOut fadeIn
//fadeOut //function start function fadeOut(ele,speed){ var ele=document.getElementById(ele); var op ...
随机推荐
- IScroll5+在ios、android点击(click)事件不兼容解决方法
Bug描述: ios.android4.4+下不能触发click事件. Bug解决: 调用iscroll插件,增加配置参数:click:true/false click的值是要根据移动终端设备进行判断 ...
- Weex 开发入门
去年也听说过 React Native 技术,现在好像很多大公司都在这套技术中踩坑,在开发自己的UI.工作中涉及不到,一直没有学习相关的知识. 并且听说阿里的 vue native,一直很期待.前段时 ...
- PHP中Global和Local范围以及Static变量
1. Local scope function update_counter() { $counter++;//此处$counter为局部变量,与函数外的$counter非同一个 } $counter ...
- easyui跨iframe属性datagrid
1.问题 如何刷新easyui父级tab页中iframe嵌套页中的datagrid? 2.解决方法 (1) parent.$("iframe[title='tabtitle']") ...
- WampServer下如何实现多域名配置
原文:WampServer下如何实现多域名配置 之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心 ...
- SSH连接不上
网上查了 大概说,一要安装开启ssh服务 然后关掉防火墙 service sshd restart service iptables stop 可是我用了之后还是连接不上, 很郁闷. 我尝试着ping ...
- denoising autoencoder
神经网络的挑战和关键技术: 1.神经网络结构决定(层,神经元,连接) 加入特定领域的知识(CNN 图片处理) 2.模型复杂度高 大的数据量: regularization: dro ...
- 迷宫寻宝(一)(bfs)
迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 一个叫ACM的寻宝者找到了一个藏宝图,它根据藏宝图找到了一个迷宫,这是一个很特别的迷宫,迷宫里有N个编 ...
- Ubuntu12.04下eclipse提示框黑色背景色的修改方法
eclipse提示框的背景颜色使用的是系统的提示框颜色配置,在windows下为黄色,但在Ubuntu12.04(gnome)下却是黑色,造成提示内容很难看清. 在eclipse中我们是无法修改这个颜 ...
- android_Intent对象初步(Activity传统的价值观念)
说明:初步Intent物.主要使用Intent对象在Activity之间传递数据的方法. 样例:由MainActivity→OtherActivity的跳转过程中,把数据传递给OtherActivit ...