[原创]Edit控件使用方法小释(由ilikenba收集整理)
<P>本文由ilikenba收集整理</P><P>Edit控件作为密码输入框:</P>
<P>Edit控件有一个PasswordChar属性,它的默认值为“#0”,当改变</P>
<P>PasswordChar的值后,即不为“#0”时,
不显示所输入的字符,而以PasswordChar所设置的字符显示。当PasswordChar</P>
<P>的值为“#1”至“#9”时,
Edit也是密码输入框,当输入密码时,以一细竖条表示。剪切、复制对已设置为</P>
<P>密码输入状态的Edit毫无作用。
*****************************************************************
Edit内显示图像:</P>
<P>void __fastcall TForm1::Button1Click(TObject *Sender)
{
HDC dc;
dc=GetDC(Edit1->Handle); </P>
<P>BitBlt(dc,0,0,Image1->Width,Image1->Height,Image1->Picture->Bitma</P>
<P>p->Canvas->Handle,
0,0,SRCCOPY);
}
这里使用了BitBlt函数向Edit控件画图。
*****************************************************************
去掉Edit控件回车后的提示音:</P>
<P>void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if(Key==13)Key=0;
}
就是拦截输入键,将其值置为0.
*****************************************************************
去掉Edit进入时自动选择文本功能:</P>
<P>将Edit控件的AutoSelect属性设为False.
*****************************************************************
用代码设置Edit选择文本:</P>
<P>void __fastcall TForm1::Edit1Enter(TObject *Sender)
{
Edit1->SelStart=3;
Edit1->SelLength=5;
}
因为无论怎样设置属性Edit控件都不会从中间选择文本,所以只能编程实现。
因为SelStart是从0开始计数的。所以实际中是从第四个字符开始选择的。
*****************************************************************
在Edit输入回车键时跳到下一个控件:</P>
<P>void __fastcall TForm1::ProEnter(TObject *Sender, char &Key)
{
int i,od;
TEdit *ed;
if(Key!=13)return;
ed=(TEdit *)Sender;
od=ed->TabOrder;
for(i=0;i<ControlCount;i++)
{
ed=(TEdit *)Controls;
if(ed->TabOrder==(od+1))
{
ed->SetFocus();
break;
}
}
Key=0;
}
//---------------------------------------------------------------</P>
<P>------------
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
ProEnter(Sender,Key);
}
首先,通过键值判断是否为回车键,不是回车就不做处理。通过OnEnter事件的</P>
<P>Sender参数确定控件的TabOrder的值。寻找下一个Edit控件,当其TabOrder的</P>
<P>值比本控件大一时,将焦点设置在该控件上,最后,
将OnEnter事件所传递的键值Key设为0,既不让Edit控件处理改键,也消除了由</P>
<P>改键引起的提示音。
方法2:</P>
<P>void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if(Key == VK_RETURN)
{
SendMessage(this->Handle, WM_NEXTDLGCTL, 0, 0);
Key = 0;
}
}
这里的VK_RETURN可以换成下面的虚拟键值:
键定义:在 winuser.h
#define VK_SPACE 0x20
#define VK_PRIOR 0x21
#define VK_NEXT 0x22
#define VK_END 0x23
#define VK_HOME 0x24
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
#define VK_SELECT 0x29
#define VK_PRINT 0x2A
#define VK_EXECUTE 0x2B
#define VK_SNAPSHOT 0x2C
#define VK_INSERT 0x2D
#define VK_DELETE 0x2E
#define VK_HELP 0x2F
*****************************************************************
将Pannel上的所有的Edit控件的Text属性清空:</P>
<P>for(int i=0; i<Panel1->ControlCount; i++)
{
if( Panel1->Controls->ClassNameIs("TEdit"))
{
((TEdit *)Panel1->Controls)->Text = "";</P>
<P> }
}
*****************************************************************
Edit控件OnKeyPress和OnKeyDown事件对应的消息:</P>
<P>Edit得OnKeyPress事件对应的是Windows得WM_CHAR消息,
而OnKeyDown对应的就是WM_KEYDOWN消息。
*****************************************************************
动态创建Edit组件:</P>
<P>void __fastcall TForm1::Button1Click(TObject *Sender)
{
TEdit *ed;
ed=new TEdit(this);
ed->Parent=this;
ed->Left=10;
ed->Top=10;
}
Edit控件的Parent属性在动态生成时一定要赋值,否则它将无法显示。
*****************************************************************
得到其它窗体上的Edit控件的Text属性值:</P>
<P>void __fastcall TForm1::Button3Click(TObject *Sender)
{
HWND ff,tt;
char s;
ff=FindWindow(NULL,"运行");//首先运行标题为“运行”的窗体
if (ff!=0)
{
tt=GetNextDlgTabItem(ff,NULL,false);
SendMessage(tt,EM_SETPASSWORDCHAR,0,0);
SendMessage(tt,WM_GETTEXT,20,(long)(s));
Edit2->Text=String(s);
}
}
如果所要得到的Edit控件的Tab属性不是第一个可以多次利GetNextDlgTabItem
方法。</P>
页:
[1]