Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!cs.utexas.edu!yale!cs.yale.edu!spolsky-joel From: spolsky-joel@cs.yale.edu (Joel Spolsky) Newsgroups: comp.windows.ms.programmer Subject: Re: Scoping question using C and SDK Keywords: scoping, C, SDK Message-ID: <28290@cs.yale.edu> Date: 23 Jan 91 06:26:34 GMT References: <1991Jan23.044611.21227@cs.uoregon.edu> Sender: news@cs.yale.edu Organization: Yale University Computer Science Dept., New Haven, CT 06520-2158 Lines: 34 Nntp-Posting-Host: zoo-gw.cs.yale.edu Originator: spolsky@suned.CS.Yale.Edu In article <1991Jan23.044611.21227@cs.uoregon.edu> akm@cs.uoregon.edu (Anant Kartik Mithal) asks why pens, brushes, etc. that are initialized in WM_CREATE and used in WM_PAINT must be global and cannot be local to the window function in which they are used. What you want to do is: WndProc (blah blah) { HBRUSH hbFoo; ... case WM_CREATE: hbFoo = GetMeABrush(); break; case WM_PAINT: use hbFoo; } The reason this will not work is a function of C's scoping rules. The variable hbFoo is a local, temporary variable which goes away when the function returns. Since the WM_CREATE clause and the WM_PAINT clause are executed on different passes through the window function, this variable is not preserved. The solution is either to make the brush global or to keep it local but add the "static" keyword so that it is allocated in the data segment and not on the stack. -- Joel Spolsky // And these streets, Quiet as a sleeping army spolsky@cs.yale.edu // Send their battered dreams to heaven. _Paul Simon