定时对话框的实现

来源:互联网 发布:编程让div显现出边框 编辑:程序博客网 时间:2024/06/06 03:13

          在我们常用的软件中,有一种对话框它非常特别,能在某一时刻显示,然后显示一段时间后自动关闭,MSDN中有一些关于具体实现的说明,整理了下。实现原理很简单,首先定义一个自己的窗口类,继承自CWnd类,对其中几个比较重要的函数进行说明,

void CQQMsgWnd::CreateMsgWindow(){RECT rect;SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);int y=rect.bottom-rect.top;int x=rect.right-rect.left;x=x-WIN_WIDTH;y=y-WIN_HEIGHT;CBrush brush;brush.CreatePatternBrush(&m_Bitmap);CreateEx(0,     AfxRegisterWndClass( 0, ::LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_HAND_1)),(HBRUSH)(brush.m_hObject),NULL), "", WS_POPUP|WS_EX_TOPMOST, x, y, WIN_WIDTH,//bmBitmap.bmWidth,  //Bitmap Width = Splash Window Width WIN_HEIGHT,//bmBitmap.bmHeight, //Bitmap Height = Splash Window Height AfxGetMainWnd()->GetSafeHwnd(), NULL, NULL);SetTimer(ID_TIMER_DISPLAY_DELAY,3000,NULL);}


注册一个自己定义的窗口,包含自己的鼠标,背景等,注册完之后,在某一个位置进行创建,实例代码是在窗口的右下角。然后设置一个定时器,时间为3S,

定时器处理函数:

void CQQMsgWnd::OnTimer(UINT nIDEvent) {/*static int nHeight=0; int cy=GetSystemMetrics(SM_CYSCREEN);int cx=GetSystemMetrics(SM_CXSCREEN);RECT rect;SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);int y=rect.bottom-rect.top;int x=rect.right-rect.left;x=x-WIN_WIDTH;*/switch(nIDEvent){case ID_TIMER_DISPLAY_DELAY:KillTimer(ID_TIMER_DISPLAY_DELAY);    SendMessage(WM_CLOSE);break;}CWnd::OnTimer(nIDEvent);}


3S之后窗口会自动销毁。

 

void CQQMsgWnd::OnPaint() {CPaintDC dc(this); // device context for paintingCDC dcMemory;CRect rect;GetClientRect(&rect);    dcMemory.CreateCompatibleDC(NULL);dcMemory.SelectObject(&m_Bitmap);dc.StretchBlt(0,0,rect.right-rect.left,//bmBitmap.bmWidth,rect.bottom-rect.top,//bmBitmap.bmHeight,    &dcMemory, 0,0,bmBitmap.bmWidth,    bmBitmap.bmHeight,SRCCOPY);CFont font;font.CreatePointFont(90,"Impact");dc.SelectObject(&font);//dc.SetTextColor(RGB(0,64,128));dc.SetTextColor(RGB(255,0,0));    dc.SetBkMode(TRANSPARENT);dc.TextOut(30,10,m_strCaption);rect.top=30;dc.DrawText(m_strMessage,-1,&rect,DT_CENTER|DT_SINGLELINE|DT_VCENTER);// Do not call CWnd::OnPaint() for painting messages}

进行贴图以及文字绘制操作,到此,基本的工作已经完成,大家还可以根据自己的需要,发挥自己的想象力,添加其它的操作。