Results 1 to 6 of 6

Thread: Making a global variable that's a class and has it's constructor method that's requir

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Code:
    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:

    Code:
    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...
    Courgettes.

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How about a global variable that's a pointer?
    Code:
    ComboBox *gpCombo;
    
    WinMain(...) {
       gpCombo = new ComboBox(...);
    
       ...
    
       delete gpCombo;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    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.

    Courgettes.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    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 >
    Courgettes.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Talking LOL

    Hey - you been going through my stuff?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width