简体   繁体   English

创建win32 API C ++后访问组件

[英]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. 喜欢c ++并想创建类似Windows的应用程序,我决定尝试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. 我想创建一个具有2个按钮的应用程序。 When 1 is pressed the other one is no longer visible (very usefull). 当按下1时,另一个不再可见(非常有用)。

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)). onCreate函数创建所有按钮,并与CALLBACK配合使​​用(当我单击按钮时,该应用程序实际上退出!(测试目的))。 But rather then having 但是宁可

PostQuitMessage(0) PostQuitMessage(0)

I would like something like: 我想要类似的东西:

IDBC_DEFPUSHBUTTON.Visible = false; IDBC_DEFPUSHBUTTON.Visible = false;

The following code is the OnCreate-function and the CreateButton-function: 以下代码是OnCreate函数和CreateButton函数:

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);
}

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. 一种是保存CreateWindowEx返回的HWND ,并在每次需要访问控件时使用它。 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. 如果您知道父窗口的HWND和创建控件时分配的ID,则另一个方法是调用GetDlgItem ,这将返回控件的HWND Obviously it's easier just to save the original value since it won't change. 显然,保存原始值会更容易,因为它不会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM