TitleBarDlg.cpp
资源名称:TitleBar.rar [点击查看]
上传用户:shixing_sh
上传日期:2021-10-02
资源大小:3629k
文件大小:14k
源码类别:
工具条
开发平台:
Visual C++
- // TitleBarDlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "TitleBar.h"
- #include "TitleBarDlg.h"
- #include "YouDlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /**************************************************************
- 严重声明:未经本人许可不得用于任何商业用途.
- 作 者:朱一松
- 联系方式:Email:song_0962@sina.com
- //*************************************************************
- 界面程序用法说明:
- 1.TitleBarDlg.h和TitleBarDlg.cpp 为主界面类程序文件
- 若其它窗口要继承主窗口风格,其窗口的基类必须为
- CTitleBarDlg,采用对话框资源编辑器完成窗口设计后,新
- 建类后,在你的对话框类文件(.cpp .h)中手工修所有的
- CDialog 为 CTitleBarDlg。
- 2.TitleBarDlg.h和TitleBarDlg.cpp
- 除了指定位置可添加代码外,其它地方不能随便添加修改代码.
- 3.一定要用本的界面工程做为模板,在此基础开发项目,不要新建
- 工程文件。
- 4.请参考CyouDlg类*/
- // CTitleBarDlg dialog
- //****************************************************
- CyouDlg *pMyDlg = NULL;
- CTitleBarDlg::CTitleBarDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CTitleBarDlg::IDD, pParent)
- {
- m_Status = 0;
- m_ShowTitle = TRUE;
- m_ReSizeFlag = FALSE;
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- CTitleBarDlg::~CTitleBarDlg()
- {
- //if(pMyDlg) delete pMyDlg;
- }
- void CTitleBarDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTitleBarDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTitleBarDlg, CDialog)
- //{{AFX_MSG_MAP(CTitleBarDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_WM_NCLBUTTONDOWN()
- ON_WM_NCMOUSEMOVE()
- ON_WM_SYSCOMMAND()
- ON_WM_SIZE()
- ON_WM_MOUSEMOVE()
- ON_BN_CLICKED(IDC_BUTTON1, OnOk)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTitleBarDlg message handlers
- BOOL CTitleBarDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
- m_ToolTip.Create(this,TTS_ALWAYSTIP);//创建工具提示控件,TTS_ALWAYSTIP表示,
- m_ReSizeFlag = TRUE;
- CRect rtWnd;
- GetWindowRect(rtWnd);
- m_OldWidth = rtWnd.Width();
- m_OldHeight = rtWnd.Height();
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CTitleBarDlg::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 CTitleBarDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- LRESULT CTitleBarDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
- {
- LRESULT lrst=CDialog::DefWindowProc(message, wParam, lParam);
- if (!::IsWindow(m_hWnd))
- return lrst;
- if (message==WM_MOVE||message==WM_PAINT||message==WM_NCPAINT||message==WM_NCACTIVATE ||message == WM_NOTIFY)
- {
- CDC* pWinDC = GetWindowDC();
- if (pWinDC)
- DrawTitleBar(pWinDC);
- ReleaseDC(pWinDC);
- }
- return lrst;
- }
- void CTitleBarDlg::OnNcLButtonDown(UINT nHitTest, CPoint point)
- {
- CRect rtWnd;
- GetWindowRect(&rtWnd);
- //mouse坐标转化为本窗口坐标 重要
- point.x = point.x - rtWnd.left;
- point.y = point.y - rtWnd.top;
- //检测各按钮是否按到
- if (m_rtIcon.PtInRect(point))
- AfxMessageBox("界面软件设计者:朱一松 EMail:Song_0962@sina.com");
- else
- {
- if (m_rtButtHelp.PtInRect(point))
- {
- SendMessage(WM_HELP);
- }
- else
- {
- if (m_rtButtExit.PtInRect(point))
- {
- SendMessage(WM_CLOSE);
- }
- else
- {
- if (m_rtButtMin.PtInRect(point))
- {
- m_ShowTitle = FALSE;
- SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, MAKELPARAM(point.x, point.y));
- }
- else
- {
- if (m_rtButtMax.PtInRect(point))
- {
- m_ShowTitle = TRUE;
- if (IsZoomed())
- SendMessage(WM_SYSCOMMAND, SC_RESTORE, MAKELPARAM(point.x, point.y));
- else
- {
- SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, MAKELPARAM(point.x, point.y));
- Invalidate();
- }
- }
- else
- {
- if (!IsZoomed())
- {
- Default();
- }
- }
- }
- }
- }
- }
- }
- void CTitleBarDlg::OnNcMouseMove(UINT nHitTest, CPoint point)
- {
- CDC* pDC = GetWindowDC();
- CDC* pDisplayMemDC=new CDC;
- pDisplayMemDC->CreateCompatibleDC(pDC);
- CBitmap* pBitmap = new CBitmap;
- CBitmap* pOldBitmap;
- CRect rtWnd, rtButton;
- if (pDC)
- {
- CString StrTemp = "";
- GetWindowRect(&rtWnd);
- //mouse坐标转化为本窗口坐标 重要
- point.x = point.x - rtWnd.left;
- point.y = point.y - rtWnd.top;
- //判断mouse是否移到系统按钮上
- if (m_rtButtExit.PtInRect(point))
- {
- pBitmap->LoadBitmap(IDB_EXIT_FOCUS);
- StrTemp = _T("关闭");
- }
- else
- {
- if(m_rtButtMin.PtInRect(point))
- {
- pBitmap->LoadBitmap(IDB_MIN_FOCUS);
- StrTemp = _T("最小化窗口");
- }
- else
- {
- if(m_rtButtMax.PtInRect(point))
- {
- pBitmap->LoadBitmap(IDB_MAX_FOCUS);
- if(IsZoomed())
- {
- StrTemp = _T("还原窗口");
- }
- else
- {
- StrTemp = _T("最化大窗口");
- }
- }
- else
- {
- pBitmap->LoadBitmap(IDB_NORMAL);
- }
- }
- }
- rtButton = m_rtButtMin;
- BITMAP BmpInfo;
- pBitmap->GetBitmap(&BmpInfo);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- pDC->BitBlt(rtButton.left-6, rtButton.top-2, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- CRect ShowTipRec;
- ShowTipRec = m_rtButtMin;
- if(!StrTemp.IsEmpty())
- {
- ScreenToClient(&ShowTipRec);
- m_ToolTip.AddToolTip(IDD_TITLEBAR_DIALOG,&ShowTipRec,StrTemp);
- m_ToolTip.SetDelayTime(200);
- }
- }
- ReleaseDC(pDisplayMemDC);
- ReleaseDC(pDC);
- delete pDisplayMemDC;
- delete pBitmap;
- CDialog::OnNcMouseMove(nHitTest, point);
- }
- void CTitleBarDlg::DrawTitleBar(CDC *pDC)
- {
- if (m_hWnd)
- {
- CBrush Brush(RGB(0,100,255));
- CBrush* pOldBrush = pDC->SelectObject(&Brush);
- CRect rtWnd, rtTitle, rtButtons;
- GetWindowRect(&rtWnd);
- //取得标题栏的位置
- rtTitle.left = GetSystemMetrics(SM_CXFRAME);
- rtTitle.top = GetSystemMetrics(SM_CYFRAME);
- rtTitle.right = rtWnd.right - rtWnd.left - GetSystemMetrics(SM_CXFRAME);
- rtTitle.bottom = rtTitle.top + GetSystemMetrics(SM_CYSIZE);
- //计算最小化按钮的位置,位图大小为15X15
- rtButtons.left = rtTitle.right-60;
- rtButtons.top= rtTitle.top+2;
- rtButtons.right = rtTitle.right-42;
- rtButtons.bottom = rtTitle.top+20;
- m_rtButtMin = rtButtons;
- //计算最大化按钮的位置,位图大小为15X15
- m_rtButtMax.left = m_rtButtMin.left + 18;
- m_rtButtMax.top = m_rtButtMin.top;
- m_rtButtMax.right = m_rtButtMin.right + 16;
- m_rtButtMax.bottom = m_rtButtMin.bottom;
- m_rtButtExit.left = m_rtButtMax.left + 18;
- m_rtButtExit.top = m_rtButtMax.top;
- m_rtButtExit.right = m_rtButtMax.right + 16;
- m_rtButtExit.bottom = m_rtButtMax.bottom;
- //准备画图
- CBitmap* pBitmap = new CBitmap;
- BITMAP BmpInfo;
- CBitmap* pOldBitmap;
- CDC* pDisplayMemDC=new CDC;
- pDisplayMemDC->CreateCompatibleDC(pDC);
- //重画最Caption
- POINT DrawPonit;
- DrawPonit.x = rtTitle.left-4;
- DrawPonit.y = rtTitle.top-4;
- pBitmap->LoadBitmap(IDB_TITLE_LEFT);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- pBitmap->GetBitmap(&BmpInfo);
- pDC->BitBlt(rtTitle.left-4, rtTitle.top-4, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- DrawPonit.x = DrawPonit.x + BmpInfo.bmWidth;
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- pBitmap->LoadBitmap(IDB_TOP);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- pBitmap->GetBitmap(&BmpInfo);
- while(DrawPonit.x<= m_rtButtMin.left-66)
- {
- pDC->BitBlt(DrawPonit.x, DrawPonit.y, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- DrawPonit.x = DrawPonit.x + BmpInfo.bmWidth;
- }
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- pBitmap->LoadBitmap(IDB_TITLE_RIGHT);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- pBitmap->GetBitmap(&BmpInfo);
- pDC->BitBlt(DrawPonit.x, DrawPonit.y, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- //重画最小化button
- rtButtons = m_rtButtMin;
- pBitmap->LoadBitmap(IDB_NORMAL);
- pBitmap->GetBitmap(&BmpInfo);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- pDC->BitBlt(rtButtons.left-6, rtButtons.top-2, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- int nOldMode = pDC->SetBkMode(TRANSPARENT);
- COLORREF clOldText=pDC->GetTextColor();
- CFont titleFont;
- titleFont.CreateFont( 12, // nHeight
- 8, // nWidth
- 0, // nEscapement文本行逆时针旋转角度
- 0, // nOrientation字体角度
- FW_BOLD, // nWeight字体粗细程度
- FALSE, // bItalic
- FALSE, // bUnderline
- 0, // cStrikeOut 删除线
- ANSI_CHARSET, // nCharSet
- OUT_DEFAULT_PRECIS, // nOutPrecision
- CLIP_DEFAULT_PRECIS, // nClipPrecision
- DEFAULT_QUALITY, // nQuality
- DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
- _T("隶书")); // lpszFac pDC->SelectStockObject(SYSTEM_FIXED_FONT);
- CFont *OldFont;
- OldFont = pDC->SelectObject(&titleFont);
- CString m_StrTitle;
- GetWindowText(m_StrTitle);
- pDC->SetTextColor(RGB(80,255,25));
- if(m_ShowTitle)
- {
- pDC->TextOut(65,10,m_StrTitle);
- }
- else
- {
- m_StrTitle = m_StrTitle.Left(6);
- m_StrTitle += "...";
- pDC->TextOut(30,10,m_StrTitle);
- }
- pDC->SetBkMode(nOldMode);
- pDC->SetTextColor(clOldText);
- pDC->SelectObject(OldFont);
- // pDC->TextOut(60,60,m_StrTitle);
- //重画左边框
- pBitmap->LoadBitmap(IDB_LEFTDOWN);
- pBitmap->GetBitmap(&BmpInfo);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- int i ;
- for (i= 20;i<=rtWnd.bottom;i=i+BmpInfo.bmHeight-3)
- {
- pDC->BitBlt(0, rtButtons.top+i, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- }
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- //重画右边框
- pBitmap->LoadBitmap(IDB_RIGHTDOWN);
- pBitmap->GetBitmap(&BmpInfo);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- GetClientRect(&rtWnd);
- for (i= 25;i<=rtWnd.bottom+27;i=i+BmpInfo.bmHeight-3)
- {
- pDC->BitBlt(rtWnd.right, i, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- }
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- //重画底边框
- pBitmap->LoadBitmap(IDB_DOWN);
- pBitmap->GetBitmap(&BmpInfo);
- pOldBitmap=(CBitmap*)pDisplayMemDC->SelectObject(pBitmap);
- GetClientRect(&rtWnd);
- for (i= 9; i<=rtWnd.right; i=i+2)
- {
- GetClientRect(&rtWnd);
- pDC->BitBlt(i,rtWnd.bottom+26, BmpInfo.bmWidth, BmpInfo.bmHeight, pDisplayMemDC, 0, 0, SRCCOPY);
- }
- pDisplayMemDC->SelectObject(pOldBitmap);
- pBitmap->DeleteObject();
- //重画图标
- // m_rtIcon.left = 3;//rtWnd.Width()-5;
- // m_rtIcon.top = GetSystemMetrics(SM_CYFRAME);
- // m_rtIcon.right = m_rtIcon.left + 32;
- // m_rtIcon.bottom = m_rtIcon.top + 32;
- // ::DrawIconEx(pDC->m_hDC, m_rtIcon.left, m_rtIcon.top, m_hIcon,
- // m_rtIcon.Width(), m_rtIcon.Height(), 0, NULL, DI_NORMAL);
- // m_rtIcon.OffsetRect(rtWnd.TopLeft());
- ReleaseDC(pDisplayMemDC);
- delete pDisplayMemDC;
- delete pBitmap;
- }
- }
- void CTitleBarDlg::OnOK()
- {
- this->DestroyWindow();
- if(pMyDlg) delete pMyDlg;
- }
- void CTitleBarDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if (nID == SC_CLOSE)
- {
- AfxGetMainWnd()->DestroyWindow();//关闭应用程序
- return;
- }
- if (nID == SC_RESTORE)
- {
- ShowWindow(SW_NORMAL);//restore应用程序
- m_ShowTitle = TRUE;
- return;
- }
- CDialog::OnSysCommand(nID, lParam);
- }
- float m_WidthScale;
- float m_HeightScale;
- BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
- {
- CRect rtCtrl,rtWnd;
- if(hwnd)
- {
- ::GetWindowRect(hwnd,&rtCtrl);
- ::GetWindowRect(GetParent(hwnd),&rtWnd);
- if((int)m_WidthScale==1 && m_WidthScale <1.001) return TRUE;
- if((int)m_WidthScale > 0)
- {
- rtCtrl.OffsetRect(-rtWnd.left-3,-rtWnd.top-28);
- }
- else
- {
- rtCtrl.OffsetRect(-rtWnd.left-2,-rtWnd.top-29);
- }
- float temp;
- temp= (float)rtCtrl.left*m_WidthScale;
- rtCtrl.left = (int)temp;
- temp= (float)rtCtrl.top*m_HeightScale;
- rtCtrl.top = (int)temp;
- temp = (float)rtCtrl.right*m_WidthScale;
- rtCtrl.right = (int)temp;
- temp =(float)rtCtrl.bottom*m_HeightScale;
- rtCtrl.bottom = (int)temp;
- ::MoveWindow(hwnd,rtCtrl.left,rtCtrl.top,
- rtCtrl.Width(),
- rtCtrl.Height(),
- TRUE);
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- void CTitleBarDlg::OnSize(UINT nType, int cx, int cy)
- {
- // CDialog::OnSize(nType, cx, cy);
- if(m_ReSizeFlag)
- {
- if(nType!=1)
- {
- CRect rtWnd;
- GetWindowRect(&rtWnd);
- m_WidthScale = (float)rtWnd.Width()/(float)m_OldWidth; // new/Old
- m_HeightScale = (float)rtWnd.Height()/(float)m_OldHeight;
- m_OldWidth = rtWnd.Width(); //保存新的Size;
- m_OldHeight = rtWnd.Height();
- HWND hWnd;
- hWnd = GetSafeHwnd();
- EnumChildWindows(hWnd,(WNDENUMPROC)EnumChildProc,0);
- }
- }
- // TODO: Add your message handler code here
- }
- void CTitleBarDlg::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CDialog::OnMouseMove(nFlags, point);
- }
- void CTitleBarDlg::OnOk()
- {
- //CyouDlg *pMyDlg;
- if(pMyDlg) delete pMyDlg;
- pMyDlg = new CyouDlg;
- pMyDlg->Create(IDD_DIALOG1);
- pMyDlg->ShowWindow(SW_SHOW);
- }
- void CTitleBarDlg::OnCancel()
- {
- // TODO: Add extra cleanup here
- this->DestroyWindow();
- if(pMyDlg) delete pMyDlg;
- }