首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> VC.NET >> 正文

Visual C++实现带阴影弹出窗口的效果

  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();

……

}

 
. 带阴影的类 CShadowWnd 类的头文件及其实现文件的全部细节 
 

//头文件:
#if !defined(AFX_SHADOWWND_H__B971A958_59CC_11D2_AC8F_0060084237F6__INCLUDED_)

#define AFX_SHADOWWND_H__B971A958_59CC_11D2_AC8F_0060084237F6__INCLUDED_

#if _MSC_VER >= 1000

#pragma once

#endif // _MSC_VER >= 1000

// ShadowWnd.h : header file

//

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

// CShadowWnd window

class CShadowWnd : public CWnd

{

// Construction

public:

  CShadowWnd();

// Attributes

public:

// Operations

public:

// Overrides

  // ClassWizard generated virtual function overrides

  //{{AFX_VIRTUAL(CShadowWnd)

  public:

  virtual BOOL Create(const RECT& rect, CWnd* pParentWnd);

  //}}AFX_VIRTUAL

// Implementation

public:

  CString m_sShowText;

  void ShowReadOnlyText(CString sText);

  CBrush m_bmpBrush;

  virtual ~CShadowWnd();

// Generated message map functions

protected:

  //{{AFX_MSG(CShadowWnd)

  afx_msg void OnNcPaint();

  afx_msg void OnPaint();

 

  afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

  //}}AFX_MSG

  DECLARE_MESSAGE_MAP()

};

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

//{{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)即可,无须考虑其实现细节

上一页  [1] [2] [3] 下一页

【责编:Luzi】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航