Page 1 of 2 12 LastLast
Results 1 to 10 of 15
  1. #1
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,422

    C# (or C++) Get window handle

    Hey everybody -

    I am in need... I need a way to get a handle for a specified window, so later I can then call that window, and make it active.

    I've looked around, and everything is a bit over my head atm, and was wondering if someone knew how to do this, and break it down a bit for me




    Oh... and w00t 1,000th post

  2. #2
    Join Date
    Mar 2004
    Posts
    0
    hmm, im not understanding you, dude

    if you are using C#, you can have a reference to the form you want to activate later, and call the Show() function if the window has not been shown, change the Visable property to true if the window was hidden, or the Focus() function if you just want to bring the window to the front and select it.
    for example, if you have a form class entitled OptionsForm in your program (IE, it was created in the form designer), if another pice of you you could put
    OptionsForm frm = new OptionsForm(); frm.Show();
    to show the form, or you could save it as a member variable so you can focus/show/make visable the form later.

    is that what you were looking for?

  3. #3
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,422
    No not quite. I'm looking to bring focus to a window which is outside of my C# app. Here is what my objective is:

    At work, I must enter a bunch of rudundant data into a really old (1986'ish) command prompt type application. It is very boring and tedious.

    I'd like to create a GUI front-end for it. Since I have no access to the source code of the app, or any other options regarding updating the actual original program - the only wat I can see to do it is with a really nasty hack.

    What I plan to do is rather simple really. First I will 'monitor' the command window by copying all the text from that prompt - then parsing through that text to determine the various fields on that part of the program.

    This is where I would need to find all the active window's handles, so I can select which window to grab the text from. I will then will use a SendKeys command to the prompt to select all the text, then ctrl-c to copy all that text to the clipboard, and finally bring that text into my C# app to parse.

    After I figure out which fields to input the data to, I will then need to go back into the command prompt and SendKeys again to fill out the various fields.


    That's pretty much what I have planned for the basic version of this; although I'd like to extend it so it actually pulls all the data I need to input from a different prompt, and adds it into the other correct prompt.

    It's all a part of my evil, maniacal plan to make myself indispensable person at work since I am only a temp.

  4. #4
    Join Date
    Oct 2002
    Location
    Leicester, UK
    Posts
    3,335
    This site has an example of using the Win32 FindWindow() function from .NET.

    Another possible approach: Maybe you could get the input and output streams of the command line program and manipulate them?
    Forget the username, call me Keith

  5. #5
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,422
    Quote Originally Posted by keithathaide
    This site has an example of using the Win32 FindWindow() function from .NET.

    Another possible approach: Maybe you could get the input and output streams of the command line program and manipulate them?
    That looks like it might work. I'm not totally sure on how to use it, but haven't had much time to look at it either.

    The app isn't a command line per se, it is a prompt - I've already tried to explain this to ostamo, and failed badly. So I'll post some screen shots of what I'm working with lol

    Thanks (yet again) for the link Keith

  6. #6
    Join Date
    Jun 2005
    Location
    NY
    Posts
    1,374
    This is for C++ since I don't know too much C#.

    Code:
    HWND fWindow; // Window handle of the window you want to get
    
    fWindow = FindWIndow(NULL, "Text_Of_Window_To_Find"); // Find the window
    
    BringWindowToTop(fWindow); // Activate the window
    Pretty straightfoward.

    MSDN Function References:
    FindWindow()
    BringWindowToTop()
    CodeGuru: DLL Tutorial For Beginners by me. Rated 4 1/2 out of 5.

  7. #7
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,422
    Thanks notsosuperhero - exactly what I needed. I've seen that around in lots of tuts floating around, but it was always coupled with a bunch of other stuff MCF stuff too, which was what was confusing me.

    These are the prompts I was trying to explain - the top two are what I'm attempting to input text to, and the bottom is an example of the prompt I'm trying to extract from.



    Unfortunately I'm not totally sure if I'll even be able to do this in C# anymore cuz they set me up on a win2000 box without the nessary service pack required by .NET. I have to talk to the admin on Monday about it all.

    I'm not sure I'm willing to take on this project in pure C++ either - there is a lot of string parsing to be done, and it might take longer to develop than I am actually working there lol.

    Hopefully it works out, but thanks a bunch for your support guys

  8. #8
    Join Date
    Oct 2002
    Location
    Leicester, UK
    Posts
    3,335
    Maybe, instead of SendKeys(), the console functions could be used instead, like ReadConsoleOutput() and WriteConsoleOutput()?

    I'm not sure if it will work, just throwing out ideas.
    Forget the username, call me Keith

  9. #9
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,422
    Quote Originally Posted by keithathaide
    Maybe, instead of SendKeys(), the console functions could be used instead, like ReadConsoleOutput() and WriteConsoleOutput()?

    I'm not sure if it will work, just throwing out ideas.
    That may work on the prompt I need to input onto (which I'll look into on Monday), but probably won't work with the prompt I need to extract from because it really isn't a console - it is a terminal emulator, so I'm not sure it will work but I'll have a look

    I just really hope they let me install .NET cuz writing this in C++ will be a pain in the ass.

  10. #10
    Join Date
    Dec 2004
    Location
    Wisconsin
    Posts
    1,422
    Ok well I've managed to set the active window I'd like and send data to it, but I am still having difficulty copying the text from the console.


    The problem is that there are basically NO keyboard shortcuts in cmd.exe

    The only way I can imagine selecting all the text would be to either do it by dragging the mouse, or using the context menu->edit->select all, then doing the same to copy.

    Now that would be fine, except I can't get to the context menu via SendKeys(). This is because the shortcut is alt+SPACE - and unfortunatley, SendKeys() doesn't seem to realize the difference between an alt modified space.

    So I've tried to send a right click to open the context menu.
    Code:
     /* from my Win32.Win32API class */
    [DllImport("User32.dll")]
    public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
    [DllImport("User32.dll")]
    public static extern Int32 SetForegroundWindow(int hWnd);
    [DllImport("user32.dll")]
    public static extern Int32 SendMessage(int hWnd, UInt32 msg, int wParam, int lParam);
    Code:
    /* from my main class */
    int cmdWnd = Win32.Win32API.FindWindow(null, "C:\\WINDOWS\\system32\\cmd.exe");
    Win32.Win32API.SetForegroundWindow(cmdWnd);
    
    //send RMouseButtonUp message (0x0205)
    Win32.Win32API.SendMessage(cmdWnd, 0x0205, 0, 0);
    Which actually works - but now I can't access that menu with SendKeys(). If I use SendKeys(), those keys don't go into the menu - but into the console window ONLY AFTER that menu closes.

    It is really confusing me, but hoping someone knows a bit more about this stuff than I do.



    /me just realizes just how dirty of a hack this is lol

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •