Skip to main content
gamedev.net
🔒 Locked

creating a windows class

Started by popcorn 10 replies 500+ views
Original Post
popcorn
popcorn
Is it possible to create a class that you can use to set up all the standard code that you have to do whenever you program in windows. I mean is it possible to to have something where you create an instance of your windows object like MYWINCLASS myWin and then just do myWin.StandardWindow(); I was thinking it could be something like this
  
#inndef mywindows
#define mywindows
#include <windows.h>

class MyWinClass
{
private:
  WNDCLASS wndclass;
public:
  void StandardWindow();
}

#endif
  
then in your cpp file you would have
  
#include "mywinclass.h"

void mywinclass::StandardWindow()
{
  //Here you would just have all the standard stuff that you

  //need to create a basic window like filling in the WNDCLASS

  // structure and registering etc.

}
  
Well is this possible and if it is am i doing something drastically wrong? How about them apples?
How about them apples?
Fruny
Fruny
Read this, as well as the function pointer link in my signature... actually they''re all good links...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
popcorn
popcorn
thanks for the links fruny. Having had a quick look at the article it looks quite complicated althouth i'm sure i'll work out what to do. I guess the function pointers link is needed so that i would understand more about the wndproc function.

would the member variables of the class contain WNDCLASS, HWND, MSG and also hInstance, iCmdShow?

How about them apples?

[edited by - popcorn on December 18, 2002 7:47:13 PM]
How about them apples?
popcorn
popcorn
yeah well hopefully i can figure it out AP, i was''nt planning to put in all the functionality that the writer of the article has. I just wanted to have a class that will allow you to just create instance of the windows object and then call a method so that wndclass structure would be filled out and registered etc. One problem that i have encountered though was with the wndproc function and how this could be incorporated into the class.

I know that the wndproc function has something to do with function pointers so i guess that''s why fruny advised visiting that link on function pointers, so i guess i will be looking into that first.

How about them apples?
How about them apples?
Oluseyi
Oluseyi
quote:
Original post by popcorn
yeah well hopefully i can figure it out AP, i was''nt planning to put in all the functionality that the writer of the article has.

Feel free to "pilfer" as much or as little as you want, or just to look at a complete implementation:
window.h
window.cpp
rendercontext.h (an OpenGL render context wrapper that interacts with the Window wrapper class; useful as an example of integrated use).

quote:
One problem that i have encountered though was with the wndproc function and how this could be incorporated into the class.

The wndproc always has to be a static class member function. Most of us get around that (allowing multiple instances) by having it take a pointer to the window in question as a parameter. Some people don''t do that, though, allowing only one window to exist instead. Find the details of my method (static wndproc serves as message router + pluggable message handlers) in window.cpp.
popcorn
popcorn
Thanks for the reply Oluseyi, i didn''t think i would get a reply from the writer of the article but thanks for taking the time to answer my post.

Having only had a quick look at the code that you provided links for I have noticed that you do not have the WNDCLASSEX structure as one of your member variables, why is this? I would have thought that this would be required.

Well i think i had better on with my reading.

How about them apples?
How about them apples?
Gladiator
Gladiator
WNDCLASS is only needed for registering the window class. This registered class is like a template for creating windows with the same settings that you applied to the window class structure (WNDCLASS). In order to create window with the same settings, after you register the WNDCLASS structure all you have to do is call CreateWindow() with the name of the window class as an argument.

Let me know if you are concerned with something else.
Oluseyi
Oluseyi
quote:
Original post by popcorn
Thanks for the reply Oluseyi, i didn''t think i would get a reply from the writer of the article but thanks for taking the time to answer my post.

My pleasure.

quote:
Having only had a quick look at the code that you provided links for I have noticed that you do not have the WNDCLASSEX structure as one of your member variables, why is this? I would have thought that this would be required.

No, Gladiator correctly points out that you only need to fill out a WNDCLASS[EX] structure when registering a windowclass for the first time. Unless you unregister the class (or destroy all instances), the definition remains in memory so you can ask for another window of the same class using the class name identifier.

For flexibility''s sake in my approach, though, I took a pointer to a WNDCLASSEX struct as a parameter in the constructor, allowing the class to abstract any conceivable user-defined windowclass. With a little bit of work the class could allow for the message router to be replaced, but I can''t see any real benefit in that as it would mean the user would then be confined to only a single wndproc pair.
popcorn
popcorn
Thanks for the replies people, i''m sure you are right regarding the wndclassex structure and i appreciate your explanations but i always thought that the parameters in a constructor matched those of the attributes(member variables) of the class. Maybe it does''nt always have to be like this but it has always been the case in most of the books and tutorials that i have read. Looking at the code that Oluseyi has posted it appears that this does''nt have to be the case all the time. It just goes to show that there is often a big difference between examples that you read and learn from in books and coding in the real world.

How about them apples?
How about them apples?
Gladiator
Gladiator
Books teach well. Normally you would initialize all data members in the constructor to prevent the object from undergoing catastrophic states.
popcorn
popcorn
I realize this Gladiator but what i want to know is if you look at the header file that Oluseyi has provided links for the attributes for the class are:

  
// Attributes

HWND m_hwnd;
HINSTANCE m_hinstance;
bool m_use_getmessage;
static bool m_classinit;
tyMessageMap m_messagemap;


and the constructor for the class is:

  
// _ctor

Window( std::string window_caption = "",
unsigned long window_style = WS_OVERLAPPEDWINDOW,
HWND window_parent = 0,
HMENU window_menu = 0,
unsigned long window_width = CW_USEDEFAULT,
unsigned long window_height = CW_USEDEFAULT,
WNDCLASSEX * wcx = 0 );


now as you can see the wndclass structure is a parameter in the constructor but isn''t in the attributes of the class and neither are the string, HMENU, and the unsigned long parameters which also aren''t attributes of the class.

Now i am sure that this is perfectly acceptable but my point is that i have never encountered this before.

How about them apples?
How about them apples?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.