C ++ MFC按钮在窗口调整大小时消失 [英] C++ MFC button disappears at window resize

查看:268
本文介绍了C ++ MFC按钮在窗口调整大小时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MFC C ++中有一个附加了CButton的对话框. 我想修改OnSize()以便按钮将锚定在左下角.

I have a Dialog in MFC C++ that has a CButton attached. I want to modify OnSize() so that the button will anchor to bottom-left.

if (btn.m_hWnd) {
        CRect winRect;
        GetWindowRect(&winRect);

        int width = winRect.right - winRect.left;
        int height = winRect.bottom - winRect.top;

        int x = width - wndWidth;
        int y = height - wndHeight;


        CRect rect;
        btn.GetWindowRect(&rect);
        rect.left += x;
        rect.top += y;
        btn.MoveWindow(&rect);
        ScreenToClient(&rect);
        btn.ShowWindow(SW_SHOW);
    }

x和y是窗口已更改的数量之差,并将被添加到按钮的起始坐标.

x and y are the difference of how much the window has changed and will be added to the button's start coordinates.

我不确定最后2条命令(我也可以删除它们),但是随后我运行该程序,按钮消失了.

I am not sure about the last 2 commands (I might as well delete them) but then I run the program the button disappears.

我需要知道一种将按钮移动x和y的方法.

I need to know a way to move the button by x and y.

推荐答案

原始代码为父对话框和按钮使用了错误的坐标系.

The original code was using the wrong coordinate system for both the parent dialog, and the button.

停靠在左下角的正确方法如下:

A correct way to dock to the bottom left would be like this:

if (btn.m_hWnd) { 
    CRect winRect; 
    GetClientRect(&winRect); 

    CRect rect; 
    btn.GetWindowRect(&rect); 
    ScreenToClient(&rect); 

    int btnWidth = rect.Width();
    int btnHeight = rect.Width();
    rect.left = winRect.right-btnWidth; 
    rect.top  = winRect.bottom-btnHeight;
    rect.right = winRect.right;
    rect.bottom = winRect.bottom; 
    btn.MoveWindow(&rect); 
}

OR

if (btn.m_hWnd) { 
    CRect winRect; 
    GetClientRect(&winRect); 

    CRect rect; 
    btn.GetWindowRect(&rect); 
    ScreenToClient(&rect); 

    int btnWidth = rect.Width();
    int btnHeight = rect.Width();
    btn.SetWindowPos(NULL,winRect.right-btnWidth,winRect.bottom-btnHeight,0,0,SWP_NOSIZE|SWP_NOZORDER);
}

这篇关于C ++ MFC按钮在窗口调整大小时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