home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / os / mswindo / programm / misc / 1110 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  2.4 KB

  1. Path: sparky!uunet!usna!faculty!baldwin
  2. From: baldwin@csservera.scs.usna.navy.mil (J.D. Baldwin)
  3. Newsgroups: comp.os.ms-windows.programmer.misc
  4. Subject: How many children does this window have?
  5. Message-ID: <BALDWIN.92Jul30213705@csservera.scs.usna.navy.mil>
  6. Date: 31 Jul 92 01:37:05 GMT
  7. Sender: news@usna.NAVY.MIL
  8. Organization: Comp. Sci. Dep't., U.S. Naval Academy, Annapolis, MD
  9. Lines: 51
  10.  
  11.  
  12. This is something that ought to be REAL simple, but I just can't figure it
  13. out.  I am using ObjectWindows with Borland C++ 3.0 to develop a Windows
  14. 3.1 application.  To summarize:    I want a simple way to tell how
  15. many children a window has, or, better, simply whether or not that
  16. number is zero.
  17.  
  18. In function CanClose() of my application's main window, I wish to check
  19. whether the window has any children before asking whether the user is sure
  20. he wants to close the window.  That is, the window will close immediately
  21. if there are NO children; but a MessageBox will ask "Are you sure?" if there
  22. are any children.
  23.  
  24. I thought I'd just do the following (in CanClose()):
  25.  
  26.    PTWindowsObject kid;
  27.  
  28.    kid = GetFirstChild();
  29.  
  30.    if (kid != NULL)
  31.    {
  32.       /*  DO MESSAGEBOX HERE */
  33.    }
  34.    else
  35.       return TRUE;
  36.  
  37. This works fine if there are children, but causes a general protection
  38. fault if there are no children.  Why?  Well it seems that GetFirstChild()
  39. is an inline function that returns, simply:
  40.  
  41.      ChildList -> SiblingList
  42.  
  43. If there are no children, ChildList is NULL.  (Is this brain-dead,
  44. or what?  A call to GetFirstChild() when there are no children will
  45. always cause a general protection fault since it dereferences NULL.)
  46.  
  47. "So," I hear you saying to yourselves, "if ChildList is NULL when
  48. there are no children, then why not just test (ChildList != NULL)?"  An
  49. excellent idea, but thanks to the data-hiding instincts of the same
  50. software designers who gave us the bug described above, ChildList is a
  51. private data member of TWindowsObject.
  52.  
  53. The only thing I can think of is to use ForEach() on a pointer to a function
  54. whose only purpose is to increment a counter; this will return a count
  55. of the child windows.  However, this is a gross kludge and, for all I know,
  56. ForEach() suffers from the same idiotic bug as GetFirstChild().
  57.  
  58. So, does anyone out there have the simple answer?  If you do, please post
  59. *and* e-mail me at baldwin@spectrum.lanl.gov, as my access to my normal
  60. mail machine and news-reading machine is unreliable at best (it requires
  61. a 3000-mile telnet connection).
  62.