⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controls.hpp

📁 一个WinCE6。0下的IP phone的源代码
💻 HPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
#pragma once

#ifndef __CONTROLS_HPP__
#define __CONTROLS_HPP__

#include <windows.h>
#include "ControlDefinitions.h"
#include "DisplayItem.hpp"


//Base class for all other controls
class CommonControl_t
{
public:
    CommonControl_t()
    {
        m_hwnd = NULL;
    }

    CommonControl_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    operator HWND()
    {
        return m_hwnd;
    }

    HRESULT
    Create(
        __in const WCHAR* pWindowClassName,
        __in_opt const WCHAR* pWindowTitle,
        DWORD Style,
        DWORD ExtendedStyle,
        HWND  Parent,
        int X,
        int Y,
        int Width,
        int Height,
        __in_opt void* pParam = NULL
        )
    {
        if (m_hwnd)
        {
            ASSERT(FALSE);
            return E_FAIL;
        }

        m_hwnd = CreateWindowEx(
            ExtendedStyle,
            pWindowClassName,
            pWindowTitle,
            Style,
            X,
            Y,
            Width,
            Height,
            Parent,
            NULL,
            NULL,
            pParam
            );
        if (! m_hwnd)
        {
            HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
            return SUCCEEDED(hr) ? E_FAIL : hr;
        }

        return S_OK;
    }

    void Destroy()
    {
        if (IsWindow(m_hwnd))
        {
            DestroyWindow(m_hwnd);
        }

        m_hwnd = NULL;
        return;
    }

protected:
    HWND m_hwnd;
};


class MenuBar_t :
    public CommonControl_t
{
public:
    //copy constructor
    MenuBar_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_MENUBAR;
    }

    int
    GetMenuButtonCount(
        void
        )
    {
        return (int)SendMessage(m_hwnd, WM_MENUBAR_GETMENUBUTTONCOUNT, 0, 0);
    }

    HMENU
    GetMenuHandle(
        void
        )
    {
        return (HMENU)SendMessage(m_hwnd, WM_MENUBAR_GETMENUHANDLE, 0, 0);
    }

    HRESULT
    SetMenu(
        HINSTANCE Instance,
        UINT ResourceId
        )
    {
        return (HRESULT)SendMessage(
            m_hwnd,
            WM_MENUBAR_SETMENU,
            (WPARAM)ResourceId,
            (LPARAM)Instance
            );
    }

    HRESULT
    SetMenuHandle(
        HMENU MenuHandle
        )
    {
        return (HRESULT)SendMessage(
            m_hwnd,
            WM_MENUBAR_SETMENUHANDLE,
            (WPARAM)MenuHandle,
            0
            );
    }

    HRESULT
    SetMenuButtonInfo(
        UINT ButtonIndex,
        __in const MENUITEMINFO* pInfo
        )
    {
        return (HRESULT)SendMessage(
            m_hwnd,
            WM_MENUBAR_SETMENUBUTTONINFO,
            ButtonIndex,
            (LPARAM)pInfo
            );
    }

    HRESULT
    SetMenuItemInfo(
        UINT MenuItemId,
        __in const MENUITEMINFO* pInfo
        )
    {
        return (HRESULT)SendMessage(
            m_hwnd,
            WM_MENUBAR_SETMENUITEMINFO,
            MenuItemId,
            (LPARAM)pInfo
            );
    }

    HRESULT
    ShowMenuButton(
        UINT MenuItemId,
        BOOL Visible
        )
    {
        return (HRESULT)SendMessage(m_hwnd, WM_MENUBAR_SHOWMENUBUTTON, MenuItemId, Visible);
    }

    inline
    HRESULT
    CheckMenuItem(
        UINT MenuItemId,
        BOOL Checked
        )
    {
        MENUITEMINFO Info;
        Info.cbSize = sizeof(MENUITEMINFO);
        Info.fMask = MIIM_STATE;
        Info.fState = (Checked ? MF_CHECKED : MF_UNCHECKED);

        return SetMenuItemInfo(MenuItemId, &Info);
    }

    inline
    HRESULT
    EnableMenuItem(
        UINT MenuItemId,
        BOOL Enabled
        )
    {
        MENUITEMINFO Info;
        Info.cbSize = sizeof(MENUITEMINFO);
        Info.fMask = MIIM_STATE;
        Info.fState = (Enabled ? MF_ENABLED : MF_GRAYED);

        return SetMenuItemInfo(MenuItemId, &Info);
    }

    void
    ResetPositions(
        void
        )
    {
        SendMessage(m_hwnd, WM_MENUBAR_RESETPOSITIONS, 0, 0);
    }
};

