11Edit编辑框

Edit编辑框

 

我们经常使用的框框,比如:登陆账号、密码时。这就是编辑框。编辑框同样是窗口。

CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",0,WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,4,4,170,30,hWnd,NULL,hInst,0);

 

注意:在前面的按钮、static,第一个参数都是为0,但是在本例中,第一参数不可以为0,否则编辑框没有框框,但可以输入内容,如下图:  

编辑框的样式是多样的,只需要改变第四个参数dwStyle。列出几个:

比较:

1组:

WS_HSCROLL                     水平移动,出现滚动条

WS_VSCROLL                     垂直移动,出现滚动条

2组:

ES_AUTOHSCROLL                    自动水平移动,不出现滚动条

ES_AUTOVSCROLL                    自动垂直移动,不出现滚动条

 

ES_UPPERCASE                输入字母全变成大写

ES_LOWERCASE               输入字母全变成小写

ES_PASSWORD                   隐藏输入内容

ES_NUMBER                      只能输入数字

ES_MULTILINE                            多行

ES_READONLY         只读编辑框

 

下面介绍两函数GetWindowText()和SetWindowText,用于复制和粘贴。。

char strCopy[MAX_PATH];      

case WM_COMMAND:

                   wmId    = LOWORD(wParam);

                   wmEvent = HIWORD(wParam);

                   // 分析菜单选择:

                   switch (wmId)

                   {

                   case IDC_BTN_COPY:

                            //if(wmId==IDC_BTN_COPY)

                            if(wmEvent==BN_CLICKED)

                            {

                                     GetWindowText(GetDlgItem(hWnd,IDC_EDT_READ),strCopy,MAX_PATH);

                                     SetWindowText(GetDlgItem(hWnd,IDC_EDT_PASTE),strCopy);

                            }

                            break;

 

GetWindowText(),可以通过窗口句柄获取窗口中的全部内容,所以通过GetDlgItem()来获取某控件(edit的本质是窗口)的句柄,再将内容存放在strCopy。

SetWindowText(),可以将内容放在其他窗口,进行重新设置。

 

当然也有另一个方法。利用消息SendMessage()

SendMessage(hWnd,WM_COPY,strCopy,0);

SendMessage(hWnd, WM_PASTE,strCopy,0);

 

注意:

         wmId    = LOWORD(wParam);

         wmEvent = HIWORD(wParam);

case IDC_BTN_COPY:

//if(wmId==IDC_BTN_COPY)

         if(wmEvent==BN_CLICKED)

两个if语句是等效的,第一个是通过控件的ID进行判断。第二个是通过事件,判断该控件是否被点击。

 

结果:

 

源代码:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    static int nSize=40;
    char strCopy[MAX_PATH];

    static char str[MAX_PATH]={"书山有路勤为径,\r\n学海无涯苦作舟。\r\n\r\n不知天上宫阙,\r\n今夕是何年。"};
    switch (message)
    {
    case WM_CREATE:
        //普通编辑框
        CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",0,WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL,4,4,170,30,hWnd,NULL,hInst,0);
        //密码编辑框
        CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",0,WS_CHILD|WS_VISIBLE|ES_PASSWORD,4,4+nSize,170,30,hWnd,NULL,hInst,0);
        //全大写字母编辑框
        CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",0,WS_CHILD|WS_VISIBLE|ES_UPPERCASE,4,4+nSize*2,170,30,hWnd,NULL,hInst,0);
        //多行编辑框
        CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",0,WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE,4,4+nSize*3,200,200,hWnd,(HMENU)IDC_EDT_PASTE,hInst,0);
        //只读编辑框
        CreateWindowEx(WS_EX_CLIENTEDGE,"EDIT",str,WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE|ES_READONLY,4+nSize*6,4+nSize*3,200,200,hWnd,(HMENU)IDC_EDT_READ,hInst,0);

        CreateWindowEx(0,"button","复制",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
            4,4+nSize*8,40,30,hWnd,(HMENU)IDC_BTN_COPY,hInst,0);
        break;
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // 分析菜单选择:
        switch (wmId)
        {
        case IDC_BTN_COPY:
            //if(wmId==IDC_BTN_COPY)
            if(wmEvent==BN_CLICKED)
            {
                GetWindowText(GetDlgItem(hWnd,IDC_EDT_READ),strCopy,MAX_PATH);
                SetWindowText(GetDlgItem(hWnd,IDC_EDT_PASTE),strCopy);
            }
            break;
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        // TODO: 在此添加任意绘图代码...
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

 

posted @ 2017-09-03 21:48  gd_沐辰  阅读(270)  评论(0编辑  收藏  举报