首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为tComboBox重置PasswordChar失败

为tComboBox重置PasswordChar失败
EN

Stack Overflow用户
提问于 2012-06-26 20:15:13
回答 1查看 544关注 0票数 2

我的用户正在组合框中输入密码,所以我希望显示*,而不是用户键入的内容。到目前为止没有问题。下面显示的例程可以完美地工作。但是,我也想让用户选择显示密码。当我用SetPasswordChar=false调用下面的例程时,它发送参数为零的EM_SETTPASSWORDCHAR。我希望组合框显示用户输入的文本。但它仍然显示*。知道我错过了什么吗?

代码语言:javascript
复制
//==============================================================================
// SetComboBoxPasswordChar
//------------------------------------------------------------------------------
// Set the Password Char for a tComboBox.
//
// This is done using by sending an EM_SETPASSWORDCHAR message to the edit box
// sub-control of the combo box.
//
//http://msdn.microsoft.com/en-us/library/windows/desktop/bb761653(v=vs.85).aspx
//
// wParam - The character to be displayed in place of the characters typed by
// the user. If this parameter is zero, the control removes the current password
// character and displays the characters typed by the user.
//==============================================================================

procedure SetComboBoxPasswordChar
            ( const nComboBox        : tComboBox;
              const nSetPasswordChar : boolean    );
var
  C : integer;
  H : tHandle;
begin

  // Get handle of the combo box

  H := nComboBox . Handle;

  // Get handle of the edit-box portion of the combo box

  H := GetWindow ( H, GW_CHILD );

  // If nSetPasswordChar is true,
  // set password char to asterisk
  // Otherwise, clear the password char

  if nSetPasswordChar then
    C := ord('*')
  else
    C := 0;

  SendMessage ( H, EM_SETPASSWORDCHAR, C, 0 );
end;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-26 22:27:53

我怀疑(并通过XE2中的一个快速测试应用程序证实)这是因为您只是假设编辑控件的HWNDGetWindow(H, GW_CHILD);返回的内容,而我认为这不是一个安全的假设。:-) COMBOBOX控件实际上由三个HWND值组成:一个用于整个控件,一个用于编辑部分,一个用于下拉列表。

获得所需句柄的更合适的方法是使用GetComboBoxInfo并使用COMBOBOXINFO结构的hwndItem成员:

代码语言:javascript
复制
var
  CBI: TComboBoxInfo;
begin
  // ..... Other code
  CBI.cbSize := SizeOf(CBI);
  H := nComboBox.Handle;
  if GetComboBoxInfo(H, CBI) then
    SendMessage(cbi.hwndItem, EM_SETPASSWORDCHAR, C, 0);
end;

要快速、简单地说明它的工作原理,请将TComboBox放到新的空白窗体上,为ComboBox1.OnDblClick事件处理程序添加一个事件处理程序,然后将以下代码添加到您的窗体中:

代码语言:javascript
复制
const
  PasswordChars: array[Boolean] of Integer = (0, Ord('*'));
var
  Ch: Integer = 0;
  UsePWdChar: Boolean = False;

procedure TForm1.ComboBox1DblClick(Sender: TObject);
var
  Ch: Integer;
  CBI: TComboBoxInfo;
begin
  CBI.cbSize := SizeOf(CBI);
  UsePwdChar := not UsePwdChar;
  Ch := PasswordChars[UsePwdChar];
  if GetComboBoxInfo(ComboBox1.Handle, CBI) then
    SendMessage(cbi.hwndItem, EM_SETPASSWORDCHAR, Ch, 0)
end;

这将使用ComboBox的编辑控件中的默认ComboBox1值,并在每次双击组合框时在密码字符*和none之间来回切换。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11207117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档