zoukankan      html  css  js  c++  java
  • win32- GetWindowText

    从编辑框中获取控件文本

    一般常用的方法是,

    wchar_t buffer[100];
    GetWindowText(hWnd, buffer, sizeof(buffer) / sizeof(buffer[0]));

    但是这样浪费了数组空间, 所以一般使用GetWindowTextLengthW获取控件文本的长度,前提是以string声明字符串

    wstring note;
    int length = GetWindowTextLengthW(hWnd);
    if (length > 0)
    {
        note.resize(length);
        length = GetWindowTextW(hWnd, note.data()/*or: &note[0]*/, length + 1);
        note.resize(length);
    }

    请注意,从技术上讲这是C ++ 11之前的未定义行为,因为不能保证wstringdata()operator[]成员返回指向连续内存中数据的指针,并且不能保证缓冲区以null结尾。只有c_str()可以保证以NULL结尾。所以我们常常可以使用const_cast<wchar_t*>移除const属性

    wstring note;
    int length = GetWindowTextLengthW(hWnd);
    if (length > 0)
    {
        note.resize(length);
        length = GetWindowTextW(hWnd, const_cast<wchar_t*>(note.c_str()), length + 1);
        note.resize(length);
    }

    如果您想对其合法,则在C ++ 11之前使用单独的缓冲区,然后将其复制到wstring之后,例如:

    wstring note;
    int length = GetWindowTextLengthW(hWnd);
    if (length > 0)
    {
        ++length;
        vector<wchar_t> buf(length);
        length = GetWindowTextW(hWnd, &buf[0], length);
        note.assign(&buf[0], length);
    }
  • 相关阅读:
    Superwebsocket 模拟微信聊天室
    python杂记-6(time&datetime模块)
    python杂记-5(装饰器)
    python杂记-4(迭代器&生成器)
    python杂记-3(购买商品)
    python杂记-2(python之文件)
    python杂记-1(os模块)
    淘宝链接中的spm参数
    分布式Web服务器架构(转)
    asp.net网站增加虚拟目录,用来访问网站图片。
  • 原文地址:https://www.cnblogs.com/strive-sun/p/12893122.html
Copyright © 2011-2022 走看看