class MenuButton_t :
    public CommonControl_t
{
public:

    struct ButtonData_t
    {
        bool IsMenu;
        bool IsInputButton;
        union
        {
            int ControlId;
            HWND PopupMenu;
        } Value;
    };

    //copy constructor
    MenuButton_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_MENUBUTTON;
    }

    bool
    CalculateDimensions(
        __out SIZE* pDimensions
        )
    {
        return (SendMessage(m_hwnd, WM_COMMON_CALCULATE_DIMENSIONS, 0, (LPARAM)pDimensions) != 0);
    }

    HRESULT
    GetData(
        __out ButtonData_t* pData
        )
    {
        return (SendMessage(m_hwnd, WM_MENUBUTTON_GETDATA, sizeof(*pData), (LPARAM)pData) != 0) ? S_OK : E_FAIL;
    }

    HRESULT
    SetData(
        __in const ButtonData_t* pData
        )
    {
        return (SendMessage(m_hwnd, WM_MENUBUTTON_SETDATA, sizeof(*pData), (LPARAM)pData) != 0) ? S_OK : E_FAIL;
    }

    HRESULT
    Show(
        BOOL Visible
        )
    {
        return (HRESULT)SendMessage(m_hwnd, WM_MENUBUTTON_SHOW, Visible, 0);
    }
};

//forward declaration
class PopupMenuItem_t;

class PopupMenu_t :
    public CommonControl_t
{
public:
    //copy constructor
    PopupMenu_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_POPUPMENU;
    }

    bool
    CalculateDimensions(
        __out SIZE* pDimensions
        )
    {
        return (SendMessage(m_hwnd, WM_COMMON_CALCULATE_DIMENSIONS, 0, (LPARAM)pDimensions) != 0);
    }

    HRESULT
    InsertItem(
        __in PopupMenuItem_t* pItem
        )
    {
        return (SendMessage(m_hwnd, WM_POPUPMENU_INSERTITEM, 0, (LPARAM)pItem) != 0) ? S_OK : E_FAIL;
    }

    void
    SetFlyoutParent(
        HWND FlyoutParent
        )
    {
        SendMessage(m_hwnd, WM_POPUPMENU_SETFLYOUTPARENT, 0, (LPARAM)FlyoutParent);
    }

    void SetVisibleItems(
        int MaxNumberOfItems
        )
    {
        SendMessage(m_hwnd, WM_POPUPMENU_SETVISIBLEITEMS, MaxNumberOfItems, 0);
    }
};

class Edit_t :
    public CommonControl_t
{
public:
    //copy constructor
    Edit_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_EDIT;
    }

    INT_PTR
    GetCallbackPointer(
        INT_PTR pCallback
        )
    {
        return SendMessage(m_hwnd, WM_COMMON_GETCALLBACK_PTR, 0, 0);
    }

    HRESULT
    SetCallbackPointer(
        INT_PTR pCallback
        )
    {
        return (HRESULT)SendMessage(m_hwnd, WM_COMMON_SETCALLBACK_PTR, 0, pCallback);
    }

    HRESULT
    SetNumbersOnly(
        BOOL NumbersOnly
        )
    {
        HRESULT hr = S_OK;
        DWORD EditSyle = GetWindowLong(m_hwnd, GWL_STYLE);

        if (NumbersOnly)
        {
            EditSyle |= ES_NUMBER;
        }
        else
        {
            EditSyle &= ~ES_NUMBER;
        }

        if (0 == SetWindowLong(m_hwnd, GWL_STYLE, EditSyle))
        {
            hr = HRESULT_FROM_WIN32(GetLastError());
        }

        return hr;
    }

};

