Archived

This topic is now archived and is closed to further replies.

ELS1

ok. i ran into a design barrier. please come on in :)

Recommended Posts

i am writing some simple win32api wrappers. i have a member function called Create in the class CControl. From CControl I derive CCommandButton, CEdit, CList, CCombo, CStatic, etc. The problem that i am running into is in the Create function. Each child control takes different parameters for the window style. For example. Edit uses the style WB_BORDER, ES_LEFT. but other controls do not. Would it be more feasable to have the Create a function in each derived class instead of in the base class?? Is there a way to just use the Create in the base class, but have different params with each control that i create? that way it wont get so "messy". this is my firt major project...and i am learning A LOT from it anyways, here is the source of what i have so far. ccontrols.h
      
#include <windows.h>

// BUTTON STYLES

#define COMMAND_BUTTON	1 
#define EDIT_CONTROL	2 
#define STATIC_WINDOW	3
#define RADIO_BUTTON	4 
#define CHECK_BOX		5
#define LIST_BOX		6 
#define COMBO_BOX		7 

// CCONTROL CLASS

class CControl {
protected:
	HWND hwnd ;
	HINSTANCE hinst ;
	int width ;
	int height ;
	int x_pos ;
	int y_pos ;
	int font_size ;
	int style ;
public:
	void Create(HWND parent, int instance, char* caption, int x_pos, int y_pos, int width, int height) ;
	void SetFont(char* font, int size) ;
} ;

class CButtonControl : public CControl {
public:
char* GetText() ;
void SetText(char* text) ;
	
} ;


// CBUTTONCONTROL DERIVED CLASSES

class CCommandButton : public CButtonControl {
public:
	CCommandButton() ; 
} ;

class CTextBox : public CButtonControl {
public:
	CTextBox() ;
} ;

class CStaticWindow : public CButtonControl {
public:
	CStaticWindow() ;
} ;

class CRadioButton : public CButtonControl {
public:
	CRadioButton() ;
} ;

class CCheckBox : public CButtonControl {
public:
	CCheckBox() ;
} ;

// CCONTROL DERIVED CLASSES

class CListBox : public CControl {
public:
	CListBox() ;
} ;

class CComboBox : public CControl {
public:
	CComboBox() ;
} ;
  
  
ccontrols.cpp
       
#include "ccontrols.h"

// CONSTRUCTORS

CCommandButton::CCommandButton()
{
	style = COMMAND_BUTTON ;
}

CTextBox::CTextBox()
{
	style = EDIT_CONTROL ;
}

CStaticWindow::CStaticWindow()
{
	style = STATIC_WINDOW ;
}

CRadioButton::CRadioButton()
{
	style = RADIO_BUTTON ;
}

CCheckBox::CCheckBox()
{
	style = CHECK_BOX ;
}

CListBox::CListBox()
{
	style = LIST_BOX ;
}

CComboBox::CComboBox()
{
	style = COMBO_BOX ;
}


// CControl Member Functions

void CControl::Create(HWND parent, int instance, char* caption, int x_pos, int y_pos, int width, int height)
{
	switch(style)
	{
		case COMMAND_BUTTON:
			hwnd = CreateWindow("button", caption, WS_CHILD | WS_VISIBLE, x_pos, y_pos, width, height, parent, (HMENU) instance, hinst, NULL) ;
			break ;

		case EDIT_CONTROL:
			hwnd = CreateWindow("edit", caption, WS_CHILD | WS_VISIBLE | WS_BORDER, x_pos, y_pos, width, height, parent, (HMENU) instance, hinst, NULL) ;
			break ;

		case LIST_BOX:
			MessageBox(NULL, "LIST_BOX", "CControl::Create", MB_OK) ;
			break ;

		case COMBO_BOX:
			MessageBox(NULL, "COMBO_BOX", "CControl::Create", MB_OK) ;
			break ;

		case RADIO_BUTTON:
			MessageBox(NULL, "RADIO_BUTTON", "CControl::Create", MB_OK) ;
			break ;

		case CHECK_BOX:
			MessageBox(NULL, "CHECK_BUTTON", "CControl::Create", MB_OK) ;
			break ;

	}

}

