📄 hwnd.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: winstl/shims/access/string/hwnd.hpp
*
* Purpose: Contains classes and functions for dealing with Win32 strings.
*
* Created: 24th May 2002
* Updated: 22nd March 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2002-2007, Matthew Wilson and Synesis Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
* any contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ////////////////////////////////////////////////////////////////////// */
/** \file winstl/shims/access/string/hwnd.hpp
*
* \brief [C++] Definition of the string access shims for
* <code>HWND</code>
* (\ref group__concept__shim__string_access "String Access Shims" Concept).
*/
#ifndef WINSTL_INCL_WINSTL_SHIMS_ACCESS_STRING_HPP_HWND
#define WINSTL_INCL_WINSTL_SHIMS_ACCESS_STRING_HPP_HWND
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_HWND_MAJOR 4
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_HWND_MINOR 0
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_HWND_REVISION 1
# define WINSTL_VER_WINSTL_SHIMS_ACCESS_STRING_HPP_HWND_EDIT 108
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Includes
*/
#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
# include <stlsoft/shims/access/string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
#ifndef WINSTL_INCL_WINSTL_WINDOW_H_FUNCTIONS
# include <winstl/window/functions.h>
#endif /* !WINSTL_INCL_WINSTL_WINDOW_H_FUNCTIONS */
#ifndef WINSTL_INCL_WINSTL_WINDOW_UTIL_HPP_IDENT_
# include <winstl/window/util/ident_.hpp>
#endif /* !WINSTL_INCL_WINSTL_WINDOW_UTIL_HPP_IDENT_ */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER
# include <stlsoft/string/cstring_maker.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_MAKER */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _WINSTL_NO_NAMESPACE
# if defined(_STLSOFT_NO_NAMESPACE) || \
defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */
namespace stlsoft
{
namespace winstl_project
{
# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Functions
*/
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
inline ws_size_t GetWindowTextLength__t(HWND hwnd, int (WINAPI *pfn)(HWND ))
{
WINSTL_ASSERT(NULL != pfn);
WindowIdent ident = GetWindowIdent(hwnd);
int sel;
switch(ident)
{
case ListBox:
if(0 == (GetStyle(hwnd) & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
{
sel = static_cast<int>(::SendMessage(hwnd, LB_GETCURSEL, 0, 0l));
if(LB_ERR != sel)
{
return static_cast<ws_size_t>(::SendMessage(hwnd, LB_GETTEXTLEN, static_cast<WPARAM>(sel), 0L));
}
else
{
return 0;
}
}
break;
#if 0
case ListBox:
if(1 == SendMessage(hwnd, LVM_GETSELECTEDCOUNT, 0, 0L))
{
sel =
}
break;
#endif /* 0 */
case Generic:
case ComboBox:
case ListView:
default:
break;
}
return static_cast<ws_size_t>(pfn(hwnd));
}
inline ws_size_t GetWindowTextLength__(HWND hwnd)
{
return GetWindowTextLength__t(hwnd, ::GetWindowTextLength);
}
inline ws_size_t GetWindowTextLength__A(HWND hwnd)
{
return GetWindowTextLength__t(hwnd, ::GetWindowTextLengthA);
}
inline ws_size_t GetWindowTextLength__W(HWND hwnd)
{
return GetWindowTextLength__t(hwnd, ::GetWindowTextLengthW);
}
template <ss_typename_param_k C>
struct WindowTextLength_traits;
STLSOFT_TEMPLATE_SPECIALISATION
struct WindowTextLength_traits<ws_char_a_t>
{
static ws_size_t get_length(HWND hwnd)
{
return GetWindowTextLength__A(hwnd);
}
};
STLSOFT_TEMPLATE_SPECIALISATION
struct WindowTextLength_traits<ws_char_w_t>
{
static ws_size_t get_length(HWND hwnd)
{
return GetWindowTextLength__W(hwnd);
}
};
inline ws_size_t GetWindowTextA__(HWND hwnd, ws_char_a_t *buffer, ws_size_t cchBuffer)
{
WindowIdent ident = GetWindowIdent(hwnd);
int sel;
ws_size_t cch;
switch(ident)
{
case ListBox:
if(0 == (GetStyle(hwnd) & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
{
sel = static_cast<int>(::SendMessage(hwnd, LB_GETCURSEL, 0, 0l));
if(LB_ERR != sel)
{
cch = static_cast<ws_size_t>(::SendMessage(hwnd, LB_GETTEXT, static_cast<WPARAM>(sel), reinterpret_cast<LPARAM>(buffer)));
// Some programs using list-boxes do not null-terminate - Visual
// SourceSafe Explorer, anyone? - so we must do so here.
buffer[cch] = '\0';
}
else
{
buffer[0] = '\0';
cch = 0;
}
WINSTL_MESSAGE_ASSERT("Buffer overwrite", !(cchBuffer < cch));
return cch;
}
break;
case Generic:
case ComboBox:
case ListView:
default:
break;
}
return static_cast<ws_size_t>(::GetWindowTextA(hwnd, buffer, static_cast<int>(cchBuffer)));
}
inline ws_size_t GetWindowTextW__(HWND hwnd, ws_char_w_t *buffer, ws_size_t cchBuffer)
{
WindowIdent ident = GetWindowIdent(hwnd);
int sel;
switch(ident)
{
case ListBox:
if(0 == (GetStyle(hwnd) & (LBS_MULTIPLESEL | LBS_EXTENDEDSEL)))
{
ws_size_t cch;
sel = static_cast<int>(::SendMessage(hwnd, LB_GETCURSEL, 0, 0l));
if(LB_ERR != sel)
{
cch = static_cast<ws_size_t>(::SendMessage(hwnd, LB_GETTEXT, static_cast<WPARAM>(sel), reinterpret_cast<LPARAM>(buffer)));
}
else
{
buffer[0] = '\0';
cch = 0;
}
WINSTL_MESSAGE_ASSERT("Buffer overwrite", !(cchBuffer < cch));
return cch;
}
break;
case Generic:
case ComboBox:
case ListView:
default:
break;
}
return static_cast<ws_size_t>(::GetWindowTextW(hwnd, buffer, static_cast<int>(cchBuffer)));
}
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
/* HWND */
/** \brief This class provides an intermediary object that may be returned by the
* c_str_ptr_null() function, such that the window text of a given window may be
* accessed as a null-terminated string.
*
* \ingroup group__concept__shim__string_access
*
*/
template <ss_typename_param_k C>
class c_str_ptr_null_HWND_proxy
{
typedef cstring_maker<C> string_maker_type;
public:
typedef C char_type;
typedef c_str_ptr_null_HWND_proxy<C> class_type;
// Construction
public:
/// Constructs an instance of the proxy from the given HWND
///
/// \param h The HWND from which the text will be retrieved
ss_explicit_k c_str_ptr_null_HWND_proxy(HWND h)
{
ws_size_t length = WindowTextLength_traits<C>::get_length(h);
if(length == 0)
{
m_buffer = NULL;
}
else
{
m_buffer = string_maker_type::alloc(length);
if(NULL != m_buffer)
{
get_window_text(h, m_buffer, length + 1);
}
}
}
#ifdef STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT
/// Move constructor
///
/// This <a href = "http://synesis.com.au/resources/articles/cpp/movectors.pdf">move constructor</a>
/// is for circumstances when the compiler does not, or cannot, apply the
/// return value optimisation. It causes the contents of \c rhs to be
/// transferred into the constructing instance. This is completely safe
/// because the \c rhs instance will never be accessed in its own right, so
/// does not need to maintain ownership of its contents.
c_str_ptr_null_HWND_proxy(class_type& rhs)
: m_buffer(rhs.m_buffer)
{
move_lhs_from_rhs(rhs).m_buffer = NULL;
}
#else /* ? STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
// Copy constructor
c_str_ptr_null_HWND_proxy(class_type const& rhs)
: m_buffer(string_maker_type::dup_null(rhs.m_buffer))
{}
#endif /* STLSOFT_CF_MOVE_CONSTRUCTOR_SUPPORT */
/// Releases any storage aquired by the proxy
~c_str_ptr_null_HWND_proxy() stlsoft_throw_0()
{
string_maker_type::free(m_buffer);
}
// Accessors
public:
/// Returns a null-terminated string representing the window contents, or
/// the empty string "" if the window contains no text.
operator char_type const* () const
{
return m_buffer;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -