简体   繁体   English

WIN32,C ++:是否可以在不隐藏窗口的情况下为窗口设置动画?

[英]WIN32, C++: Is it possible to animate a window without hiding it?

I Have an edit control (a text field) which I want to animate. 我有一个编辑控件(文本字段),我想要动画。 The animation I want is that it slides out, creating an extra line for this text field. 我想要的动画是它滑出来,为这个文本字段创建一个额外的行。 I am able to animate my text field and able to make it larger, however to show the sliding animation I first have to hide it. 我可以为我的文本字段设置动画并使其更大,但是为了显示我首先要隐藏它的滑动动画。 This means the entire text fields slides out as if being created for the first time from nothing, instead of just adding a new line. 这意味着整个文本字段滑出,好像是第一次从零开始创建,而不是仅添加新行。

This is the code I have now: 这是我现在的代码:

SetWindowPos(hwnd, HWND_TOP, x, y, newWidth, newHeight, SWP_DRAWFRAME);

ShowWindow(hwnd, SW_HIDE);

AnimateWindow(hwnd, 300, AW_SLIDE | AW_VER_NEGATIVE);

Is it possible to show this animation without hiding it? 是否可以在不隐藏它的情况下显示此动画?

To expand on Nick D's answer, here's the code to achieve what you're looking for... 为了扩展Nick D的答案,这里是实现你正在寻找的代码......

.h 。H

#define ANIMATION_TIMER 1234
#define ANIMATION_LIMIT 8
#define ANIMATION_OFFSET 4

int m_nAnimationCount;

.cpp 的.cpp

void CExampleDlg::OnTimer(UINT_PTR nIDEvent)
{
    if (nIDEvent == ANIMATION_TIMER)
    {
        if (++m_nAnimationCount > ANIMATION_LIMIT)
            KillTimer(EXPAND_TIMER);
        else
        {
            CRect rcExpand;
            m_edtExpand.GetWindowRect(rcExpand);
            ScreenToClient(rcExpand);

            rcExpand.bottom += ANIMATION_OFFSET;

            m_edtExpand.MoveWindow(rcExpand);
        }   
    }

    CDialog::OnTimer(nIDEvent);
}

void CExampleDlg::OnStartAnimation()
{
    m_nAnimationCount = 0;
    SetTimer(ANIMATION_TIMER, 20, NULL);
}

Don't forget to set the Multiline property on the edit control (m_edtExpand) 不要忘记在编辑控件上设置Multiline属性(m_edtExpand)

另一种方法是使用SetTimer函数模拟动画,该函数将调用例程以递增方式调整窗口大小。

I think it's not possible to do with the built-in AnimateWindow API. 我认为使用内置的AnimateWindow API是不可能的。 The MSDN entry to AnimateWindow http://msdn.microsoft.com/en-us/library/ms632669(VS.85).aspx says it is used to "produce special effects when showing or hiding windows", and the AW_HIDE flag determines that the function either shows or hides a window. AnimateWindow的MSDN条目http://msdn.microsoft.com/en-us/library/ms632669(VS.85).aspx表示它用于“在显示或隐藏窗口时产生特殊效果”,AW_HIDE标志确定该功能显示或隐藏窗口。 And I can't see any alternative built-in function to do what you want. 而且我看不到任何替代的内置函数来做你想要的。

Thus, Nick D. and Alan have the right approach of coding the resizing yourself. 因此,Nick D.和Alan有正确的方法来自己调整大小。 This is often the solution. 这通常是解决方案。 (I had never heard of AnimateWindow before.) I assume AnimateWindow does something very similar internally, though I assume it's much more reliable. (我之前从未听说过AnimateWindow。)我假设AnimateWindow在内部做了类似的事情,尽管我认为它更可靠。

You also obviously need to make sure the Timer does the right thing if another line is added or removed to the text box, or its otherwise resized, before it's finished animating. 显然,在完成动画制作之前,如果在文本框中添加或删除了另一行,或者其他行已调整大小,则显然需要确保Timer做正确的事情。

And also give serious thought to making the animation low priority, if it's inconvenient to code. 如果编码不方便,还要认真考虑将动画设置为低优先级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM