// WndEx2Dlg.cpp : implementation file // #include "stdafx.h" #include "WndEx2.h" #include "WndEx2Dlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif / // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() / // CWndEx2Dlg dialog CWndEx2Dlg::CWndEx2Dlg(CWnd* pParent /*=NULL*/) : CDialog(CWndEx2Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CWndEx2Dlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CWndEx2Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CWndEx2Dlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CWndEx2Dlg, CDialog) //{{AFX_MSG_MAP(CWndEx2Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BTN_RECT, OnBtnRect) ON_BN_CLICKED(IDC_BTN_ELL, OnBtnEll) ON_BN_CLICKED(IDC_BTN_POLYG, OnBtnPolyg) ON_BN_CLICKED(IDC_BTN_ROUND, OnBtnRound) ON_BN_CLICKED(IDC_BTN_TRANS, OnBtnTrans) ON_BN_CLICKED(IDC_BTN_TRANS2, OnBtnTrans2) ON_BN_CLICKED(IDC_BTN_FLASH, OnBtnFlash) ON_BN_CLICKED(IDC_BTN_STOPF, OnBtnStopf) ON_WM_TIMER() ON_BN_CLICKED(IDC_BTN_PICTURE, OnBtnPicture) ON_BN_CLICKED(IDC_BTN_ANIMATE, OnBtnAnimate) ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() / // CWndEx2Dlg message handlers BOOL CWndEx2Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here CenterWindow(); AnimateWindow(GetSafeHwnd(),500,AW_BLEND);//淡入 return TRUE; // return TRUE unless you set the focus to a control } void CWndEx2Dlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CWndEx2Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CWndEx2Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CWndEx2Dlg::OnBtnRect() { CRect rect; GetClientRect(&rect); //创建矩形区域 CRgn rgn; rgn.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom); //设置窗口的区域 SetWindowRgn((HRGN)rgn,TRUE); } void CWndEx2Dlg::OnBtnEll() { CRect rect; GetClientRect(&rect); //创建椭圆形区域 CRgn rgn; rgn.CreateEllipticRgn(0,0,rect.Width(),rect.Height()); //设置区域 SetWindowRgn((HRGN)rgn,TRUE); } void CWndEx2Dlg::OnBtnPolyg() { CRect rect; GetClientRect(rect); //常见多边形区域 CPoint point[6]; //六边形的六个点 point[0].x=0; point[0].y=rect.Height()/2; point[1].x=rect.Width()/3; point[1].y=0; point[2].x=2*rect.Width()/3; point[2].y=0; point[3].x=rect.Width(); point[3].y=rect.Height()/2; point[4].x=2*rect.Width()/3; point[4].y=rect.Height(); point[5].x=rect.Width()/3; point[5].y=rect.Height(); CRgn rgn; rgn.CreatePolygonRgn(point,6,ALTERNATE); //设置窗口 的区域 SetWindowRgn((HRGN)rgn,TRUE); } void CWndEx2Dlg::OnBtnRound() { CRect rect; GetClientRect(rect); //创建圆形区域 CRgn rgn; rgn.CreateRoundRectRgn(0,0,rect.Width(),rect.Height(),rect.Width()/2,rect.Height()/2); //设置窗口区域 SetWindowRgn((HRGN)rgn,TRUE); } void CWndEx2Dlg::OnBtnTrans() { CRect rect1; GetWindowRect(rect1); CRect rect2; GetWindowRect(rect2); ClientToScreen(rect2); CRgn rgn1; rgn1.CreateRectRgn(rect1.left,rect2.top,rect1.right,rect1.bottom); CRgn rgn2; rgn2.CreateRectRgn(rect2.left,rect2.top,rect2.right,rect2.bottom); CRgn rgn; rgn.CreateRectRgn(0,0,1,1); rgn.CombineRgn(&rgn1,&rgn2,RGN_DIFF); //设置窗口区域 SetWindowRgn((HRGN)rgn,TRUE); } void CWndEx2Dlg::OnBtnTrans2() { //添加WS_EX_LAYERED(0X80000)扩展风格 ModifyStyleEx(0,0x80000); //加载User32.dll 动态链接库 HMODULE hModule=LoadLibrary("User32.DLL"); if (hModule!=NULL) { typedef BOOL(WINAPI*FUNC)(HWND,COLORREF,BYTE,DWORD); //获得SetLayeredWindowAttributes函数指针 FUNC func=(FUNC)GetProcAddress(hModule,"SetLayeredWindowAttributes"); if (func!=NULL) { func(GetSafeHwnd(),0,50,2);//具体参看SetLayeredWindowAttributes 函数 } FreeLibrary(hModule); } } void CWndEx2Dlg::OnBtnFlash() { //设置定时器 SetTimer(1,100,NULL); } void CWndEx2Dlg::OnBtnStopf() { //关闭定时器 KillTimer(1); //窗口返回原始的状态 FlashWindow(FALSE); } void CWndEx2Dlg::OnTimer(UINT nIDEvent) { if (nIDEvent==1) { //窗口从一种状态闪烁到另一种状态 FlashWindow(TRUE); } CDialog::OnTimer(nIDEvent); } void CWndEx2Dlg::OnBtnPicture() { CRect rect; GetWindowRect(&rect); //加载位图 CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP); //将位图选入设备环境 CDC dc; CDC* pDC=GetDC(); dc.CreateCompatibleDC(pDC); dc.SelectObject(&bmp); //将位图中的黑色区域变成透明 CRgn rgn; rgn.CreateRectRgn(0,0,rect.Width(),rect.Height()); for (int x=0;x<rect.Height();x++) { for (int y=0;y<rect.Width();y++) { COLORREF cr=dc.GetPixel(x,y); if (cr==RGB(0,0,0)) { CRgn rgn2; rgn2.CreateRectRgn(x,y,x+1,y+1); rgn.CombineRgn(&rgn,&rgn2,RGN_XOR);//XOR 结合俩个区域,去掉重叠的部分 } } } //设置窗口区域 SetWindowRgn((HRGN)rgn,TRUE); ReleaseDC(pDC); } void CWndEx2Dlg::OnBtnAnimate() { //CenterWindow(); //AnimateWindow(GetSafeHwnd(),100,AW_BLEND); } BOOL CWndEx2Dlg::OnEraseBkgnd(CDC* pDC) { CRect rect; GetWindowRect(&rect); CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP); CDC dc; dc.CreateCompatibleDC(pDC); dc.SelectObject(&bmp); pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dc,0,0,SRCCOPY); return TRUE; } 图片窗口操作中,需要自己添加消息响应 afx_msg BOOL OnEraseBkgnd(CDC* pDC); ON_WM_ERASEBKGND() 下载地址:http://ishare.iask.sina.com.cn/f/14464563.html