Delphi2007中Combox控件中文乱码问题解决办法
2008-11-07 16:37
316 查看
方法1.把combobox的autocomplete属性设为false可以解决乱码问题。
方法2.乱码是因为你使用了TXPMan控件,且在NT系统中使用。
在KeyPress事件中执行ComboBox_XPMan_KeyPress可解决北问题。
function IsNTSystem:Boolean;
var
info:OSVERSIONINFO;
begin
info.dwOSVersionInfoSize:=sizeof(info);
GetVersionEx(info);
Result:=info.dwPlatformId=VER_PLATFORM_WIN32_NT;
end;
procedure ComboBox_XPMan_KeyPress(Sender:TObject;var Key: Char);
function HasSelectedText(CB_HWND:HWND; var StartPos, EndPos: DWORD): Boolean;
begin
SendMessage(CB_HWND, CB_GETEDITSEL, Integer(@StartPos),
Integer(@EndPos));
Result := EndPos > StartPos;
end;
var
StartPos, EndPos: DWORD;
OldText, SaveText: WideString; //关键在此,中间字符串要设为宽字符串型
LastByte: Integer;
TheCB:TComboBox;
CBHandle:HWND;
begin
if not IsNTSystem then Exit; //非NT系统则退出
TheCB:=TComboBox(Sender);
CBHandle:=TheCB.Handle;
if not TheCB.AutoComplete then exit;
if ord(Key) = VK_BACK then
begin
SaveText := TheCB.Text;
if HasSelectedText(CBHandle, StartPos, EndPos) then
begin
SendMessage(CBHandle,CB_GETEDITSEL,Integer(@StartPos),Integer(@EndPos));
Delete(SaveText, StartPos + 1, EndPos - StartPos);
SendMessage(CBHandle, CB_SETCURSEL, -1, 0);
TheCB.Text := SaveText;
SendMessage(CBHandle,CB_SETEDITSEL,0,MakeLParam(StartPos,StartPos));
Key := #0;
end
else if(TheCB.Style in [csDropDown,csSimple]) and (Length(SaveText)>0) then
begin
LastByte := StartPos;
OldText := Copy(SaveText, 1, LastByte - 1);
SendMessage(CBHandle, CB_SETCURSEL, -1, 0);
TheCB.Text := OldText + Copy(SaveText, EndPos + 1, MaxInt);
SendMessage(CBHandle, CB_SETEDITSEL, 0, MakeLParam(LastByte - 1,
LastByte - 1));
Key := #0;
end;
end;
end;
方法2.乱码是因为你使用了TXPMan控件,且在NT系统中使用。
在KeyPress事件中执行ComboBox_XPMan_KeyPress可解决北问题。
function IsNTSystem:Boolean;
var
info:OSVERSIONINFO;
begin
info.dwOSVersionInfoSize:=sizeof(info);
GetVersionEx(info);
Result:=info.dwPlatformId=VER_PLATFORM_WIN32_NT;
end;
procedure ComboBox_XPMan_KeyPress(Sender:TObject;var Key: Char);
function HasSelectedText(CB_HWND:HWND; var StartPos, EndPos: DWORD): Boolean;
begin
SendMessage(CB_HWND, CB_GETEDITSEL, Integer(@StartPos),
Integer(@EndPos));
Result := EndPos > StartPos;
end;
var
StartPos, EndPos: DWORD;
OldText, SaveText: WideString; //关键在此,中间字符串要设为宽字符串型
LastByte: Integer;
TheCB:TComboBox;
CBHandle:HWND;
begin
if not IsNTSystem then Exit; //非NT系统则退出
TheCB:=TComboBox(Sender);
CBHandle:=TheCB.Handle;
if not TheCB.AutoComplete then exit;
if ord(Key) = VK_BACK then
begin
SaveText := TheCB.Text;
if HasSelectedText(CBHandle, StartPos, EndPos) then
begin
SendMessage(CBHandle,CB_GETEDITSEL,Integer(@StartPos),Integer(@EndPos));
Delete(SaveText, StartPos + 1, EndPos - StartPos);
SendMessage(CBHandle, CB_SETCURSEL, -1, 0);
TheCB.Text := SaveText;
SendMessage(CBHandle,CB_SETEDITSEL,0,MakeLParam(StartPos,StartPos));
Key := #0;
end
else if(TheCB.Style in [csDropDown,csSimple]) and (Length(SaveText)>0) then
begin
LastByte := StartPos;
OldText := Copy(SaveText, 1, LastByte - 1);
SendMessage(CBHandle, CB_SETCURSEL, -1, 0);
TheCB.Text := OldText + Copy(SaveText, EndPos + 1, MaxInt);
SendMessage(CBHandle, CB_SETEDITSEL, 0, MakeLParam(LastByte - 1,
LastByte - 1));
Key := #0;
end;
end;
end;
相关文章推荐
- SmartUpload控件 中文乱码问题 jsp+servlet解决办法
- 数据库中文显示乱码问题解决办法
- struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)
- 关于jFinal开发中遇到的中文乱码问题解决办法
- PHP自带函数fgetcsv读取csv文件中文乱码问题解决办法
- cocos2d-x解决中文乱码问题的几种办法
- 解决URL地址中的中文乱码问题的办法
- 修改Linux下MySQL编码配置文件的办法,即中文乱码问题解决方法(实战篇)
- struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)
- Delphi xe5 控件TIdhttp的用法post,get解决中文乱码问题
- request.getParameter中文乱码问题解决办法
- freemarker 中文乱码问题解决办法
- struts2中form提交到action中的中文参数乱码问题解决办法(包括取中文路径)
- Sql Server2005 Varchar类型中文乱码问题的解决办法
- 基于Apache Tomee服务系统的web前端输入中文乱码问题解决办法
- PHP中strrev翻转中文乱码问题的解决办法
- url传中文乱码问题解决办法
- ubuntu gedit中文乱码的问题解决办法
- IntelliJ IDEA 中文乱码问题解决办法
- WEB开发中常见的几个中文乱码问题和解决办法