|
MSG Msg; 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(); …… } #define AFX_SHADOWWND_H__B971A958_59CC_11D2_AC8F_0060084237F6__INCLUDED_
#pragma once #endif // _MSC_VER >= 1000 // ShadowWnd.h : header file //
// CShadowWnd window
{ // Construction public: CShadowWnd();
public:
public:
// ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CShadowWnd) public: virtual BOOL Create(const RECT& rect, CWnd* pParentWnd); //}}AFX_VIRTUAL
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() };
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
// ShadowWnd.cpp : implementation file //
#include "Shadow.h" #include "ShadowWnd.h"
#define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif
#define SPOPUP_SHADOWWIDTH 10 //阴影宽度 #define SPOPUP_SHADOWHEIGHT 13 //阴影高度 #define MAXWIDTH 400 //显示字符矩形的最大宽度
// CShadowWnd
{ CBitmap bmp; bmp.CreateBitmap(8,8,1,1,(void* )aPattern);//创建一个阴影位图 m_bmpBrush.CreatePatternBrush(&bmp); //创建一把阴影刷
{ }
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
{ // 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); } { // TODO: Add your message handler code here CWindowDC dc(this); CRect rc; GetWindowRect(&rc);
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 }
{ 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 }
{ 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();
{ 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)即可,无须考虑其实现细节 |