使用CreateDesktop / SwitchDesktop在新桌面中创建表单

时间:2013-05-02 21:24:21

标签: c++ windows delphi winapi

我需要为实用程序创建一个系统模式窗体,它应该阻止整个窗口,直到输入某些值。所以我正在尝试创建桌面和切换。到目前为止,创建一个桌面切换到它,并返回工作正常。

但是,当我尝试在新线程中创建表单时,表单没有显示但是应用程序保留在新创建的空白桌面中,因此在我注销之前永远阻止屏幕。

我根据此处的代码制作了它:

http://developex.com/blog/system-modal-back/

// ScreenLocker.h

#pragma once

using namespace System;
using namespace System::Windows::Forms;

namespace Developex
{
   public ref class ScreenLocker
   {
   private:
      String ^_desktopName;
      Form ^_form;
      void DialogThread(void);

   public:
      static void ShowSystemModalDialog (String ^desktopName, Form ^form);
   };
}


// ScreenLocker.cpp

#include "stdafx.h"
#include "ScreenLocker.h"

using namespace System::Threading;
using namespace System::Runtime::InteropServices;

namespace Developex
{
   void ScreenLocker::DialogThread()
   {
      // Save the handle to the current desktop
      HDESK hDeskOld = GetThreadDesktop(GetCurrentThreadId());

      // Create a new desktop
      IntPtr ptr = Marshal::StringToHGlobalUni(_desktopName);
      HDESK hDesk = CreateDesktop((LPCWSTR)ptr.ToPointer(),
         NULL, NULL, 0, GENERIC_ALL, NULL);
       Marshal::FreeHGlobal(ptr);

      // Switch to the new deskop
      SwitchDesktop(hDesk);

      // Assign new desktop to the current thread
      SetThreadDesktop(hDesk);

      // Run the dialog
      Application::Run(_form);

      // Switch back to the initial desktop
      SwitchDesktop(hDeskOld);
      CloseDesktop(hDesk);
   }

   void ScreenLocker::ShowSystemModalDialog(String ^desktopName, Form ^form)
   {
     // Create and init ScreenLocker instance
      ScreenLocker ^locker = gcnew ScreenLocker();
      locker->_desktopName = desktopName;
      locker->_form = form;

      // Create a new thread for the dialog
      (gcnew Thread(gcnew ThreadStart(locker,
         &Developex::ScreenLocker::DialogThread)))->Start();
   }
}

好吧,现在我正试图将其“翻译”为Delphi,到目前为止这就是我所拥有的:

unit Utils;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ADODB, Grids, DBGrids, ExtCtrls, ComCtrls, SyncObjs, ShellApi,
  AddTimeU;

type
  TFormShowThread = class(TThread)
    HDesktopglobal: HDESK;
    hDeskOld: HDESK;

    UHeapSize: ULong;
    tempDword: DWORD;

    frm : TfrmBlockScreen;
  private
  protected
    procedure Execute; override;
  public
    constructor Create(form : TfrmBlockScreen);
    destructor Destroy; override;
  end;

implementation

constructor TFormShowThread.Create(form : TfrmBlockScreen);
begin
  FreeOnTerminate := True;
  inherited Create(True);
  frm := form;
end;


destructor TFormShowThread.Destroy;
begin
  inherited;
end;

procedure TFormShowThread.Execute;
begin
    hDeskOld := GetThreadDesktop(GetCurrentThreadId());

    HDesktopglobal := CreateDesktop('Z', nil, nil, 0, GENERIC_ALL, nil);

    SwitchDesktop(HDesktopglobal);
    SetThreadDesktop(HDesktopglobal);

    // tried this
    Application.CreateForm(TfrmBlockScreen, frm);

    // also tried this with same result
    //frm := TfrmBlockScreen.Create(nil);
    //frm.Show();

    SwitchDesktop(hDeskOld);

    CloseDesktop(HDesktopglobal);
end;

