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 ...
随机推荐
- 数据结构-B树
1.前言: 动态查找树主要有:二叉查找树(Binary Search Tree),平衡二叉查找树(Balanced Binary Search Tree),红黑树(Red-Black Tree ) ...
- Android之Adapter用法总结
http://blog.csdn.net/fznpcy/article/details/8658155 Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器 ...
- C# 队列数据结构 (三)
队列是一种先进先出的线性表 因此需要引用Clist线性类 class CQueue { private Clist m_list;//构造链表对象实例 public CQueue()//构造函数 { ...
- EMV标准
EMV标准是由国际三大银行卡组织--Europay(欧陆卡,已被万事达收购).MasterCard(万事达卡)和Visa(维萨)共同发起制定的银行卡从磁条卡向智能IC卡转移的技术标准,是基于IC卡的金 ...
- CC++初学者编程教程(5) 安装codeblocks软件开发环境
Code::Blocks 是一个开放源码的全功能的跨平台C/C++集成开发环境. Code::Blocks是开放源码软件.Code::Blocks由纯粹的C++语言开发完成,它使用了蓍名的图形界面库w ...
- poj3100---求根问题
题意:a的n方=b,a这个整数与b开n方的值相近,分别向上取整和向下取整,同时n方,b一定介于这两个整数之间,然后比较这两个数与b的距离,取最近的 收获:c++的cei和floor函数在c中的向上取整 ...
- paip.QQ音乐导出歌单总结
paip.QQ音乐导出歌单总结 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...
- Error occured processing XML 'Cannot find class [springmvc.extention.BeanArgumentResolver]'.
<Description Resource Path Location Type Error occured processing XML 'Cannot find class [springm ...
- zepto打造一款移动端划屏插件
效果图 样式1 样式2 调用 正常情况下应该是后台生成的html代码,但还是写了一套操作tab页的方法 调用简便如下: <link rel="stylesheet" href ...
- RHEL与Centos
一直在用centos,但对他的由来以及与RHEL的关系不是很明白,查些资料,小记一番. 倘若一说到Red Hat这个大名,大家似乎都听过. Qustion1:Red Hat家族中有哪些产品呢? Red ...