Solved

Window handle

Posted on 1998-03-27
9
288 Views
Last Modified: 2008-02-01
How do you retrieve the handle of the main window in an SDI application?  I need the HWND value for various procedures, but not sure if I have it correctly.  Example prefered.

Thanks
0
Comment
Question by:jerm
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1183897
  Assuming you are using MFC, try AfxGetMainWnd().  That will return a CWnd*, from which you can obtain the HWND.

-=- James.
0
 

Accepted Solution

by:
HoJu earned 10 total points
ID: 1183898
It's all going to depend on where you're trying to get the handle from ...

If you're getting it from within your main callback -- probably called WndProc -- then it should be your hwnd parameter.

If you're getting it from within other callbacks that have the main window as a parent window or an owner window, you can use:

hwndMain = (HWND) GetWindowLong(hwnd, GWL_HWNDPARENT);
or
hwndMain = (HWND) GetWindow(hwnd, GWL_OWNER);

... where hwnd is the handle of the window whose callback you're inside of.

Any other functions can be passed hwnd as a parameter, or you can simply have hwnd be a static HWND when you declare it in the first place, so you can access it from anywhere.

Hope that answers your question.
0
 
LVL 1

Expert Comment

by:vsinha
ID: 1183899
I think a simpler solution would be:

hwndMain = (HWND) GetWindow(hwnd, GWL_OWNER);

I am however not an expert (currently) in this topic, so try it and correct me if I am wrong.
0
 
LVL 1

Expert Comment

by:vsinha
ID: 1183900
sorry about the previous comment, it should have been:

I think a simpler solution would be:

hwndMain = GetParent(hwnd);

I am however not an expert (currently) in this topic, so try it and correct me if I am wrong.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:jtwine100697
ID: 1183901
  "vsinha", you are assuming that the immediate parent will be the main window...

   That is not always the case. :P

-=- James.
0
 
LVL 1

Expert Comment

by:vsinha
ID: 1183902
Jtwine, I guess this is not my question, but it would be very helpful if you could give me an example where my soln. won't work and yours will. Also could you explain why?

0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1183903
  If you are in, say, a dialog box, or in a custom control that is implemented in a window (or in the view of an MFC Doc/View MDI application), the parent of the "current" window might not be the "main window" of the application.  (Note the question reads "various procedures".  That could mean various windows.)

   You (We) need more details on the specifics of where "jerm" wants to obtain the main window from.  If using MFC, AfxGetMainWnd() works...  Anywhere.

-=- James.

0
 
LVL 1

Expert Comment

by:vsinha
ID: 1183904
jtwine, thanks. If you are not using MFC would this function work:

HWND GetMainWnd(HWND hwnd)
{
   while (GetParent(hwnd) != NULL)
      hwnd = GetParent(hwnd);
   
   return hwnd;
}

[written, based on the fact that the parent of the main window is NULL]

Also didn't HoJu`s soln. have the same drawback as mine.
0
 
LVL 4

Expert Comment

by:jtwine100697
ID: 1183905
  Yes, the function (loop) will work as expected.  (That, BTW, is similar to how MFC delegates control [updates] for Context Menus!)

   And yes, the Answer posted by "Hoju" has the same problem.  That is why I try to contain myself and post suggestions before wasting time with possibly incorrect answers! :)

-=- James.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now