天天看點

MFC_vc++_vs2005對話框視窗靠邊自動隐藏

頭檔案:

// side_auto_hide_show_txwtechDlg.h : 頭檔案
//

#pragma once


// Cside_auto_hide_show_txwtechDlg 對話框
class Cside_auto_hide_show_txwtechDlg : public CDialog
{
// 構造
public:
	Cside_auto_hide_show_txwtechDlg(CWnd* pParent = NULL);	// 标準構造函數
    enum HidePosition{
	NO,  //非靠邊
	LEFT, //靠左
	RIGHT,//靠右
	TOP	  //靠上
    };
	DWORD m_lastActiveTime; //标記最後active 時間
// 對話框資料
	enum { IDD = IDD_SIDE_AUTO_HIDE_SHOW_TXWTECH_DIALOG };
     
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV 支援


// 實作
protected:
	HICON m_hIcon;

	// 生成的消息映射函數
	virtual BOOL OnInitDialog();
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedOk();
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnNcMouseMove(UINT nHitTest, CPoint point);
	bool isMouseInWindow();
	int m_screenX;
	int m_screenY;
	HidePosition m_hidePosition;
	void OnTimer(UINT nIDEvent);
	void DockedShow();
	void DockedHidden();
	CRect m_rect;
	afx_msg LRESULT OnNcHitTest(CPoint point);
};
           

cpp檔案:

// side_auto_hide_show_txwtechDlg.cpp : 實作檔案
//

#include "stdafx.h"
#include "side_auto_hide_show_txwtech.h"
#include "side_auto_hide_show_txwtechDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于應用程式“關于”菜單項的 CAboutDlg 對話框

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 對話框資料
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支援

// 實作
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// Cside_auto_hide_show_txwtechDlg 對話框




Cside_auto_hide_show_txwtechDlg::Cside_auto_hide_show_txwtechDlg(CWnd* pParent /*=NULL*/)
	: CDialog(Cside_auto_hide_show_txwtechDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void Cside_auto_hide_show_txwtechDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(Cside_auto_hide_show_txwtechDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, &Cside_auto_hide_show_txwtechDlg::OnBnClickedOk)
	ON_WM_MOUSEMOVE()
	ON_WM_NCMOUSEMOVE()
	ON_WM_NCHITTEST()
	ON_WM_TIMER()
END_MESSAGE_MAP()


// Cside_auto_hide_show_txwtechDlg 消息處理程式

BOOL Cside_auto_hide_show_txwtechDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“關于...”菜單項添加到系統菜單中。

	// IDM_ABOUTBOX 必須在系統指令範圍内。
	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);
		}
	}

	// 設定此對話框的圖示。當應用程式主視窗不是對話框時,架構将自動
	//  執行此操作
	SetIcon(m_hIcon, TRUE);			// 設定大圖示
	SetIcon(m_hIcon, FALSE);		// 設定小圖示

	// TODO: 在此添加額外的初始化代碼
	m_screenX=GetSystemMetrics (SM_CXSCREEN);
    m_screenY=GetSystemMetrics (SM_CYSCREEN);
    m_lastActiveTime=GetTickCount();
    m_hidePosition =HidePosition::NO;
    SetTimer(100,100,NULL);//本人喜歡把Timer 的Event 和間隔設定成一個,

	return TRUE;  // 除非将焦點設定到控件,否則傳回 TRUE
}
void Cside_auto_hide_show_txwtechDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	switch (nIDEvent)
	{
	case 100: 
		if(GetTickCount()-m_lastActiveTime < 300){//展示
			DockedShow();
		}else{//隐藏
			DockedHidden();
		}
		break;
	}
	CDialog::OnTimer(nIDEvent);
}

void Cside_auto_hide_show_txwtechDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向對話框添加最小化按鈕,則需要下面的代碼
//  來繪制該圖示。對于使用文檔/視圖模型的 MFC 應用程式,
//  這将由架構自動完成。

void Cside_auto_hide_show_txwtechDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于繪制的裝置上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使圖示在工作矩形中居中
		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;

		// 繪制圖示
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//當使用者拖動最小化視窗時系統調用此函數取得光标顯示。
//
HCURSOR Cside_auto_hide_show_txwtechDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void Cside_auto_hide_show_txwtechDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知處理程式代碼
	OnOK();
}

void Cside_auto_hide_show_txwtechDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息處理程式代碼和/或調用預設值
    m_lastActiveTime =GetTickCount();
	CDialog::OnMouseMove(nFlags, point);
}



void Cside_auto_hide_show_txwtechDlg::OnNcMouseMove(UINT nHitTest, CPoint point)
{
	// TODO: 在此添加消息處理程式代碼和/或調用預設值
    m_lastActiveTime =GetTickCount();
	CDialog::OnNcMouseMove(nHitTest, point);
}

bool Cside_auto_hide_show_txwtechDlg::isMouseInWindow()
{
	CRect rect;
	GetWindowRect(&rect);
	CPoint point;
	GetCursorPos(&point);
	return rect.PtInRect(point);
}
//停靠隐藏
void Cside_auto_hide_show_txwtechDlg::DockedHidden()
{
	CRect rect;
	GetWindowRect(&rect);
	if(m_hidePosition ==HidePosition::NO && !isMouseInWindow()){
		m_rect =rect;
		if(m_rect.top <=0 ){//靠頂
			m_hidePosition =HidePosition::TOP;
			ModifyStyle(WS_SYSMENU,NULL);
			this->SetWindowPos(NULL,m_rect.left,0,m_rect.right-m_rect.left,2,SWP_NOCOPYBITS);
			//下移
			//右移動
			m_rect.bottom -=m_rect.top;
			m_rect.top =0;
			
		}else if( m_rect.left <=0 ){ //靠左
			m_hidePosition =HidePosition::LEFT;
			ModifyStyle(WS_SYSMENU,NULL);
			this->SetWindowPos(NULL,0,m_rect.top,2,m_rect.bottom-m_rect.top,SWP_NOCOPYBITS);
			//右移動
			m_rect.right -=m_rect.left;
			m_rect.left =0;
			
		}else if( m_rect.right >= m_screenX){ //靠右
			m_hidePosition =HidePosition::RIGHT;
			ModifyStyle(WS_SYSMENU,NULL);
			this->SetWindowPos(NULL,m_screenX-2,m_rect.top,2,m_rect.bottom-m_rect.top,SWP_NOCOPYBITS);
			//左移動
			m_rect.left = m_screenX-(m_rect.right - m_rect.left);
			m_rect.right =m_screenX;//m_rect.left; //偏移量   m_rect.right - m_screenX
			
		}else{
			m_hidePosition =HidePosition::NO;
		}	
	}
}
 
 
//停靠顯示
void Cside_auto_hide_show_txwtechDlg::DockedShow()
{
	if(m_hidePosition !=HidePosition::NO &&isMouseInWindow()){//已隐藏,顯示舊值
		//恢複樣式
		ModifyStyle(NULL,WS_SYSMENU);
		//還原大小  5下,次處随意寫了一下,嘿嘿,按照勻速漸出
		int seq=0;
		switch(m_hidePosition){
		case HidePosition::TOP:
			while (++seq <= 5){
				this->SetWindowPos(NULL,m_rect.left,m_rect.top,m_rect.right-m_rect.left,(m_rect.bottom-m_rect.top)*seq/5,SWP_NOCOPYBITS);
			    Sleep(80);
			}
			break;
		case HidePosition::LEFT:
			while (++seq <= 5){
				this->SetWindowPos(NULL,m_rect.left,m_rect.top,(m_rect.right-m_rect.left)*seq/5,m_rect.bottom-m_rect.top,SWP_NOCOPYBITS);
				Sleep(80);		
			}
			break;
		case HidePosition::RIGHT:
			while (++seq <= 5){
				this->SetWindowPos(NULL,m_rect.left+(m_rect.right-m_rect.left)*(5-seq)/5,m_rect.top,m_rect.right-m_rect.left,m_rect.bottom-m_rect.top,SWP_NOCOPYBITS);
				Sleep(80);		
			}
			break;
		}
		m_hidePosition =HidePosition::NO;
	}
}

LRESULT Cside_auto_hide_show_txwtechDlg::OnNcHitTest(CPoint point)
{
	// TODO: 在此添加消息處理程式代碼和/或調用預設值
   return HTCAPTION;
	return CDialog::OnNcHitTest(point);
}
           
MFC_vc++_vs2005對話框視窗靠邊自動隐藏

參考:https://blog.csdn.net/suifenghuidong/article/details/13614953

需要項目源碼:可以留言郵箱

繼續閱讀