熟悉WinApi的大侠快进来帮一下忙啊(50分)

  • 主题发起人 主题发起人 balaschen
  • 开始时间 开始时间
B

balaschen

Unregistered / Unconfirmed
GUEST, unregistred user!
在Form上放一按扭,onclick事件写如下代码:<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; dwStyle:DWord;<br>&nbsp; FEditWnd:HWND;<br>&nbsp; TextRect:TRect;<br>begin<br>&nbsp; dwStyle := WS_VISIBLE or WS_CHILD or ES_MULTILINE or ES_LEFT or ES_AUTOVSCROLL;<br>&nbsp; dwStyle:=dwStyle or ES_WANTRETURN;<br>&nbsp; FEditWnd := CreateWindow('EDIT', '', dwStyle, 0, 0, 0, 0, Handle, 0, hInstance, nil);<br>&nbsp; TextRect:=Rect(10,10,60,80);<br>&nbsp; MoveWindow(FEditWnd,TextRect.left,TextRect.Top,TextRect.Right-TextRect.Left,TextRect.Bottom-TextRect.Top,True);<br>&nbsp; ShowWindow(FEditWnd, SW_SHOWNORMAL);<br>&nbsp; Windows.SetFocus(FEditWnd);<br>end;<br>为什么创建的Edit编辑框无法用左右键和回车键??
 
不要尝试去干这样的事情(CreateWindow来生成EDIT),因为键盘消息早被DELPHI处理掉了,<br>Delphi只支持他自己的TWinControl<br>controls.pas CNKeyDown and CNKeyUp<br>procedure TWinControl.CNKeyDown(var Message: TWMKeyDown);<br>var<br>&nbsp; Mask: Integer;<br>begin<br>&nbsp; with Message do<br>&nbsp; begin<br>&nbsp; &nbsp; Result := 1;<br>&nbsp; &nbsp; if IsMenuKey(Message) then Exit;<br>&nbsp; &nbsp; if not (csDesigning in ComponentState) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if Perform(CM_CHILDKEY, CharCode, Integer(Self)) &lt;&gt; 0 then Exit;<br>&nbsp; &nbsp; &nbsp; Mask := 0;<br>&nbsp; &nbsp; &nbsp; case CharCode of<br>&nbsp; &nbsp; &nbsp; &nbsp; VK_TAB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mask := DLGC_WANTTAB;<br>&nbsp; &nbsp; &nbsp; &nbsp; VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mask := DLGC_WANTARROWS;<br>&nbsp; &nbsp; &nbsp; &nbsp; VK_RETURN, VK_EXECUTE, VK_ESCAPE, VK_CANCEL:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mask := DLGC_WANTALLKEYS;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; if (Mask &lt;&gt; 0) and<br>&nbsp; &nbsp; &nbsp; &nbsp; (Perform(CM_WANTSPECIALKEY, CharCode, 0) = 0) and<br>&nbsp; &nbsp; &nbsp; &nbsp; (Perform(WM_GETDLGCODE, 0, 0) and Mask = 0) and<br>&nbsp; &nbsp; &nbsp; &nbsp; (GetParentForm(Self).Perform(CM_DIALOGKEY,<br>&nbsp; &nbsp; &nbsp; &nbsp; CharCode, KeyData) &lt;&gt; 0) then Exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Result := 0;<br>&nbsp; end;<br>end;
 
To wenyue:<br>&nbsp; 为什么如果Form上没有任何可以获取焦点的控件,可以?我上面的那段代码你可以<br>在Form的OnShow事件里写,Form上不要放任何东西。
 
你要知道Delphi 用来控制控件的Focus使用了哪些键,你就可以猜测到了<br>关键在于:<br>if (Mask &lt;&gt; 0) and<br>&nbsp; &nbsp; &nbsp; &nbsp; (Perform(CM_WANTSPECIALKEY, CharCode, 0) = 0) and<br>&nbsp; &nbsp; &nbsp; &nbsp; (Perform(WM_GETDLGCODE, 0, 0) and Mask = 0) and<br>&nbsp; &nbsp; &nbsp; &nbsp; (GetParentForm(Self).Perform(CM_DIALOGKEY,<br>&nbsp; &nbsp; &nbsp; &nbsp; CharCode, KeyData) &lt;&gt; 0) then Exit;<br>他决定是否过滤VK_LEFT ....<br>procedure TCustomForm.CMDialogKey(var Message: TCMDialogKey);<br>begin<br>&nbsp; if GetKeyState(VK_MENU) &gt;= 0 then<br>&nbsp; &nbsp; with Message do<br>&nbsp; &nbsp; &nbsp; case CharCode of<br>&nbsp; &nbsp; &nbsp; &nbsp; VK_TAB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if GetKeyState(VK_CONTROL) &gt;= 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SelectNext(FActiveControl, GetKeyState(VK_SHIFT) &gt;= 0, True);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FActiveControl &lt;&gt; nil then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TForm(FActiveControl.Parent).SelectNext(FActiveControl,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (CharCode = VK_RIGHT) or (CharCode = VK_DOWN), False);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Result := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; inherited;<br>end;<br>
 
接受答案了.
 
to balaschen:对不起,我开始网不通,现在才来
 
后退
顶部