这是我以前的程序,可以跳到最前面
你试一下
if (Timer1.Interval<>3000) and (i<10) then
        begin
                Form1.Visible:=True;
                Application.BringToFront;
                ShowWindow(Application.Handle,SW_RESTORE);                Application.BringToFront;
                Timer1.Enabled:=False;
                Timer1.Interval:=3000;
                Timer1.Enabled:=True;
                i:=i+1;
        end;
        
        if i>=10 then
        begin
                i:=0;
                Timer1.Enabled:=False;
                Timer1.Interval:=24000000;
                Timer1.Enabled:=True;
                Form1.Visible:=False;
                ShowWindow(Application.Handle,SW_Hide);
        end;
        
        if Application.MessageBox('You Should Have A Rest','Rest',MB_OKCANCEL )=ID_OK then
        begin
                i:=0;
                Timer1.Enabled:=False;
                Timer1.Interval:=24000000;
                Timer1.Enabled:=True;
                Form1.Visible:=False;
                ShowWindow(Application.Handle,SW_Hide);
        end;

解决方案 »

  1.   

    参考一下,是用消息的
    unit MultInst;interfaceconst
      MI_QUERYWINDOWHANDLE   = 1;
      MI_RESPONDWINDOWHANDLE = 2;  MI_ERROR_NONE          = 0;
      MI_ERROR_FAILSUBCLASS  = 1;
      MI_ERROR_CREATINGMUTEX = 2;// Call this function to determine if error occurred in startup.
    // Value will be one or more of the MI_ERROR_* error flags.
    function GetMIError: Integer;implementationuses Forms, Windows, SysUtils;const
      UniqueAppStr = 'DDG.I_am_the_Eggman!';var
      MessageId: Integer;
      WProc: TFNWndProc;
      MutHandle: THandle;
      MIError: Integer;function GetMIError: Integer;
    begin
      Result := MIError;
    end;function NewWndProc(Handle: HWND; Msg: Integer; wParam, lParam: Longint):
      Longint; stdcall;
    begin
      Result := 0;
      // If this is the registered message...
      if Msg = MessageID then
      begin
        case wParam of
          MI_QUERYWINDOWHANDLE:
            // A new instance is asking for main window handle in order
            // to focus the main window, so normalize app and send back
            // message with main window handle.
            begin
              if IsIconic(Application.Handle) then
              begin
                Application.MainForm.WindowState := wsNormal;
                Application.Restore;
              end;
              PostMessage(HWND(lParam), MessageID, MI_RESPONDWINDOWHANDLE,
                Application.MainForm.Handle);
            end;
          MI_RESPONDWINDOWHANDLE:
            // The running instance has returned its main window handle,
            // so we need to focus it and go away.
            begin
              SetForegroundWindow(HWND(lParam));
              Application.Terminate;
            end;
        end;
      end
      // Otherwise, pass message on to old window proc
      else
        Result := CallWindowProc(WProc, Handle, Msg, wParam, lParam);
    end;procedure SubClassApplication;
    begin
      // We subclass Application window procedure so that
      // Application.OnMessage remains available for user.
      WProc := TFNWndProc(SetWindowLong(Application.Handle, GWL_WNDPROC,
        Longint(@NewWndProc)));
      // Set appropriate error flag if error condition occurred
      if WProc = nil then
        MIError := MIError or MI_ERROR_FAILSUBCLASS;
    end;procedure DoFirstInstance;
    // This is called only for the first instance of the application
    begin
      // Create the mutex with the (hopefully) unique string
      MutHandle := CreateMutex(nil, False, UniqueAppStr);
      if MutHandle = 0 then
        MIError := MIError or MI_ERROR_CREATINGMUTEX;
    end;procedure BroadcastFocusMessage;
    // This is called when there is already an instance running.
    var
      BSMRecipients: DWORD;
    begin
      // Prevent main form from flashing
      Application.ShowMainForm := False;
      // Post message to try to establish a dialogue with previous instance
      BSMRecipients := BSM_APPLICATIONS;
      BroadCastSystemMessage(BSF_IGNORECURRENTTASK or BSF_POSTMESSAGE,
        @BSMRecipients, MessageID, MI_QUERYWINDOWHANDLE,
        Application.Handle);
    end;procedure InitInstance;
    begin
      SubClassApplication;   // hook application message loop
      MutHandle := OpenMutex(MUTEX_ALL_ACCESS, False, UniqueAppStr);
      if MutHandle = 0 then
        // Mutex object has not yet been created, meaning that no previous
        // instance has been created.
        DoFirstInstance
      else
        BroadcastFocusMessage;
    end;initialization
      MessageID := RegisterWindowMessage(UniqueAppStr);
      InitInstance;
    finalization
      // Restore old application window procedure
      if WProc <> Nil then
        SetWindowLong(Application.Handle, GWL_WNDPROC, LongInt(WProc));
      if MutHandle <> 0 then CloseHandle(MutHandle);  // Free mutex
    end.
      

  2.   

    赫赫,这个原因是因为M$更改了bringwindowtotop的代码,凡是调用这个API的窗口,只能够使非本程序的窗口跳到前面,自己调用自己是不会跳到最前面的!有几个方法可以跳过这个地方!
    1:用一个SwitchToThisWindow() 未公开的API可以做到。
    2:修改系统规则。代码?我忘记了~~~~~~~~~~~~~~~
      

  3.   

    The foreground window is the window at the top of the Z order. It is the window that the user is working with. In a preemptive multitasking environment, you should generally let the user control which window is the foreground window. However, an application can call SetForegroundWindow if it wants to put itself into the foreground to display a critical error or information that requires the user's immediate attention. A good example is a debugger when it hits a breakpoint. 
    上面的说明里明明说可以是itself的啊?
    不行把下面两条合起来试试:
            SetForegroundWindow(TargetWnd);
            SetActiveWindow(TarGetWnd);
      

  4.   

    即使topmost这个窗口也只能让它显示在最前面,它的标题栏还是灰色的,没有用.
      

  5.   

    最好是给个例子,如一个窗口加一个定时器,定时器的interval为4000,在定时器事件中写
    bringwindowtotop之类的代码,谢谢.
      

  6.   

    无数帮助文件说明这不应该很难,无数事实说明是实在很难,没有人会吗?
    upupup
      

  7.   

    upupupupupup,
    欢迎各位前来捧场,
      

  8.   

    在OnTimer 里面放入下面的代码:        CloseWindow(Application.Handle);
            SwitchToThisWindow(Application.handle);
            EnableWindow(Application.Handle,True);注:其中SwitchToThisWindow函数的声明为:
    procedure SwitchToThisWindow(handle:THandle);cdecl;external 'user32.dll' name 'SwitchToThisWindow';
    很奇怪的是,这里CloseWindow一句是必须的,如果没有这一句,当有其他窗口在前面的时候
    就不行了。
      

  9.   

    to flypuma:
    见鬼了,在我的机上还是不行,先是最小化了窗口,然后再打开窗口,然后就一个劲的在任务栏上闪呀闪的,就是switch不了它自己.会不会跟我的系统有关?我的系统是2000+ie6+sp2+visual .net studio+visual studio+delphi6+office xp+其他
    ie6很变态,有时会莫名其妙的自动关闭,清华紫光输入法使用时,屏幕有时候会一闪一闪的
    能否指点指点?
      

  10.   

    你的机器配置太新潮了,老朽赶不上啦,唉....我的是98+IE5+Delphi5+Office97+Visual
    Stadio6+Rose+PhoteShop5+Oracle+Informix+XMLSpy+Project98+ErWin+PowerDVD+金山结巴2000...
    唉,全是老掉牙的东西....:(
      

  11.   

    发现一个2000下面的方法,试一下 
    function  AllowSetForegroundWindow( dwProcessId:DWORD): BOOL; stdcall;implementation
    function AllowSetForegroundWindow; external  'user32.dll' name 'AllowSetForegroundWindow';具体看一下 MSDN上的帮助,以前没有看,偶然一看,上面的道理讲的正是透彻,呵呵
      

  12.   

    从Win98开始,微软更改了系统代码,一般的SetForegroundWindow只能将
    状态栏中应用按钮闪烁,并没有将应用调到最前面。请使用下列函数:function ForceForegroundWindow(hwnd: THandle): boolean;
    const
       SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
       SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
    var
       timeout: DWORD;
    begin
       if ((Win32Platform = VER_PLATFORM_WIN32_NT) and (Win32MajorVersion
    > 4)) or
          ((Win32Platform = VER_PLATFORM_WIN32_WINDOWS) and
          ((Win32MajorVersion > 4) or ((Win32MajorVersion = 4) and
    (Win32MinorVersion > 0)))) then begin
          SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, @timeout,
    0);
          SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
    TObject(0), SPIF_SENDCHANGE);
          Result := SetForegroundWindow(hWnd);
          SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
    TObject(timeout), SPIF_SENDCHANGE);
          end
       else
          Result := SetForegroundWindow(hWnd);
    end; { ForceForegroundWindow }