以下代码中fbhandle和gothandle均为全局变量,可是,在使用中发现,fbhandle:=hd这一句是起作用的,而gothandle:='True';却不起作用,表现为每次按下热键时显示的gothandle值都是空的,而如果在FormCreate中加上gothandle:=‘false’的话显示的就是False,请问这是为什么?还有一小问:Boolean型如何转成String呢?function getw(hd:HWND;aform:TForm1):Boolean;stdcall;
var
  pid:dWord;
  classname,title:array[0..255] of char;
begin
  GetWindowText(hd,title,254);
  if Pos('MyForm',title)<>0 then
  begin
    gothandle:='True';
    fbhandle:=hd;
  end
  else
    gothandle:='False';
    Result:=True;
end;procedure tform1.ONHotkey(var message:Tmessage);
begin
  ShowMessage(gothandle);
  if gothandle='False' then
  begin
    EnumWindows(@getw,0);
    //……
   end
   else
   begin
    //……
   end;
end;

解决方案 »

  1.   

    function getw(hd: HWND; aform: TForm1): BOOL; stdcall;//Boolean -> BOOL;//如果是Boolean类型扫描停止不下begin
      GetWindowText(hd, title, SizeOf(title));
      if Pos('MyForm', title) <> 0 then
      begin
        gothandle := 'True';
        fbhandle := hd;
      end else gothandle := 'False';
      Result := gothandle <> 'True'; //<<<<<<<<<如果窗体找到则,停止扫描
    end;两对函数的用法参考
      if StrToBoolDef(gothandle, False) then
    //BoolToStr()
      

  2.   

    gothandle全局变量没有正确初始化
    Boolean型如何转成String:BoolToStr(B: Boolean; UseBoolStrs: Boolean = False)
      

  3.   

    //这样调试,就明白Boolean和BOOL的区别
    var
      vBoolean: Boolean;
      vBOOL: BOOL;
    begin
      vBoolean := True; //1
      vBOOL := True; //-1
      ShowMessage(Format('%d,%d', [Ord(vBoolean), Ord(vBOOL)]));
    end;
      

  4.   

    zswang(伴水清清)(专家门诊清洁工)的方法有效,但为什么Boolean就停不下呢?
      

  5.   

    汗,我就是怕你问才补上一条Boolean和BOOL的区别回调函数原型:
    BOOL CALLBACK EnumWindowsProc(
        HWND hwnd, // handle to parent window
        LPARAM lParam  // application-defined value
    );
    C里的类型和Pascal的类型并不一样
    考虑兼容,Delphi也增加了BOOL,声明的函数要和回调函数原型一致
      

  6.   

    Boolean是1个字节
    BOOL是4个字节
    Caption := Format('%d,%d', [SizeOf(Boolean), SizeOf(BOOL)]);你的函数使用Boolean
    当返回False时,只把1个字节赋直为0,但后面3个字节没改变,这3个字节中只要有一个不为0,就会使返回值非0(True)