Floris Velleman
Floris Velleman

Reputation: 4888

Accessing Components after creation win32 API C++

Taking a liking to c++ and wanting to create a windows-like application I have decided to try win32. Now that is working out pretty decent (I think). Nevertheless I find myself with a problem.

I want to create an application that has 2 buttons. When 1 is pressed the other one is no longer visible (very usefull).

The problem

I cannot seem to access the buttons I have created in my application. How can I access the created button in the CALLBACK. I am pretty sure this is a rather noobish question but it will certainly help me advance.

What have I done so far (not including all code)

enum {
  IDBC_DEFPUSHBUTTON=200
};

//Prototype functions
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int OnCreate(const HWND,CREATESTRUCT*);
HWND CreateButton(const HWND,const HINSTANCE,DWORD,const RECT&,const int,const ustring&);
inline int ErrMsg(const ustring&);

//Main
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd) {
   ...
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE: //APP-CREATE
        return OnCreate(hwnd,reinterpret_cast<CREATESTRUCT*>(lParam));
    case WM_COMMAND:
        switch(LOWORD(wParam)) {
            case IDBC_DEFPUSHBUTTON: {
                PostQuitMessage(0);
                return 0;
            }
        }
        break;
    case WM_DESTROY: //APP-END
        PostQuitMessage(0);
        return 0;
    default:
       return DefWindowProc(hwnd,uMsg,wParam,lParam);  
    }
}

The onCreate-function creates all the buttons and works great together with the CALLBACK (The app actually quits when I click on the button! (Testing purpose)). But rather then having

PostQuitMessage(0)

I would like something like:

IDBC_DEFPUSHBUTTON.Visible = false;

The following code is the OnCreate-function and the CreateButton-function:

int OnCreate(const HWND hwnd,CREATESTRUCT *cs) {
    RECT rc={10,10,200,40}; //Position Rectangle
    CreateButton(hwnd,cs->hInstance,BS_DEFPUSHBUTTON,rc,IDBC_DEFPUSHBUTTON,_T("DEFAULT PUSH BUTTON"));
    return 0;
}

//Button creation function
HWND CreateButton(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,const RECT& rc,const int id,const ustring& caption) {
    dwStyle|=WS_CHILD|WS_VISIBLE;
    return CreateWindowEx(0,_T("button"),caption.c_str(),dwStyle,rc.left,rc.top,rc.right,rc.bottom,hParent,reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),hInst,0);
}

Upvotes: 0

Views: 471

Answers (1)

Mark Ransom
Mark Ransom

Reputation: 308130

There are two ways to access a control window. One is to save the HWND returned by CreateWindowEx and use it every time you need to access the control. The other is to call GetDlgItem if you know the HWND of the parent window and the ID you assigned when you created the control, which will return the HWND of the control. Obviously it's easier just to save the original value since it won't change.

Upvotes: 1

Related Questions