PDA

Click to See Complete Forum and Search --> : Making a global variable that's a class and has it's constructor method that's requir


V(ery) Basic
Sep 30th, 2000, 07:55 AM
class Window;
class ComboBox;


class Window
{
public:
void Show();
void Hide();
void SetFont(HFONT NewFont);
HWND GethWnd();
protected:
HWND itshWnd;
};

HWND Window::GethWnd()
{
return itshWnd;
}


void Window::Show()
{
ShowWindow(itshWnd, SW_NORMAL);
}

void Window::Hide()
{
ShowWindow(itshWnd, SW_HIDE);
}

void Window::SetFont(HFONT NewFont)
{
SendMessage(itshWnd, WM_SETFONT, (WPARAM) NewFont, (LPARAM) true);
}



class ComboBox: public Window
{
public:
//constructors
ComboBox(RECT area, HWND OwnerhWnd, HINSTANCE hStance);
//ComboBox(RECT area, HWND OwnerhWnd);
~ComboBox();
void AddItem(char *NewItem);
int GetItem();
protected:
HWND Owner;
HINSTANCE hIns;
};


ComboBox::ComboBox(RECT area, HWND OwnerhWnd, HINSTANCE hStance)
{
Owner = OwnerhWnd;
//hIns = hStance;
itshWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "ComboBox", "", WS_CHILD | CBS_DROPDOWNLIST , area.left, area.top, area.right - area.left, area.bottom - area.top, Owner, 0, hStance, NULL);
}

/* ComboBox::ComboBox(RECT area, HWND OwnerhWnd)
{
//Assumes that a different Combo (with HINSTANCE) has been created
Owner = OwnerhWnd;
itshWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "ComboBox", "", WS_CHILD | CBS_DROPDOWNLIST , area.left, area.top, area.right - area.left, area.bottom - area.top, Owner, 0, hIns, NULL);
} */

ComboBox::~ComboBox()
{
//Nothing
}

void ComboBox::AddItem(char *NewItem)
{
SendMessage(itshWnd, CB_ADDSTRING, 0, (WPARAM) NewItem);
}

int ComboBox::GetItem()
{
return (int) SendMessage(itshWnd, CB_GETCURSEL, 0, 0);
}



In case you hadn't noticed I'm trying to make my own sort of class for each control, starting with comboboxes.

Now, what I want to do is to be able to access the class
from any part of code module. But if I make a global variable:


ComboBox cmbOper(WndRect, hWnd, hInst);


It obviously fails miserably because hInst, hWnd and WndRect haven't been created yet.

I tried something to do with the free store, but I couldn't access it outside of the procedure where I created it because the actual memory still existed, but the name I had given it was not declared in the routine I was trying to call it from, and that me... Arrrrgh.

Plain and simple:

How do I create an object that I can access anywhere in my code given that it has its own constructor?

There's got to be a way...

parksie
Sep 30th, 2000, 08:58 AM
How about a global variable that's a pointer?

ComboBox *gpCombo;

WinMain(...) {
gpCombo = new ComboBox(...);

...

delete gpCombo;
}

V(ery) Basic
Sep 30th, 2000, 11:38 AM
Thanks soooo much. I really needed that. I'll help you one day.

<ahem>

<ahem>

<ahem><ahem>

<ahem><ahem>

<ahem><ahem><ahem>

*breaks into mad coughing fit*

Sorry. :(

parksie
Sep 30th, 2000, 12:01 PM
Okay...lets see if you can help me with this small query:

Do you know how to use a C++ class from VB???

Hehehe.

PS: This is a genuine question (I just never bothered asking after I only got 2 views, and one was mine)

V(ery) Basic
Oct 1st, 2000, 07:57 AM
You'd obviously... err ...

<looks through book>
<realises the 'book' is PLAYBOY>
<sees in-depth article in C++-To-VB class porting on page 27>
<Notices they're 'porting' something else on page 28> ;)
<Can't be bothered to answer question now that he's seen page 31 ;) >
<Sorry, parksie, business before pleasure :D >

parksie
Oct 1st, 2000, 01:43 PM
Hey - you been going through my stuff?