CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    some questions about Window Class (WNDCLASS)

    I read chapter 19 for Chalres Petzold Book (Windows Programming)..
    This chapter explain how to create Multi Document interface (MDI)..

    But after reading this chapter I was confused for Window Class ...
    I will expalin in simple words ..
    for MDI we have to create two windows
    first Window is : Frame Window (= Main window of program)
    second Window: Child Window

    Petzold in his book Created only one window Class Say = WndClass
    He Used This class one time to Prepare Frame Window ; Second time He Used the same class to prepare Child Window

    the code for frame Window is :

    wndclass.lpfnWndProc = FrameWndProc ;
    ....................
    ...............


    the code for Child Window is :
    wndclass.lpfnWndProc = HelloWndProc ;
    ........................
    .............................


    My Questions are :

    Q1:
    should the application have only one Window Class ?? and this will be used for all windows of the program,

    Or I can Use many Window Classes in the same Program ?? and Why...

    Q2:
    and here , Petzold used the same Window class , How I can store the data two times .. I stored the data for wndclass when I create Frame Window
    and Again I store the new data for the same Class when I create The Child Window!!!?? or something is not clear with Me??

    Thanks & Regards

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: some questions about Window Class (WNDCLASS)

    Application can have many windows of the same class.
    You have to distinguish "different windows" and "different window classes".
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: some questions about Window Class (WNDCLASS)

    Thanks VictorN,

    Now , I agree , I can use many windows for one one window class..
    Just question , Can I use many window Classes for one Application and Why one class , or many classes??

    Thanks & Regards

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: some questions about Window Class (WNDCLASS)

    Quote Originally Posted by mecheil.edwar View Post
    Can I use many window Classes for one Application and Why one class , or many classes??
    Yes, you can use many window Classes for one Application. And usually you do it: Windows has already registered a lot of window classes for you, such as button, static, edit, listbox, SysListView32, and so on.
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: some questions about Window Class (WNDCLASS)

    Thanks Victor for your effective answers...
    Now I have some notes after understanding this issue , I would like to add here to be useful for others..

    Note 1: the first window should be created outside the wndProc Function Why...
    Beacause if you want WndProc to do any action for the application .. first thing you have to inform wndProc is HWND.. the handle of the window..
    if we look to windproc format it is:
    WndProc(hwnd,...,....,...)
    So , in this case if we want to create the first window from wndproc sure , the program will not run..

    So, We have to create the first window outside wndProc

    Example 1:
    WinMain(...........)
    {
    ...........
    ...........
    CreateWindow (....) /// this should be the first window of application
    }

    Some users create the first window outside the winmain function also it is correct , but it should be outside windproc///


    Note 2: After creating first window , we can create any window inside wndProc..and we can use same window class ..
    the program in this case will run as following
    1. After creating the window_1 .. Now this window has a handle say HWND = 0x002 as example
    2. from window_1 when we send a message to create new window the wndProc will recive this message as following :

    WndProc( hwnd = 0x002///Note the value of handle = the handle of window that we sent the message from it ,...,..)

    ...................
    /// Say we will handle the message as following
    Case New_Window:
    HWND hwnd2 // now we will create object hwnd2 which will recieve the return value from function CreatWindow
    hwnd2= CreateWindow(.....)
    ShowWindow(hwnd2)
    UpdateWindow(hwnd2)
    Break;
    ..................
    ................
    }

    I hope this clarifications are useful

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: some questions about Window Class (WNDCLASS)

    Quote Originally Posted by mecheil.edwar View Post
    Why one class , or many classes??
    A window class is directly associated with its window procedure that you provide during registration. So creating window of the class implies creating a window with this specific behavior (which window procedure implements).

    Now, if you need your app to implement some complex behavior, you distribute the whole functionality among particular windows with predefined behavior corresponding to their window classes, for example, edit controls process user keyboard input, buttons send specific commands, dialog implements command and control data processing logic, and at the same time all the windows handle their visual attributes, self drawing, mouse cursor shapes, etc. The more versatile/complex behavior you're after, the more window classes you need.

    One more axis of flexibility: the class generic behavior can be tweaked a bit by subclassing window procedure. This way you obtain some specialized instance of the same class, for example, implementing a specific reaction to a specific message(s) only.
    Best regards,
    Igor

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: some questions about Window Class (WNDCLASS)

    Note 1: the first window should be created outside the wndProc Function Why...
    Beacause if you want WndProc to do any action for the application .. first thing you have to inform wndProc is HWND.. the handle of the window..
    Wrong. WndProc is called by Windows only. Therefore, to make WndProc to start being called by Windows you have to create the window first. And there is no other way to do that for the very first window in a thread but creating outside any window procedure.

    Even more than that. To let messages reach your window procedure, the message pump must be spinned first providing message dispatching for the thread the window was created in.
    Best regards,
    Igor

  8. #8
    Join Date
    Jun 2012
    Location
    UAE
    Posts
    62

    Re: some questions about Window Class (WNDCLASS)

    Thanks Igor for your effective comments...

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