首页 > 代码库 > MFC 窗口移动、置顶、浮起效果

MFC 窗口移动、置顶、浮起效果

技术分享

 

void CMFCMoveWindowDlg::OnBnClickedButton1()
{
    //left 
    CRect rec;
    GetWindowRect(&rec);
    rec.left -=  m_iMove;
    rec.right -= m_iMove;
    MoveWindow(&rec);    
}

void CMFCMoveWindowDlg::OnBnClickedButton3()
{
    //right 
    CRect rec;
    GetWindowRect(&rec);
    rec.left +=  m_iMove;
    rec.right += m_iMove;
    MoveWindow(&rec);    
}

void CMFCMoveWindowDlg::OnBnClickedButton2()
{
    //up 
    CRect rec;
    GetWindowRect(&rec);
    rec.top -= m_iMove;
    rec.bottom -= m_iMove;
    MoveWindow(&rec);    
}

void CMFCMoveWindowDlg::OnBnClickedButton4()
{
    //down
    CRect rec;
    GetWindowRect(&rec);
    rec.top += m_iMove;
    rec.bottom += m_iMove;
    MoveWindow(&rec);    
}

void CMFCMoveWindowDlg::OnEnChangeEdit1()
{
    // TODO:  如果该控件是 RICHEDIT 控件,它将不
    // 发送此通知,除非重写 CDialog::OnInitDialog()
    // 函数并调用 CRichEditCtrl().SetEventMask(),
    // 同时将 ENM_CHANGE 标志“或”运算到掩码中。

    m_iMove = GetDlgItemInt(IDC_EDIT1);
}

void CMFCMoveWindowDlg::OnBnClickedButton5()
{
    if (!m_bTopWindow)
    {
        ::SetWindowPos(
            m_hWnd, 
            HWND_TOPMOST, 
            0, 0, 0, 0, 
            SWP_NOMOVE | SWP_NOSIZE);    
        SetDlgItemText(IDC_BUTTON5, "Cancel");
    }
    else
    {
        ::SetWindowPos(
            m_hWnd, 
            HWND_NOTOPMOST, 
            0, 0, 0, 0, 
            SWP_NOMOVE | SWP_NOSIZE);    
        SetDlgItemText(IDC_BUTTON5, "SetTopWindow");
    }
    m_bTopWindow = !m_bTopWindow;
}

 

void CMFCMoveWindowDlg::OnBnClickedButton6()
{
    if (!m_bCreate)
    {
        m_bCreate = !m_bCreate;
        m_dlgShow.Create(IDD_DIALOG1, NULL);        
    }
    m_dlgShow.ShowWindow(SW_SHOW);
}


//subDlg.cpp
BOOL CDiaShow::OnInitDialog()
{
    CDialog::OnInitDialog();

    // 获得桌面大小
    CRect rectWorkArea;
    SystemParametersInfo(SPI_GETWORKAREA, 0, &rectWorkArea, SPIF_SENDCHANGE);   

    // 获得对话框大小
    CRect rectDlg;
    GetWindowRect(&rectDlg);

    // 将窗口设置到右下角
    ::SetWindowPos(m_hWnd, 
        HWND_BOTTOM,
        rectWorkArea.right - rectDlg.Width(),
        rectWorkArea.bottom - rectDlg.Height(),
        rectDlg.Width(),
        rectDlg.Height(),
        SWP_NOZORDER);

    //浮起效果
    ::AnimateWindow(m_hWnd, 1000, AW_VER_NEGATIVE|AW_SLIDE);

    return TRUE;  // return TRUE unless you set the focus to a control
    // 异常: OCX 属性页应返回 FALSE
}

 

MFC 窗口移动、置顶、浮起效果