如何在按钮单击时从win32 c ++中的文本框中获取文本?

时间:2014-02-28 07:02:58

标签: c++ winapi win32com

我是win32 gui应用程序的新手。我正在尝试创建简单的计算器应用程序。 因为我已经为计算器设计了一个GUI。现在我想获取用户输入的文本框文本,并且想要点击我设计的从0到9的任何按钮设置文本。

出于测试目的,我编写了一个代码来从文本框中获取文本并尝试在消息框中显示它。但我的消息框显示空消息。以下是我在WinProc中的代码:

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    HDC hdc;
        PAINTSTRUCT ps;
        HWND B[16];// these are the saperate handles to each button in the main window.
        HWND hEditA,hEditB,hEditC;//handle for text box.
        char c[16][2]={"1","2","3","4","5","6","7","8","9","0","+","-","*","/",".","="};
        int i=0,j,x=100,y=130;
        int id[15]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        int cnt;
        //char val[20];
        TCHAR val[30] = {0};
        int len;


        switch(msg)
        {
            case WM_PAINT:
                hdc=BeginPaint(hWnd,&ps);
                TextOut(hdc,10,12,"Number 1:",strlen("Number 1:")+1);
                TextOut(hdc,10,47,"Number 2:",strlen("Number 2:")+1);
                TextOut(hdc,10,87,"Result  :",strlen("Result  :")+1);
                //  TextOut(hdc,10,100,"hellow sachin",strlen("hello sachin")+1);
                EndPaint(hWnd,&ps);
                break;

            case WM_DESTROY:
                PostQuitMessage (0);
                break;

            case WM_CREATE:

                            hEditA=CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER|ES_NUMBER|ES_RIGHT, 100, 10, 150, 25, hWnd, NULL, NULL, NULL);
                            hEditB=CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER|ES_NUMBER|ES_RIGHT, 100, 45, 150, 25, hWnd, NULL, NULL, NULL);
                            hEditC=CreateWindow(TEXT("Edit"), TEXT(""), WS_CHILD | WS_VISIBLE | WS_BORDER|ES_NUMBER|ES_RIGHT, 100, 85, 150, 25, hWnd, NULL, NULL, NULL);
                            cnt=0;
                            for(i=0;i<16;i++)
                            {
                                if(cnt==4)
                                {
                                    y=y+40;
                                    x=100;
                                    cnt=0;


                                }

                                if(cnt<4)
                                {
                                    B[i] =   CreateWindow(TEXT("button"), TEXT(c[i]),    
                                         WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
                                         x, y, 30, 30,        
                                         hWnd, (HMENU) id[i], NULL, NULL);
                                    x=x+40;
                                    //y=y+30;
                                    cnt++;

                                }
                            }

            case WM_COMMAND:

                    switch(LOWORD(wParam))
                    {
                        case 1:
                                len=GetWindowText( hEditA,val,10);
                               MessageBox(hWnd,(LPSTR)val, "Info", MB_OK); 
                                SetWindowText( hEditB,val);




                               break;
                                    }
    }
}

B中。我还想显示在文本框中按下的按钮的标题。

所以请任何人帮我找到解决问题的方法。

提前预订..

1 个答案:

答案 0 :(得分:2)

这应该为您提供文字:

SendMessage(hEditA, WM_GETTEXT, (WPARAM)10, (LPARAM)val);

那些窗口句柄是在窗口过程的范围内定义的,还是全局的?如果它们是在窗口过程的范围内定义的,那么在WM_CREATE将控制权返回给操作系统后,它们将被遗忘。

如果在窗口过程中定义了它们,请尝试:

static HWND hEditA, ...

编辑: 您也应该从WM_CREATE处理程序中断,因为它将落入您的WM_COMMAND处理程序。