AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > VC编程

用Visual C++实现带阴影弹出窗口的技术

51自学网 2015-08-30 http://www.wanshiok.com

 

/////////////////////////////////////////////////////////////////////////////

 

//{{AFX_INSERT_LOCATION}}

// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

 

#endif // !defined(AFX_SHADOWWND_H__B971A958_59CC_11D2_AC8F_0060084237F6__INCLUDED_)


 

//实现文件
}

// ShadowWnd.cpp : implementation file

//

 

#include "stdafx.h"

#include "Shadow.h"

#include "ShadowWnd.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

 

//定义常数

 

static int aPattern[]={0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55};//阴影位图数组

#define SPOPUP_SHADOWWIDTH  10 //阴影宽度

#define SPOPUP_SHADOWHEIGHT 13 //阴影高度

#define MAXWIDTH  400 //显示字符矩形的最大宽度

 

/////////////////////////////////////////////////////////////////////////////

// CShadowWnd

 

CShadowWnd::CShadowWnd()

{

  CBitmap bmp;

  bmp.CreateBitmap(8,8,1,1,(void* )aPattern);//创建一个阴影位图

  m_bmpBrush.CreatePatternBrush(&bmp); //创建一把阴影刷

 

}

 

CShadowWnd::~CShadowWnd()

{

}

 

 

BEGIN_MESSAGE_MAP(CShadowWnd, CWnd)

  //{{AFX_MSG_MAP(CShadowWnd)

  ON_WM_NCPAINT()

  ON_WM_PAINT()

  ON_WM_CREATE()

  //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

 

/////////////////////////////////////////////////////////////////////////////

// CShadowWnd message handlers

 

BOOL CShadowWnd::Create(const RECT& rect, CWnd* pParentWnd)

{

  // TODO: Add your specialized code here and/or call the base class

  const char*  pClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW);

  return CWnd::CreateEx(WS_EX_STATICEDGE,pClassName, "Shadow window", WS_POPUP,

    rect.left,rect.top,rect.right,rect.bottom,

    pParentWnd->GetSafeHwnd(),0,NULL);

}

 

 

void CShadowWnd::OnNcPaint()

{

  // TODO: Add your message handler code here

  CWindowDC dc(this);

  CRect rc;

  GetWindowRect(&rc);

 

  rc.right-=rc.left; //width

  rc.bottom-=rc.top; //height

  rc.top=0;

  rc.left=0;

  m_bmpBrush.UnrealizeObject();

  CBrush* OldBrush=dc.SelectObject(&m_bmpBrush);

//画底部阴影

  dc.PatBlt(rc.left+SPOPUP_SHADOWWIDTH,rc.bottom-SPOPUP_SHADOWHEIGHT,

    rc.right-SPOPUP_SHADOWWIDTH,SPOPUP_SHADOWHEIGHT,PATCOPY);

//画右边阴影

  dc.PatBlt(rc.right-SPOPUP_SHADOWWIDTH,rc.top+SPOPUP_SHADOWHEIGHT,

    SPOPUP_SHADOWWIDTH,rc.bottom,PATCOPY);

  dc.SelectObject(OldBrush); //restore old brush

  CBrush* pBrush=CBrush::FromHandle(GetSysColorBrush(COLOR_WINDOWFRAME));

  rc.right-=SPOPUP_SHADOWWIDTH;

  rc.bottom-=SPOPUP_SHADOWHEIGHT;

  dc.FrameRect(rc,pBrush); //画边框

  // Do not call CWnd::OnNcPaint() for painting messages

}

 

void CShadowWnd::OnPaint()

{

  CPaintDC dc(this); // device context for painting

  

  // TODO: Add your message handler code here

  CRect rect;

  GetClientRect(&rect);

  rect.left+=5; rect.top+=5;

  rect.right-=SPOPUP_SHADOWWIDTH;

  rect.bottom-=SPOPUP_SHADOWHEIGHT;

  dc.SetTextColor(RGB(0,0,255));//设置显示文本颜色

  dc.DrawText(m_sShowText,rect,DT_WORDBREAK|DT_NOPREFIX);

  // Do not call CWnd::OnPaint() for painting messages

}

 

void CShadowWnd::ShowReadOnlyText(CString sText)

{

  m_sShowText=sText; //存入显示字符串

  CDC dc;

  dc.CreateDC("DISPLAY",NULL,NULL,NULL); //创建一个显示设备描述表

  dc.SelectObject(GetStockObject(SYSTEM_FONT)); //选择字体到设备描述表

  CRect rect(0,0,MAXWIDTH,0);

//获得待显示的字符串 sText 的实际高度和宽度

  dc.DrawText(sText,rect,DT_WORDBREAK|DT_CENTER|DT_CALCRECT|DT_NOPREFIX);

//为矩形留些余量

  rect.right+=3*SPOPUP_SHADOWWIDTH;

  rect.bottom+=3*SPOPUP_SHADOWHEIGHT;

  this->Create(rect,0);//创建窗口

  this->ShowWindow(SW_SHOW); //显示窗口

  this->UpdateWindow(); //立刻更新窗口

//进入消息循环,获取全部消息,控制整个系统

  MSG Msg;

  BOOL bDone;

  SetCapture();

  bDone=FALSE;

  while(!bDone)

  {

    if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE))

      if(Msg.message==WM_KEYDOWN||Msg.message==WM_SYSKEYDOWN||

        Msg.message==WM_LBUTTONDOWN||Msg.message==WM_RBUTTONDOWN)

        bDone=TRUE;

      else

      {

        TranslateMessage(&Msg);

        DispatchMessage(&Msg);

      }

  }

  ReleaseCapture();

  DestroyWindow();

 

}

 

int CShadowWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)

{

  if (CWnd::OnCreate(lpCreateStruct) == -1)

    return -1;

  

  // TODO: Add your specialized creation code here

  CenterWindow();

  return 0;

}


 

四.使用方法:

1.   将该类增加到一个项目文件中

2. 在你欲使用函数的类(一般为视类或框架窗口类)中增加一个成员变量(如:CshadowWnd m_ShadowWnd),当需要使用带阴影的弹出窗口显示信息时,调用成员函数(如: m_ShadowWnd.ShowText(String sText)即可,无须考虑其实现细节



   本程序在Visual C++ 5.0环境下编译通过.

 
 
说明
:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。

上一篇:用VC++6.0开发多表联接的数据库应用程序  下一篇:如何在VC中改变控件的背景色