void CControl::SetFont(char* font, int size)
{
	HFONT hfont ;

	hfont = CreateFont(size, 0,0,0,0,0,0,0,0,0,0,0,0,font) ;
	SendMessage(hwnd, WM_SETFONT, (WPARAM)hfont, (LPARAM)0) ;
}

// CButtonControl Member Functions

char* CButtonControl::GetText()
{
	char* buffer = NULL ;
	int length ;

	length = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, 0) ;
	buffer = new char[length] ;
	SendMessage(hwnd, WM_GETTEXT, length + 1, (LPARAM)buffer) ;
	
	return buffer ;

}

void CButtonControl::SetText(char* text)
{
	SendMessage(hwnd, WM_SETTEXT, (WPARAM)0, (LPARAM)text) ;
}
  
  
main.cpp
  
      
#include "ccontrols.h"

#define BUTTON1		1
#define EDIT1		2

CCommandButton COMMAND1 ;
CTextBox TEXT1 ;

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{

	switch(iMsg)
	{
		case WM_CREATE:
		{
			COMMAND1.Create(hwnd, BUTTON1, "button", 10, 10 ,75, 20) ;
			COMMAND1.SetFont("Verdana", 15) ;

			TEXT1.Create(hwnd, EDIT1, "edit", 95, 10, 75, 20) ;
			TEXT1.SetFont("verdana", 15) ;

			return 0 ;
		}

		case WM_COMMAND:
		{
			switch(wParam)
			{
				case BUTTON1:
				{
					MessageBox(hwnd, TEXT1.GetText(), "Test", MB_OK) ;
					COMMAND1.SetText(TEXT1.GetText()) ;
					return 0 ;
				}

				case EDIT1:
				{
					return 0 ;

				}

			}
			return 0 ;
		}
	
		case WM_DESTROY:
		{
		
			PostQuitMessage(0) ;
		
			return 0 ;
		}
	}

	return DefWindowProc(hwnd, iMsg, wParam, lParam) ;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	char* ClassName = "Class Name" ;
	HWND hwnd ;
	MSG msg ;
	WNDCLASSEX WClass ;

	WClass.cbSize = sizeof(WClass) ;
	WClass.style = CS_HREDRAW | CS_VREDRAW ;
	WClass.lpfnWndProc = WndProc ;
	WClass.cbClsExtra = 0 ;
	WClass.cbWndExtra = 0 ;
	WClass.hInstance = hInstance ;
	WClass.hIcon = LoadIcon(hInstance, IDI_APPLICATION) ;
	WClass.hIconSm = LoadIcon(hInstance, IDI_APPLICATION) ;
	WClass.hCursor = LoadCursor(NULL, IDC_ARROW) ;
	WClass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ;
	WClass.lpszMenuName = NULL ;
	WClass.lpszClassName = ClassName ;

	RegisterClassEx(&WClass) ;

	hwnd = CreateWindow(ClassName, "Test", WS_OVERLAPPEDWINDOW, 0, 0, 400, 400, NULL, NULL, hInstance, NULL) ;

	ShowWindow(hwnd, iCmdShow) ;
	UpdateWindow(hwnd) ;


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

	return msg.wParam ;
}
      
Edited by - ELS1 on February 18, 2002 10:02:12 PM Edited by - ELS1 on February 18, 2002 10:04:36 PM

Share this post


Link to post
Share on other sites
The way you have your hierarchy setup, there is no real point in having all those different classes since the only difference is style. You might as well just have CControl with another parameter style.

Looking at it from a OO point of view (at least I think), you can never create just a CControl, right? I mean it doesn''t really look like anything, just a control. So the class CControl should be abstract. It should have the members you already have, except for style, plus a pure virtual Create function. Every class that inherits from it, such as CStatic or CButtonControl, has to implement, at the least, it''s own Create function.

Share this post


Link to post
Share on other sites
quote:
Original post by masonium
The way you have your hierarchy setup, there is no real point in having all those different classes since the only difference is style. You might as well just have CControl with another parameter style.


well, im not done with all the functions. i am going to add specific functions for each control. take for example CList. ListBox specific functions will be in that inherited class...and so on. like i said...this is my first project using classes...and im trying to keep a nice OO approach. Any more suggestions/comments are welcomed.

Share this post


Link to post
Share on other sites