class ListBox_t :
    public CommonControl_t
{
public:
    //copy constructor
    ListBox_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_LISTBOX;
    }

    HRESULT
    AddItem(
        int Index,
        __in IVoIPDisplayItem* pItem
        )
    {
        if (!IsWindow(m_hwnd))
        {
            return HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE);
        }

        HRESULT hr;

        int Result = SendMessage(m_hwnd, LB_INSERTSTRING, Index, (LPARAM)pItem);
        switch (Result)
        {
        case LB_ERRSPACE:
            hr = E_OUTOFMEMORY;
            break;
        case LB_ERR:
            hr = E_FAIL;
            break;
        default:
            hr = S_OK;
            break;
        }

        return hr;
    }

    IVoIPDisplayItem*
    GetItem(
        int Index
        )
    {
        int Result = SendMessage(m_hwnd, LB_GETITEMDATA, Index, 0);
        if (Result == LB_ERR)
        {
            return NULL;
        }
        return reinterpret_cast<IVoIPDisplayItem*>(Result);
    }

    HRESULT
    SetItemData(
        int Index,
        __in_opt void* pData
        )
    {
        if (!IsWindow(m_hwnd))
        {
            return HRESULT_FROM_WIN32(ERROR_INVALID_WINDOW_HANDLE);
        }

        return (SendMessage(m_hwnd, LB_SETITEMDATA, Index, (LPARAM)pData) != LB_ERR) ? S_OK : E_FAIL;
    }

    int
    GetItemRect(
        int Index,
        __out RECT* pRectangle
        )
    {
        return (int)SendMessage(m_hwnd, LB_GETITEMRECT, Index, (LPARAM)pRectangle);
    }

    int
    GetCount(
        void
        )
    {
        return (int)SendMessage(m_hwnd, LB_GETCOUNT, 0, 0);
    }

    int
    GetTopIndex(
        void
        )
    {
        return (int)SendMessage(m_hwnd, LB_GETTOPINDEX, 0, 0);
    }

    int
    SetTopIndex(
        int NewTop
        )
    {
        return (int)SendMessage(m_hwnd, LB_SETTOPINDEX, NewTop, 0);
    }

    int
    GetBottomIndex(
        void
        )
    {
        RECT ClientRect;
        RECT ItemRect;

        int ItemCount = GetCount();
        int Index = GetTopIndex();

        if ((Index == LB_ERR) || (0 == ItemCount))
        {
            return -1;
        }
        
        GetClientRect(m_hwnd, &ClientRect);
        while (Index < ItemCount)
        {
            GetItemRect(Index, &ItemRect);
            if (ItemRect.bottom > ClientRect.bottom)
            {
                //only include WHOLE items
                return Index-1;
            }
        
            Index++;
        }
            
        return ItemCount-1;
    }

    int
    GetCurSel(
        void
        )
    {
        return (int)SendMessage(m_hwnd, LB_GETCURSEL, 0, 0);
    }
    int
    SetCurSel(
        int ItemToSelect
        )
    {
        return (int)SendMessage(m_hwnd, LB_SETCURSEL, ItemToSelect, 0);
    }
};

class StatusHeader_t :
    public CommonControl_t
{
public:
    //copy constructor
    StatusHeader_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_STATUSHEADER;
    }
};

class ToolTip_t :
    public CommonControl_t
{
public:
    //copy constructor
    ToolTip_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_TOOLTIP;
    }

    HRESULT
    GetWindowMetrics(
        UINT MaximumWidth,
        __out SIZE* pSize
        )
    {
        return (HRESULT)SendMessage(
            m_hwnd,
            WM_TOOLTIP_GETMETRICS,
            (WPARAM)MaximumWidth,
            (LPARAM)pSize
            );
    }
};

class TrackBar_t :
    public CommonControl_t
{
public:
    //copy constructor
    TrackBar_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_TRACKBAR;
    }

    INT_PTR
    GetCallbackPointer(
        INT_PTR pCallback
        )
    {
        return SendMessage(m_hwnd, WM_COMMON_GETCALLBACK_PTR, 0, 0);
    }

    HRESULT
    SetCallbackPointer(
        INT_PTR pCallback
        )
    {
        return (HRESULT)SendMessage(m_hwnd, WM_COMMON_SETCALLBACK_PTR, 0, pCallback);
    }
};


class TransparentTextBox_t :
    public CommonControl_t
{
public:
    //copy constructor
    TransparentTextBox_t& operator=(HWND hwnd)
    {
        m_hwnd = hwnd;
        return *this;
    }

    //WndClassName
    static
    const WCHAR*
    GetWindowClassName(
        void
        )
    {
        return WNDCLASS_TRANSPARENTTEXT;
    }

    HRESULT
    GetText(
        __out_ecount(cchBuffer) WCHAR* pBuffer,
        int cchBuffer
        )
    {
        if (0 >= GetWindowTextW(m_hwnd, pBuffer, cchBuffer))
        {
            HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
            return SUCCEEDED(hr) ? E_FAIL : hr;
        }
        return S_OK;
    }

    HRESULT
    SetText(
        __in_opt const WCHAR* pText
        )
    {
        if (!SetWindowTextW(m_hwnd, pText))
        {
            HRESULT hr = HRESULT_FROM_WIN32(GetLastError());
            return SUCCEEDED(hr) ? E_FAIL : hr;
        }
        return S_OK;
    }

    void
    SetFont(
        HFONT Font
        )
    {
        SendMessage(m_hwnd, WM_SETFONT, (WPARAM)Font, 0);
    }
};

#endif // !defined __CONTROLS_HPP__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -