vc ++避免WM_GETTEXT消息从TextBox获取文本 [英] vc++ avoid WM_GETTEXT message to get the text from a TextBox

查看:205
本文介绍了vc ++避免WM_GETTEXT消息从TextBox获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当WM_GETTEXT消息发送到密码TextBox时,我试图避免在TextBox中发送文本.
我以包含TextBox(tbxPw)的形式编写的代码如下:

I''m trying to avoid sending the text inside a TextBox when WM_GETTEXT message is sent to a password TextBox.
The code I''ve written in the form that contains the TextBox (tbxPw) is the following:

[SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
virtual void WndProc(Message% m) override
{
    if (m.Msg == WM_GETTEXT)
    {
        char *buff = (char *) m.LParam.ToInt32();
        buff[m.Result.ToInt32()] = 0;
        String^ sTextInside = gcnew String(buff);
        if ( (sTextInside != "") && (sTextInside == this->tbxPw->Text) )
        {
            m.LParam = IntPtr(0);
            return;
        }
    }
    Form::WndProc(m);
}



当Windows发送消息WM_GETTEXT时,我尝试调试,结果是sTextInside始终为空字符串.

为什么我无法获取WM_GETTEXT消息试图检索的文本?
我尝试在开头和相同的结果上处理Form :: WndProc(m)(同样我也不知道这应该在开头还是结尾).



I tried to debug when windows sends the message WM_GETTEXT and the result is that sTextInside is always an empty string.

Why I can''t get the text that WM_GETTEXT message is trying to retrieve?
I tried processing Form::WndProc(m) at the begining and the same result (also I don''t know if this should be at the begining or at the end)

推荐答案

1)如果(0 == buff)(0 == buff[0])
sTextInside将为空 2)这是您的形式(不是this->tbxPw的形式)的反应
3)即将填充的缓冲区在这里,而不是被读取
4)确保还需要char而不是WCHAR
1) sTextInside will be empty in case of (0 == buff) or (0 == buff[0])
2) It is a reaction of your form (not of this->tbxPw)
3) The coming buffer is here to be filled in, not to be read
4) Be sure also you need char and not WCHAR


解决方案:(另请参见http://blog.tcx.be/2008/04/making-your-password-textbox-more.html [
The solution: (also see http://blog.tcx.be/2008/04/making-your-password-textbox-more.html[^])

#pragma once
#include <Windows.h>
using namespace System;
using namespace System::ComponentModel;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::Security::Permissions;
namespace InputForms
{
    /// <summary>Represents a text box control for entering passwords.</summary>
    [ToolboxItem(true), ToolboxBitmap(TextBox::typeid)]
    public ref class pwTextBox : public System::Windows::Forms::TextBox
    {
    public:
        /// <summary>Initializes a new instance of the <see cref="T:pwTextBox" /> class.</summary>
        pwTextBox(void) : TextBox()
        {
            this->_bAccessText = false;
            this->UseSystemPasswordChar = true;
        };
        /// <summary>Gets or sets the current text in the <see cref="T:TextBox"/>.</summary>
        /// <returns>The text displayed in the control.</returns>
        property String^ Text
        {
            virtual String^ get(void) override
            {
                this->_bAccessText = true;
                try { return TextBox::Text; }
                finally { this->_bAccessText = false; }
            }
            virtual void set(String^ value) override
            {
                this->_bAccessText = true;
                try { TextBox::Text = value; }
                finally { this->_bAccessText = false; }
            }
        };
        /// <summary>Gets the length of text in the control.</summary>
        /// <returns>The number of characters contained in the text of the control.</returns>
        property int TextLength
        {
            virtual int get(void) override
            {
                this->_bAccessText = true;
                try { return TextBox::TextLength; }
                finally { this->_bAccessText = false; }
            }
        };
    protected:
        /// <summary>Clean resources.</summary>
        ~pwTextBox()
        {
        }
        [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
        virtual void WndProc(Message% m) override
        {
            switch (m.Msg)
            {
                case WM_GETTEXT:
                case WM_GETTEXTLENGTH:
                    if (!this->_bAccessText)
                    {
                        m.Result = IntPtr::Zero;
                        return;
                    }
                    else break;
                case EM_SETPASSWORDCHAR: return;
            }
            TextBox::WndProc(m);
        };
    private:
        bool _bAccessText;
    };
}


这篇关于vc ++避免WM_GETTEXT消息从TextBox获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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