CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2002
    Posts
    33

    Windows Programming Question

    Hi all, I am in a bit of a Dilemma. My app currently takes the format:

    Declare Globals.
    function InitApp().
    function UpdateFrame().
    function CallBack WindowProc().
    function WinMain().

    I would like to incorporate a CD3DApp class which WinMain() would create, initialise and set the Run() function. The CD3DApp class would then handle its own messages in a similar style to the code:

    while(GetMessage(&msg, NULL, 0, 0))
    {
    TranslateMessage(&msg);
    ProcessMessage(&msg);
    }

    which obviously uses the WindowProc CallBack function (so I could specify which buttons do what etc.). I DO NOT want to use MFC. I would like to make my InitApp(), UpdateFrame() and Run() a part of the CD3DApp class and also the global variables as member variables (I think the code is much cleaner then). However, if I put the messaging code listed above in the Run() the CallBack WindowProc cannot access the member variables of the CD3DApp class because the object hasn't been passed through to it (whereas before they were global which allowed for access). There is a timer in the WindowProc which is used to create an animation, which is why I would like access to the CD3DApp class.

    What I would like to know is:

    1.) Is there any way of creating a class that has its own WindowProc function, so that it can access the member variables?

    2.) If not, is there any better way to go about solving this problem of getting the code cleaner (losing the globals)?

    Alan.
    Last edited by Chambers; June 27th, 2002 at 05:51 AM.

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    This is the same as what I did in X-Motif when writing classes for the widgets/components. Use
    SetWindowLong/GWL_USERDATA to save the object address. User data should be used for anything else otherwise this will not work.

    There is a chicken and egg problem since both CreateWindow and
    DialogBox both use the callback. Save a pointer to the class being used to get around this problem. It is set before either CreateWindow or DialogBox is called and picked up if there is no userdata.

    See attached file as an example.
    Attached Files Attached Files
    Succinct is verbose for terse

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured