WINAPI windows management question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: WINAPI windows management question

  1. #1
    Join Date
    Sep 2012
    Posts
    3

    Talking WINAPI windows management question

    Hello everyone.
    First of all I want to introduce myself as I am new to this forum.
    I am a Software developer who loves programming and who likes to learn a lot of languages. So I am in search of gathering basic knowledge for the most significant languages to have some basic clue in case I need them someday.

    Now my question:

    I'm learning to develop windows applications using WINAPI and plain C.
    Now I got a bit confused with all those handles and would like to ask if you guys could teach me some good practices to structure and handle controls and windows.

    Here's where I get confused:

    Using the IDs declared in the resources for each object, we can get their handles using GetDlgItem(). Now what if we don't know their parent, which is needed by this function.

    One example: We have the main window created at launch. Then we register two new window classes and create a window for each new class and we create a message function for each too. Now if inside one of the children windows I create a button and inside the other child window I create a text label. Now when we click the button inside of child window A the label in child window B shall be modified to whatever.

    The WM_COMMAND for the button is interpreted inside the message loop for child window A. Now what would be the best and more elegant way to access the text label inside the child window B?

    I am in the process of learning the WINAPI and just want to learn it right from the start instead of producing Hacked code that someday becomes unreadable and to later have to adapt to a new way of programing.


    Thank you.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,976

    Re: WINAPI windows management question

    Now if inside one of the children windows I create a button and inside the other child window I create a text label. Now when we click the button inside of child window A the label in child window B shall be modified to whatever.
    Child window should send a custom message to its parent about wanting to change the label. Only parent window knows whether the other child has been created to the moment. If yes, the parent sends another custom message to the other child instructing it to change the label for corresponding control.

    Note, parent is a message broker in this scheme able to route/transform the messages, and child windows just need to follow some special protocol for sending commands or responding to those. Looks a bit tedious, though flexible and reliable.

    BTW, this approach generally has nothing specific to do with Win32 API.

    EDIT: Of course there may be other ways to implement this behavior. Feel free to develop your own one.
    Last edited by Igor Vartanov; September 18th, 2012 at 04:30 AM.
    Best regards,
    Igor

  3. #3
    Join Date
    Sep 2012
    Posts
    3

    Re: WINAPI windows management question

    Hello,

    Thanks a lot for your help. I really appreciate it.

    Now I may be complicating myself but I'm still a bit confused. Also I may not being explaining myself correctly.
    I made a picture where I state my problem:

    Name:  explanation.jpg
Views: 57
Size:  28.9 KB

    Now where I'm confused:
    When hButton is clicked, hLabel should be changed.
    I get a bit confused on which messages go where and how to know the parents etc.

    So normally hwndControls being the parent of hButton, should be the one receiving the message from hButton right?
    Now hwndControls doesn't know that hwndContent exists! so it can't send it a message.
    What I also don't know is if it is good practice to pass all the handles by parameter or declare them as global so they are visible. I don't know if you see where I get confused?
    And I don't think that parent of parent of parent of child is a good practice is it?

    Maybe you could post some more specific example or maybe some simple code for this problem so I can actually see what I'm missing to understand, If you wouldn't mind.

    This example is just an example I've created so I can understand how to handle that kind of problem. Understanding this would help me understand how to do it for all other contexts.

    Hope I am not asking for too much. I really just want to learn it the right way on the first run so I use good practices from the beginning on.


    Thank you so much!!!

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: WINAPI windows management question

    Quote Originally Posted by gekod View Post
    I made a picture where I state my problem:
    ...
    Now where I'm confused:
    When hButton is clicked, hLabel should be changed.
    I get a bit confused on which messages go where and how to know the parents etc.
    As Igor stated, this has nothing to do with win32. You should forget about the windows and start talking in terms of classes and objects. Basically, your problem is:
    You have an object a of class A and an object b of class B. Now in some function of a, you want to call a function of b. Therefore, a needs to have a reference to b. That's it; no windows, no messages.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,976

    Re: WINAPI windows management question

    Sample attached.
    Attached Files Attached Files
    Best regards,
    Igor

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

    Re: WINAPI windows management question

    Name:  main.jpg
Views: 46
Size:  69.6 KB
    Best regards,
    Igor

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,976

    Re: WINAPI windows management question

    Quote Originally Posted by D_Drmmr View Post
    As Igor stated, this has nothing to do with win32. You should forget about the windows and start talking in terms of classes and objects. Basically, your problem is:
    You have an object a of class A and an object b of class B. Now in some function of a, you want to call a function of b. Therefore, a needs to have a reference to b. That's it; no windows, no messages.
    That's true for object oriented frameworks. But in plain Win32 API you need to invent some routine mechanism based on some kind of messaging.
    Best regards,
    Igor

  8. #8
    Join Date
    Sep 2012
    Posts
    3

    Re: WINAPI windows management question

    Hello,

    Sir, I really want to thank you for taking time to help me out! This is in deed very kind of you and I really appreciate it! If all forums had people like you, people would be able to learn so much more!

    I will take a look at the code you posted.

    Thanks again =)

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,976

    Re: WINAPI windows management question

    A little bit developed concept of custom message protocol, sample attached. Now it makes use of the single message WM_MYCOMMAND encapsulating a number of commands based on corresponding command parameter structures. In fact, this way the message based Win API part works.
    Attached Files Attached Files
    Last edited by Igor Vartanov; September 20th, 2012 at 08:16 AM.
    Best regards,
    Igor

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center