Set foreground window

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QUEyZTcyRQ==?=

    #1

    Set foreground window

    Given the handle of a window, is there a way to make it the foreground window
    using C# (rather than win32 API)?
  • Peter Duniho

    #2
    Re: Set foreground window

    On Fri, 06 Jun 2008 00:58:03 -0700, AA2e72E
    <AA2e72E@discus sions.microsoft .comwrote:
    Given the handle of a window, is there a way to make it the foreground
    window
    using C# (rather than win32 API)?
    Given just the handle? No, I don't think so. A handle is an unmanaged
    data structure, and you need to use unmanaged code.

    If you know something else about the handle (for example, you know for
    sure it's from a managed Form instance), then there's the possibility you
    can convert it into something that would allow you to use the result from
    managed code. But otherwise, no.

    Pete

    Comment

    • =?Utf-8?B?QUEyZTcyRQ==?=

      #3
      Re: Set foreground window


      I can do this quite easily using Win32 APIs from unmanaged code; I am trying
      to avoid unmanaged code.

      As well as the handle, I can get the caption.

      Basically I am using an out of process COM server to which I need to send
      instructions via SendKeys; for this, I need to make the COM server visible (I
      can) and be the foreground window.


      Comment

      • Peter Duniho

        #4
        Re: Set foreground window

        On Fri, 06 Jun 2008 09:34:00 -0700, AA2e72E
        <AA2e72E@discus sions.microsoft .comwrote:
        I can do this quite easily using Win32 APIs from unmanaged code; I am
        trying
        to avoid unmanaged code.
        Based on your description, I don't think that will be possible. But,
        using p/invoke for the one function you need (SetForegroundW indow)
        shouldn't really be an issue, especially given that you're already
        basically messing around with unmanaged stuff anyway.

        Note, of course, the limitations on SetForegroundWi ndow. Assuming your
        own application is already in the foreground, you should be fine. But
        it's not a panacea.

        Pete

        Comment

        Working...