Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:34:41+00:00 2026-05-30T16:34:41+00:00

I need to get back to foreground window, that was active before my app

  • 0

I need to get back to foreground window, that was active before my app window, I tryed to use user32.dll for this, but i can’t find previous window hendle.

   [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);

    [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern IntPtr GetWindow(IntPtr hwnd, uint wFlag);

    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetFocus();

    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    public static extern IntPtr SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    static extern bool PostMessage(IntPtr hWnd, int Msg, char wParam, int lParam);

    [DllImport("user32")]
    public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);

    [DllImport("user32")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern uint GetCurrentThreadId();
     ...
        hMe = GetForegroundWindow();
        hNext = GetNextWindow(hMe, hw_next);
        System.Text.StringBuilder window = new StringBuilder(32);
        GetWindowText(hNext, window, 32);

and i get only “default IME” or “M” in next window, how can i found real app window?

i’ve loop thrue windows, to find my notepad window:

0: D:\univer\C#
1: 
2: 
3: 
4: 
5: 
6: Главное меню
7: 
8: 
9: M
10: Default IME
11: 
12: 
13: 
14: CiceroUIWndFrame
15: 
16: 
17: 
18: SysFader
19: SysFader
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: SysFader
36: 
37: 
38: HDMI Settings
39: S/PDIF IN/OUT Settings
40: Set Device Type
41: Mixer ToolBox
42: Параметры разъёма
43: CiceroUIWndFrame
44: TF_FloatingLangBar_WndTitle
45: Syn Zoom Window
46: Syn Visual Window
47: 
48: 
49: Начать отладку (F5)
50: M
51: Default IME
52: 
53: 
54: 
55: 
56: 
57: 
58: *new  2 - Notepad++

I’ve got the solution with Hook:

    delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

    [DllImport("user32.dll")]
    static extern bool UnhookWinEvent(IntPtr hWinEventHook);

    [DllImport("user32.dll")]
    static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);


 private static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
        uint id = 0;
        if (eventType == EVENT_SYSTEM_FOREGROUND)
        {
            if (hwnd != _this.hKeyboard && hwnd != _this.hLast && hwnd != IntPtr.Zero)
            {
                _this.hLast = hwnd;

            }
        }
    }
 ...
 Form1_load(){
    _WinEvent = new WinEventDelegate(WinEventProc);
    mHook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _WinEvent, 0, 0, WINEVENT_OUTOFCONTEXT);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:34:43+00:00Added an answer on May 30, 2026 at 4:34 pm

    The Z-Order of windows has no direct relationship to when they were activated or switched to/from.
    The correct way to get the hWnd of the previously active window is to handle the WM_ACTIVATE message. The previous hWnd will be passed in the lParam value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need to get the 10 word before and 10 words after for the given
I need to get the default printer name. I'll be using C# but I
I need to get some information that is contained in the MFT on a
I need to get the name of the machine my .NET app is running
This looks like a very simple issue, but I just can't get it to
I need to get back the postid (auto-incrementing PK) of a row when i
I need to get every possible combination of characters that follows a rule. Say:
We need to get back a collection of Widgets, where each widget can contain
I am upsizing the back end of a client's Access app tomorrow and need
Just need get some vals located in application.ini(main ini) in the Controller plugin I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.