end.

我用这段代码运行它:

var
  frmBlockScreen : TfrmBlockScreen;
  frmShowThread : TFormShowThread;
begin

  frmShowThread := TFormShowThread.Create(frmBlockScreen);
  frmShowThread.Priority := tpNormal;
  frmShowThread.OnTerminate := ThreadDone;
  frmShowThread.Start();

我不明白为什么这不起作用,C ++应该可行,它会在同一个应用程序中创建一个新表单。

这就是我结束这样做的方式:

我将我想要显示的表单移动到一个新项目并将其编译为timeup.exe。 我使用下面显示的过程创建了一个进程,将Desktop作为参数发送,以便我可以将进程分配给该桌面。 这样我甚至不需要创建一个新的线程......它到目前为止一直在工作。

这有什么缺陷吗?

var
  HDesktopglobal: HDESK;
  hDeskOld: HDESK;

  sDesktopName : String;
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;

  try
    hDeskOld := GetThreadDesktop(GetCurrentThreadId());

    sDesktopName := 'TimeUpDesktop';

    HDesktopglobal := CreateDesktop(PWideChar(sDesktopName), nil, nil, 0, GENERIC_ALL, nil);

    SwitchDesktop(HDesktopglobal);
    SetThreadDesktop(HDesktopglobal);

    ExecNewProcess('TimeUp.exe', sDesktopName);

    SwitchDesktop(hDeskOld);
    CloseDesktop(HDesktopglobal);

  finally
    SwitchDesktop(hDeskOld);
    CloseDesktop(HDesktopglobal);
  end;

  Application.Run;
end.



procedure ExecNewProcess(ProgramName : String; Desktop : String);
var
  StartInfo  : TStartupInfo;
  ProcInfo   : TProcessInformation;
  CreateOK   : Boolean;
begin

  { fill with known state }
  FillChar(StartInfo,SizeOf(TStartupInfo),#0);
  FillChar(ProcInfo,SizeOf(TProcessInformation),#0);
  StartInfo.cb := SizeOf(TStartupInfo);
  StartInfo.lpDesktop := PChar(Desktop);

  CreateOK := CreateProcess(PChar(ProgramName),nil, nil, nil,False,
              CREATE_NEW_PROCESS_GROUP+NORMAL_PRIORITY_CLASS,
              nil, nil, StartInfo, ProcInfo);

  { check to see if successful }
  if CreateOK then
    //may or may not be needed. Usually wait for child processes
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

1 个答案:

答案 0 :(得分:2)

在继续之前,您需要认识到VCL设计强制所有VCL表单与主GUI线程相关联。您无法在其他线程上创建它们。所以你的设计在这方面存在根本缺陷。您永远无法在主GUI线程以外的任何线程中创建VCL表单。

即使不是这种情况,您的代码也无法做任何有用的事情。那是因为你的线程不包含消息循环。表单创建后,与其关联的线程终止。

你可以使用原始的Win32调用CreateWindow等工作。但是你至少需要在你的线程中运行一个消息循环,以便在那里创建任何窗口的生命周期。

至于为什么你的代码永远不会切换回原来的桌面,我无法确定。也许代码中有一个异常尝试创建表单,因此恢复原始桌面的代码永远不会运行。该代码应该通过try / finally保护。

一般来说,为了调试调用原始Win32 API的代码,必须包含错误检查。您没有执行任何操作,因此您不知道哪个API调用失败。如果我们还不知道这种方法注定要失败,那么这将是调试这样一个问题的第一步。


也许我错过了一些东西,但对我来说,为什么你试图从另一个线程运行这个表格并不明显。有没有理由说它不能用完主GUI线程?

为了回答我自己的问题,我错过了一些东西。来自SetThreadDesktop的文档:

  

如果调用线程在其当前桌面上有任何窗口或挂钩,则SetThreadDesktop函数将失败(除非hDesktop参数是当前桌面的句柄)。