Thread: Win32Control design concern

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    903

    Win32Control design concern

    Hello. I currently have an idea of a program I'd like to make for myself (and hell, maybe for everyone) and I want to make a wrapper around the Win32 API to make things a tad easier for the GUI (I don't want to make it dialog-based nor do I want to use any other API). I just want to know your opinion on what I have right now. I have no idea why but it just looks plain wrong to me... It looks way too simple yet I don't know what I am missing. Of course, this is only an abstract class and there are many others to come... Yet if I screw up on the base class, chances are that all other derived classes will be screwed up as well.

    Here's the header file for my Win32Control class. I won't post the implementation file unless asked because I feel it would be useless as my question lies more on the design than on the implementation of the methods.


    Edit:

    I have worked a bit on my project and modified it so that it will work better with controls I will create from scratch. I'm also posting an example of derived class from my base class Control.

    Code:
    //===============================================
    //
    // Author: Alexandre P. L.
    // Created: December 4th 07
    // File: control.h
    // Contact: <removed for privacy>
    //
    //===============================================
    
    
    #ifndef CONTROL_H_INCLUDED
    #define CONTROL_H_INCLUDED
    
    
    #include "control_utilities.h"
    #include <windows.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    
    //===============================================
    //
    //                class Control
    //
    //                 DECLARATION
    //
    //===============================================
    
    
    class Control
    {
    public:
        Control();
        ~Control();
    
        virtual void Update() = 0;
    
        //
        // Static methods
        //
    
        static void UpdateControls();
        static void RemoveControl(Control* ctrl);
    
        //
        // Action methods
        //
    
        virtual void EnableControl(bool enable) = 0;
        virtual void ShowControl(bool show) = 0;
    
        //
        // "Set" methods
        //
    
        virtual void SetCaption(const std::string& txt) = 0;
        virtual void SetSize(u_int cx, u_int cy) = 0;
        virtual void SetPosition(u_int cx, u_int cy) = 0;
        virtual void SetPosition(const Position& obj_pos) = 0;
    
        //
        // "Is" methods
        //
    
        virtual bool IsControlEnabled() const = 0;
        virtual bool IsControlVisible() const = 0;
    
    protected:
        Position position;
        std::string caption;
    
    private:
        static std::vector<Control*> ctrl_list;
    };
    
    
    #endif // CONTROL_H_INCLUDED
    Code:
    //===============================================
    //
    // Author: Alexandre Prince-Levasseur
    // Created: December 4th 07
    // File: win32control.h
    // Contact: [email protected]
    //
    //===============================================
    
    
    #ifndef WIN32CONTROL_H_INCLUDED
    #define WIN32CONTROL_H_INCLUDED
    
    
    #include "control_utilities.h"
    #include <windows.h>
    #include <string>
    #include <vector>
    
    
    //===============================================
    //
    //              class Win32Control
    //
    //                 DECLARATION
    //
    //===============================================
    
    
    class Win32Control
    {
    public:
        virtual void Update() = 0;
    
        //
        // Action methods
        //
    
        void EnableControl(bool enable);
        void ShowControl(bool show);
    
        //
        // "Set" methods
        //
    
        void SetCaption(const std::string& txt);
        void SetSize(u_int cx, u_int cy);
        void SetPosition(u_int cx, u_int cy);
        void SetPosition(const Position& obj_pos);
    
        //
        // "Is" methods
        //
    
        bool IsControlEnabled() const;
        bool IsControlVisible() const;
    
    protected:
        std::string wnd_class;
    
    private:
        HWND ctrl_wnd;
    };
    
    
    #endif // WIN32CONTROL_H_INCLUDED
    I know it's quite a bit of code to post on a forum but I'd appreciate so much comments on the overall design and suggestions of improvements.
    Last edited by Desolation; 12-05-2007 at 11:46 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Moved to Windows board.
    Last edited by VirtualAce; 12-05-2007 at 10:33 PM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    903
    Thanks for moving it. After I posted I thought it would maybe more appropriate in the Windows section but it was too late.

    As for the code, I think it would be wise to add methods for sub-classing the Win32 controls since I will definitely do it and I might even make a few ones from scratch (think split bar).

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    One thing that you appear to be missing is callbacks. How do you know if a user clicks on your control, or presses a key while your control has the focus? How do you know when it gets resized or moved? How about when your control gets and loses focus?

    Win32 programming is completely event driven, which means classes need a means to perform those notifications. This is why MFC is such a mess.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    True. Infact after reading the first three sentences of the first post, my thoughts immediately turned to MFC. If you're goal is to learn C++, it may be a fun experiment; however if you're goal is to make a somewhat decent and functional OO wrapper for vanilla win32 API, you're better off using what is already out there:
    MFC (bloated, not recommended)
    wxWidgets (mm-mmm... cross-platform goodness!)
    A List of Others (GTK+, Qt, etc..)

    If you're a microsoft fan boy or just want to look cool sporting the new CLI/CLR fad going around these days, you can also check out the .NET family (I'd recommend C++ or C#), and so un-like MS, its an open-standard and has potential to be cross-platform too (check out Mono).

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    The fact is I really don't like the syntax that both MFC and wxWidgets impose to their users. I've never really looked at the other options seriously but that's not something I want to do either. I just want to make a simple OO wrapper of the Win32 API even though I know it's not portable or whatever. It doesn't matter, it's for my personnal use and maybe friends. I don't want to use .NET either. There's no particular reason why, but I just don't like it. :P

    As for the first comment, I will try to implement a message system in my application and make it compatible with both existing Win32 controls and the ones I will have to make from scratch.

    Thanks to both of you =)

    Keep the comments coming !

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you are looking for a thin C++ wrapper to win32, then I suggest you take a look at WTL. It may be exactly what you are looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. any comments about a cache design?
    By George2 in forum C Programming
    Replies: 6
    Last Post: 09-14-2006, 12:53 PM
  2. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  3. Opinions on new site design
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 01-21-2005, 01:34 PM
  4. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM