按ALT键为啥会跳出主窗体?(50分)

  • 主题发起人 主题发起人 oldnew
  • 开始时间 开始时间
O

oldnew

Unregistered / Unconfirmed
GUEST, unregistred user!
新建一个project,两个窗体Form1、Form2,其中Form1为窗体。
Form1中建一个按钮btn1,点击事件如下:
procedure TForm1.btn1Click(Sender: TObject);
var
f: TForm2;
begin
f := TForm2.Create(Self);
f.Show;
end;

问题来了:程序运行后,当点击btn1后,显示Form2窗体,此时按一下ALT键,会弹出Form1窗体。
我晕了!这是为啥啊? 如果想不弹出Form1窗体,应该怎么做呢?
 
改成
procedure TForm1.btn1Click(Sender: TObject);
var
f: TForm2;
begin
f := TForm2.Create(Self);
f.ShowModal;
end;
 
procedure TForm1.btn1Click(Sender: TObject);
var
f: TForm2;
begin
f := TForm2.Create(Self);
f.Show;
///////////////////////
Form1.Enabled := False;
//////////////////////
end;
这样可行么?
 
二楼的可行
begin
Form2.ShowModal;
end;
 
to japhe: 我就是要Show不要ShowModal歪,OK?
to ricoo:这种歪招不可取吧?这样主窗口根本没法点了
 
to 兰凌:我本来就是在写多窗体的程序,一定要Show滴,不要ShowModal
 
那你把form2的ALT按键事件屏蔽掉好了。
 
我晕! 在哪里写?
 
下面两个任选一个.应该能满足你!记得俺的分数^&^
procedure TForm2.FormKeyDown(Sender: TObject
var Key: Word;
Shift: TShiftState);
begin
if key = VK_MENU then
begin
key := word(#0);
end;
end;

procedure TForm2.FormKeyUp(Sender: TObject
var Key: Word;
Shift: TShiftState);
begin
if key = VK_MENU then
begin
key := word(#0);
end;
end;
 
这个方法还要设置KeyPreview啊!

这是治标不治本的方法,有没有更好的办法啊? 大伙……
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls


type
TForm1 = class(TForm)
Button1: TButton

procedure FormCreate(Sender: TObject)

procedure FormDestroy(Sender: TObject)

procedure Button1Click(Sender: TObject)

private
{ Private declarations }
public
{ Public declarations }
end

KBDLLHOOKSTRUCT=record
vkCode:DWORD

scanCode:DWORD

flags:DWORD

time:DWORD

dwExtraInfo:ulong

end

LPKBDLLHOOKSTRUCT=^KBDLLHOOKSTRUCT


var
Form1: TForm1

//定义句柄
OldHook:HHOOK;// 钩子 句柄

implementation

{$R *.dfm}
const
LLKHF_ALTDOWN=$20

WH_KEYBOARD_LL= 13


//键盘钩子
function KeyProc(code: Integer
wparam: WPARAM
lparam: LPARAM): LRESULT stdcall

var
pkbhs:LPKBDLLHOOKSTRUCT

begin


Result:=1

if (code < 0) then
begin
Result:= CallNextHookEx(OldHook,code, wParam, lParam)

exit

end


pkbhs := LPKBDLLHOOKSTRUCT( lParam)


if pkbhs.flags and LLKHF_ALTDOWN>0 then
begin
exit
//封锁 ALT
end



Result:=CallNextHookEx(OldHook, code, wParam, lParam)

end


//---------------------------------------------------------------------------

procedure TForm1.FormCreate(Sender: TObject)

begin
OldHook:=0

end


procedure TForm1.FormDestroy(Sender: TObject);
begin
if OldHook>0 then
UnhookWindowsHookEx(OldHook)

end


procedure TForm1.Button1Click(Sender: TObject)

begin
//注册 钩子
OldHook := SetWindowsHookEx(WH_KEYBOARD_LL {WH_KEYBOARD_LL},
KeyProc, HInstance, 0)

if (Sender is TButton) then
TButton(Sender).Enabled:=not (OldHook>0)


end


end.
 

Similar threads

D
回复
0
查看
825
DelphiTeacher的专栏
D
D
回复
0
查看
831
DelphiTeacher的专栏
D
D
回复
0
查看
664
DelphiTeacher的专栏
D
D
回复
0
查看
541
DelphiTeacher的专栏
D
后退
顶部