用VC++实现带阴影的弹出窗口
2008-11-13 19:30:58 来源:WEB开发网//头文件:
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
efore 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;
}
使用方法:将该类增加到一个项目文件中;在你欲使用函数的类(一般为视类或框架窗口类)中增加一个成员变量(如:CshadowWnd m_ShadowWnd),当需要使用带阴影的弹出窗口显示信息时,调用成员函数(如:m_ShadowWnd.ShowText(String sText))即可,无须考虑其实现细节。程序在Visual C++ 6.0环境下编译通过。
更多精彩
赞助商链接