Button控件自绘

1. 派生CButton

2. 重写PreSubclassWindow函数,设置BS_OWNERDRAW属性

  void CButtonEx::PreSubclassWindow() {

  // TODO: 在此添加专用代码和/或调用基类

  UINT nStyle = GetButtonStyle();

  SetButtonStyle(nStyle | BS_OWNERDRAW);

  CButton::PreSubclassWindow();

  }

3. 重写DrawItem函数实现Button重绘

ButtonEx.h

/* @Button自绘控件类设计
*根据鼠标动作绘制相对应的button形态
*鼠标动作:停止、按下、弹起
*button形态:
*/
#pragma once

// CButtonEx
class CButtonEx : public CButton
{
    DECLARE_DYNAMIC(CButtonEx)

public:
    CButtonEx();
    virtual ~CButtonEx();

protected:
    DECLARE_MESSAGE_MAP()
    afx_msg void OnKillFocus(CWnd* pNewWnd);
    afx_msg void OnCaptureChanged(CWnd *pWnd);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);

    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    virtual void PreSubclassWindow();
    void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed/*, BOOL IsDisabled*/);

public:
    void SetIcon(int nIconInId, int nIconOutId = NULL, BYTE cx = 32, BYTE cy = 32);

private:
    BOOL m_MouseOnButton;
    HICON m_hIconIn;
    HICON m_hIconOut;
    BYTE m_cyIcon;
    BYTE m_cxIcon;

    int m_nAlign;
};

ButtonEx.cpp

// ButtonEx.cpp : 实现文件
//

#include "stdafx.h"
#include "mfcctrlstu.h"
#include "ButtonEx.h"

#include "ButtonBase.h"

// CButtonEx

IMPLEMENT_DYNAMIC(CButtonEx, CButton)

CButtonEx::CButtonEx()
{
    m_MouseOnButton = FALSE;
    m_nAlign = ST_ALIGN_HORIZ;
}

CButtonEx::~CButtonEx()
{
    if (m_hIconIn != NULL) ::DeleteObject(m_hIconIn);
    if (m_hIconOut != NULL) ::DeleteObject(m_hIconOut);
}

BEGIN_MESSAGE_MAP(CButtonEx, CButton)
    ON_WM_MOUSEMOVE()
    ON_WM_KILLFOCUS()
    ON_WM_CAPTURECHANGED()
END_MESSAGE_MAP()

// CButtonEx 消息处理程序
void CButtonEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    CRect itemRect = lpDrawItemStruct->rcItem;

    BOOL bIsPressed  = (lpDrawItemStruct->itemState & ODS_SELECTED);
    BOOL bIsFocused  = (lpDrawItemStruct->itemState & ODS_FOCUS);
    BOOL bIsDisabled = (lpDrawItemStruct->itemState & ODS_DISABLED);
    CButtonBase *pButton = NULL;
    if (bIsPressed)
    {
        pButton = new CPressButton();
    }
    else if (m_MouseOnButton)
    {
        pButton = new CMouseOnButton();
    }
    else
    {
        pButton = new CFlatButton();
    }
    // 属性初始化
    CString strTitle="";
    GetWindowText(strTitle);
    COLORREF color = RGB(255,0,0);
    pButton->SetButtonText(strTitle);
    pButton->SetBkColor(color);
    pButton->SetTextColor(color);
    pButton->SetIcon(m_hIconIn, m_hIconOut,m_cxIcon, m_cyIcon);
    pButton->SetGroupLignMode(m_nAlign);
    pButton->Draw(pDC, itemRect);

    delete pButton;
    pButton = NULL;
}

void CButtonEx::PreSubclassWindow()
{
    // TODO: 在此添加专用代码和/或调用基类
    UINT nStyle = GetButtonStyle();
    SetButtonStyle(nStyle | BS_OWNERDRAW);

    CButton::PreSubclassWindow();
}

void CButtonEx::OnMouseMove(UINT nFlags, CPoint point)
{
    CWnd* pWnd;  // Finestra attiva
    CWnd* pParent; // Finestra che contiene il bottone

    CButton::OnMouseMove(nFlags, point);

    // If the mouse enter the button with the left button pressed
    // then do nothing
    if (nFlags & MK_LBUTTON && m_MouseOnButton == FALSE) return;

    // If our button is not flat then do nothing
    //if (m_bIsFlat == FALSE) return;

    pWnd = GetActiveWindow();
    pParent = GetOwner();

    if ((GetCapture() != this) &&
        (
#ifndef ST_LIKEIE
        pWnd != NULL &&
#endif
        pParent != NULL))
    {
        m_MouseOnButton = TRUE;
        //SetFocus();    // Thanks Ralph!
        SetCapture();
        Invalidate();
    }
    else
    {
        CRect rc;
        GetClientRect(&rc);
        if (!rc.PtInRect(point))
        {
            // Redraw only if mouse goes out
            if (m_MouseOnButton == TRUE)
            {
                m_MouseOnButton = FALSE;
                Invalidate();
            }
            // If user is NOT pressing left button then release capture!
            if (!(nFlags & MK_LBUTTON)) ReleaseCapture();
        }
    }
}

void CButtonEx::OnKillFocus(CWnd* pNewWnd)
{
    CButton::OnKillFocus(pNewWnd);

    // If our button is not flat then do nothing
    //if (m_bIsFlat == FALSE) return;

    if (m_MouseOnButton == TRUE)
    {
        m_MouseOnButton = FALSE;
        Invalidate();
    }
}

void CButtonEx::OnCaptureChanged(CWnd *pWnd)
{
    if (m_MouseOnButton == TRUE)
    {
        ReleaseCapture();
        Invalidate();
    }

    // Call base message handler
    CButton::OnCaptureChanged(pWnd);
} // End of OnCaptureChanged

void CButtonEx::SetIcon(int nIconInId, int nIconOutId, BYTE cx, BYTE cy)
{
    HINSTANCE hInstResource = AfxFindResourceHandle(MAKEINTRESOURCE(nIconInId),RT_GROUP_ICON);
    // Set icon when the mouse is IN the button
    m_hIconIn = (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconInId), IMAGE_ICON, 0, 0, 0);

    // Set icon when the mouse is OUT the button
    m_hIconOut = (nIconOutId == NULL) ? m_hIconIn : (HICON)::LoadImage(hInstResource/*AfxGetApp()->m_hInstance*/, MAKEINTRESOURCE(nIconOutId), IMAGE_ICON, 0, 0, 0);

    m_cxIcon = cx;
    m_cyIcon = cy;

    RedrawWindow();
} // End of SetIcon

void CButtonEx::DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed/*, BOOL IsDisabled*/)
{
    CRect iconRect = rcItem;

    // 根据对齐方式进行布局
    switch (m_nAlign)
    {
    case ST_ALIGN_HORIZ:
        if (title->IsEmpty())
        {
            // Center the icon horizontally
            iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        }
        else
        {
            // L‘icona deve vedersi subito dentro il focus rect
            iconRect.left += 3;
            captionRect->left += m_cxIcon + 3;
        }
        // Center the icon vertically
        iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        break;
    case ST_ALIGN_VERT:
        // Center the icon horizontally
        iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        if (title->IsEmpty())
        {
            // Center the icon vertically
            iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        }
        else
        {
            captionRect->top += m_cyIcon;
        }
        break;
    }

    // If button is pressed then press the icon also
    if (IsPressed) iconRect.OffsetRect(1, 1);
    // Ole‘!
    pDC->DrawState(iconRect.TopLeft(),
        iconRect.Size(),
        (m_MouseOnButton == TRUE || IsPressed) ? m_hIconIn : m_hIconOut,
        DSS_NORMAL,
        /*(IsDisabled ? DSS_DISABLED : DSS_NORMAL), */
        (CBrush*)NULL);
} // End of DrawTheIcon

 ButtonBase.h

/*
*
*/
#pragma once

enum TEXT_ICON_ALIGN
{
    ST_ALIGN_HORIZ = 0,
    ST_ALIGN_VERT
};

class CButtonBase
{
public:
    CButtonBase(){};
    virtual ~CButtonBase(){};

    virtual void Draw(CDC *pDC, CRect itemRect) = 0;      // button绘制

    virtual void SetButtonText(CString StrText)=0;
    virtual void SetBkColor(COLORREF color)=0;
    virtual void SetTextColor(COLORREF color)=0;
    virtual void SetIcon(HICON hIconIn=NULL, HICON hIconOut=NULL, BYTE m_cyIcon=32,BYTE m_cxIcon=32)=0;
    virtual void SetGroupLignMode(int nAlignType = ST_ALIGN_HORIZ)=0;

protected:
    CString m_strText;           // 文本内容
    COLORREF m_bkColor;          // 背景色
    COLORREF m_TextColor;        // 文本颜色

    HICON m_hIconIn;            // 图标1
    HICON m_hIconOut;           // 图标2;
    BYTE m_cyIcon;
    BYTE m_cxIcon;

    int m_nAlign;               // 文本、icon对齐方式
};

class CMouseOnButton : public CButtonBase
{
public:
    CMouseOnButton();
    virtual ~CMouseOnButton();

    void Draw(CDC *pDC, CRect itemRect);
    void SetButtonText(CString StrText){m_strText=StrText;}
    void SetBkColor(COLORREF color){m_bkColor=color;}
    void SetTextColor(COLORREF color){m_TextColor=color;}
    void SetIcon(HICON hIconIn=NULL, HICON hIconOut=NULL,BYTE cyIcon=32,BYTE cxIcon=32)
    {
        m_hIconIn=hIconIn;
        m_hIconOut=hIconOut;
        m_cxIcon = cxIcon;
        m_cyIcon = cyIcon;
    }
    void SetGroupLignMode(int nAlignType = ST_ALIGN_HORIZ){m_nAlign=nAlignType;}

private:
    void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect);
};

class CFlatButton : public CButtonBase
{
public:
    CFlatButton();
    virtual ~CFlatButton();

    void Draw(CDC *pDC, CRect itemRect);
    void SetButtonText(CString StrText){m_strText=StrText;}
    void SetBkColor(COLORREF color){m_bkColor=color;}
    void SetTextColor(COLORREF color){m_TextColor=color;}
    void SetIcon(HICON hIconIn=NULL, HICON hIconOut=NULL,BYTE cyIcon=32,BYTE cxIcon=32)
    {
        m_hIconIn=hIconIn;
        m_hIconOut=hIconOut;
        m_cxIcon = cxIcon;
        m_cyIcon = cyIcon;
    }
    void SetGroupLignMode(int nAlignType = ST_ALIGN_HORIZ){m_nAlign=nAlignType;}

private:
    void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect);
};

class CPressButton : public CButtonBase
{
public:
    CPressButton();
    virtual ~CPressButton();

    void Draw(CDC *pDC, CRect itemRect);
    void SetButtonText(CString StrText){m_strText=StrText;}
    void SetBkColor(COLORREF color){m_bkColor=color;}
    void SetTextColor(COLORREF color){m_TextColor=color;}
    void SetIcon(HICON hIconIn=NULL, HICON hIconOut=NULL,BYTE cyIcon=32,BYTE cxIcon=32)
    {
        m_hIconIn=hIconIn;
        m_hIconOut=hIconOut;
        m_cxIcon = cxIcon;
        m_cyIcon = cyIcon;
    }
    void SetGroupLignMode(int nAlignType = ST_ALIGN_HORIZ){m_nAlign=nAlignType;}

private:
    void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect);
};

 ButtonBase.cpp

#include "stdafx.h"
#include "ButtonBase.h"

// 平按钮类实现
CFlatButton::CFlatButton()
{
    m_strText = "";
    m_bkColor = RGB(0,0,0);
    m_TextColor = RGB(0,0,0);
    m_hIconIn = NULL;
    m_hIconOut = NULL;
    m_cyIcon = 32;
    m_cxIcon = 32;
    m_nAlign = 0;
}

CFlatButton::~CFlatButton()
{

}

void CFlatButton::Draw( CDC *pDC, CRect itemRect )
{
    itemRect.DeflateRect(1,1);
    CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black
    CPen *pOldPen = pDC->SelectObject(&pen3DDKShadow);
    //CBrush bkBrush(m_bkColor);
    //pDC->FillRect(&itemRect, &bkBrush);
    CRect rcICon = itemRect;
    DrawTheIcon(pDC, &m_strText, &rcICon,&itemRect);
    pDC->SetBkMode(TRANSPARENT);
    pDC->DrawText(m_strText, &itemRect,DT_SINGLELINE|DT_CENTER);
    pDC->SetTextColor(m_TextColor);

    pDC->SelectObject(pOldPen);
}

void CFlatButton::DrawTheIcon( CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect )
{
    CRect iconRect = rcItem;

    // 根据对齐方式进行布局
    switch (m_nAlign)
    {
    case ST_ALIGN_HORIZ:
        if (title->IsEmpty())
        {
            // Center the icon horizontally
            iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        }
        else
        {
            // L‘icona deve vedersi subito dentro il focus rect
            iconRect.left += 3;
            captionRect->left += m_cxIcon + 3;
        }
        // Center the icon vertically
        iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        break;
    case ST_ALIGN_VERT:
        // Center the icon horizontally
        iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        if (title->IsEmpty())
        {
            // Center the icon vertically
            iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        }
        else
        {
            captionRect->top += m_cyIcon;
        }
        break;
    }

    pDC->DrawState(iconRect.TopLeft(), iconRect.Size(), m_hIconOut, DSS_NORMAL,(CBrush*)NULL);
}

CMouseOnButton::CMouseOnButton()
{
    m_strText = "";
    m_bkColor = RGB(0,0,0);
    m_TextColor = RGB(0,0,0);
    m_hIconIn = NULL;
    m_hIconOut = NULL;
    m_cyIcon = 32;
    m_cxIcon = 32;
    m_nAlign = 0;
}

CMouseOnButton::~CMouseOnButton()
{

}

void CMouseOnButton::Draw( CDC *pDC, CRect itemRect)
{
    CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // White
    CPen pen3DLight(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));       // Light gray
    CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Dark gray
    CPen pen3DDKShadow(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); // Black

    CPen *pOldPen = pDC->SelectObject(&penBtnHiLight);
    pDC->MoveTo(itemRect.left, itemRect.bottom-1);
    pDC->LineTo(itemRect.left, itemRect.top);
    pDC->LineTo(itemRect.right, itemRect.top);
    // Disegno i bordi a destra e in basso
    // Dark gray line
    pDC->SelectObject(penBtnShadow);
    pDC->MoveTo(itemRect.left, itemRect.bottom-1);
    pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
    pDC->LineTo(itemRect.right-1, itemRect.top-1);

    itemRect.DeflateRect(1,1);
    pDC->SelectObject(&pen3DDKShadow);
    //CBrush bkBrush(m_bkColor);
    //pDC->FillRect(&itemRect, &bkBrush);
    CRect rcICon = itemRect;
    DrawTheIcon(pDC, &m_strText, &rcICon,&itemRect);
    pDC->SetBkMode(TRANSPARENT);
    pDC->DrawText(m_strText, &itemRect,DT_SINGLELINE|DT_CENTER);
    pDC->SetTextColor(m_TextColor);

    pDC->SelectObject(pOldPen);
}

void CMouseOnButton::DrawTheIcon( CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect )
{
    CRect iconRect = rcItem;

    // 根据对齐方式进行布局
    switch (m_nAlign)
    {
    case ST_ALIGN_HORIZ:
        if (title->IsEmpty())
        {
            // Center the icon horizontally
            iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        }
        else
        {
            // L‘icona deve vedersi subito dentro il focus rect
            iconRect.left += 3;
            captionRect->left += m_cxIcon + 3;
        }
        // Center the icon vertically
        iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        break;
    case ST_ALIGN_VERT:
        // Center the icon horizontally
        iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        if (title->IsEmpty())
        {
            // Center the icon vertically
            iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        }
        else
        {
            captionRect->top += m_cyIcon;
        }
        break;
    }

    pDC->DrawState(iconRect.TopLeft(), iconRect.Size(), m_hIconOut, DSS_NORMAL,(CBrush*)NULL);
}

// PressButton类实现
CPressButton::CPressButton()
{
    m_strText = "";
    m_bkColor = RGB(0,0,0);
    m_TextColor = RGB(0,0,0);
    m_hIconIn = NULL;
    m_hIconOut = NULL;
    m_cyIcon = 32;
    m_cxIcon = 32;
    m_nAlign = 0;
}

CPressButton::~CPressButton()
{

}

void CPressButton::Draw( CDC *pDC, CRect itemRect )
{
    CPen penBtnHiLight(PS_SOLID, 0, GetSysColor(COLOR_BTNHILIGHT)); // Bianco
    CPen penBtnShadow(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));   // Grigio scuro

    // Disegno i bordi a sinistra e in alto
    // Dark gray line
    CPen *pOldPen = pDC->SelectObject(&penBtnShadow);
    pDC->MoveTo(itemRect.left, itemRect.bottom-1);
    pDC->LineTo(itemRect.left, itemRect.top);
    pDC->LineTo(itemRect.right, itemRect.top);
    // Disegno i bordi a destra e in basso
    // White line
    pDC->SelectObject(penBtnHiLight);
    pDC->MoveTo(itemRect.left, itemRect.bottom-1);
    pDC->LineTo(itemRect.right-1, itemRect.bottom-1);
    pDC->LineTo(itemRect.right-1, itemRect.top-1);

    itemRect.DeflateRect(1,1);
//    pDC->SelectObject(&pen3DDKShadow);
    //CBrush bkBrush(m_bkColor);
    //pDC->FillRect(&itemRect, &bkBrush);
    CRect rcICon = itemRect;
    DrawTheIcon(pDC, &m_strText, &rcICon,&itemRect);
    pDC->SetBkMode(TRANSPARENT);
    pDC->DrawText(m_strText, &itemRect,DT_SINGLELINE|DT_CENTER);
    pDC->SetTextColor(m_TextColor);

    pDC->SelectObject(pOldPen);
}

void CPressButton::DrawTheIcon( CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect )
{

    CRect iconRect = rcItem;

    // 根据对齐方式进行布局
    switch (m_nAlign)
    {
    case ST_ALIGN_HORIZ:
        if (title->IsEmpty())
        {
            // Center the icon horizontally
            iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        }
        else
        {
            // L‘icona deve vedersi subito dentro il focus rect
            iconRect.left += 3;
            captionRect->left += m_cxIcon + 3;
        }
        // Center the icon vertically
        iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        break;
    case ST_ALIGN_VERT:
        // Center the icon horizontally
        iconRect.left += ((iconRect.Width() - m_cxIcon)/2);
        if (title->IsEmpty())
        {
            // Center the icon vertically
            iconRect.top += ((iconRect.Height() - m_cyIcon)/2);
        }
        else
        {
            captionRect->top += m_cyIcon;
        }
        break;
    }

    pDC->DrawState(iconRect.TopLeft(), iconRect.Size(), m_hIconIn, DSS_NORMAL,(CBrush*)NULL);
}

时间: 2024-08-28 13:37:59

Button控件自绘的相关文章

MFC Button控件自绘

文章参考地址:  http://blog.csdn.net/yue7603835/article/details/6649458    VC下的界面着实难看 有时候我们不得不自己进行控件的绘制 以前 一直不理解最近再次看了学了一遍终于明白了一点   与大家分享下...       需要源代码的Q我 寻找一起学VC的朋友    比如说   我们要改变一个编辑框的背景 我们响应WM_CTLCOLOR函数 进行OnCtlColor进行修改但是对与 Button控件就不行了 ..   这时候我们要进行自

深入Windows窗体原理及控件重绘技巧

之前有学MFC的同学告诉我觉得Windows的控件重绘难以理解,就算重绘成功了还是有些地方不明白,我觉得可能很多人都有这样的问题,在这里我从Windows窗体的最基本原理来讲解,如果你有类似的疑惑希望这篇文章可以帮你解惑. 1.Windows窗体原理 首先,如果看过Win32 SDK编程的都知道Windows的三大核心系统:负责窗口对象产生和消息分发的USER模块,负责图像显示绘制的GDI模块,负责内存.进程.IO管理的KERNEL模块.试想象一下如何在一个像素阵列上产生窗口对象,其实就是使用G

Button控件

从最简单的开始写起: 首先先从工具性中拖入一个Button控件,然后可以在其属性面板之中更改其自身的属性. 当然也可用直接在代码编辑界面进行直接的编辑添加,如果有什么属性不清楚,可在属性面板中查看一下,然后可以用代码直接编写,和在属性面板中更改的是一样的. 之后可以编辑控件的事件了,其中Button有Click事件和Command事件,在这里就只写Click事件. 代码: 1 <asp:Button ID="Button1" runat="server" Te

VB.NET 章鱼哥出品--入门基础Button控件的使用详解(一)

全网最全的Button控件详解!!!Button 按钮是VB.NET 中最基础,也是最常用的控件,不管你是初学者还是大牛.每个程序中必然少不了Button按钮.但是Button控件有很多用法很多大牛却不见得知道.用的最多的无非就是在点击事件中处理程序,今天我将使用2到3篇文章的篇幅来详细讲解Button按钮的用法.      '作者:章鱼哥,QQ:3107073263 群:309816713            '如有疑问或好的建议请联系我,大家一起进步     1,属性(以最常用的开始) (

纯代码创建Button控件:

纯代码创建Button控件: // 创建按钮 UIButton *btn = [[UIButton alloc] init]; // 添加按钮 [self.view addSubview:btn]; // 设置frame btn.frame = CGRectMake(50, 50, 100, 100); // 设置背景图片 // 通过文件名加载图片(凡是PNG图片,都不用加拓展名) UIImage *normal = [UIImage imageName:@“btn_01”]; // 设置普通状

listView中的button控件获取索引

1.在listitem中初始化button的时候,给该button添加一个setTag方法,将此时的索引值传进去,然后在button的onclick事件中调用view的getTag方法,即可将listitem的索引读出来,代码如下: tagButton.setTag(position); 此处的tagButton就是定义的button,Position是view里边的位置. 2.初始化button的时候通过setTag方法传入一个item的索引值 private OnClickListener 

listView中的button控件获取item的索引

在listview中的listitem设置事件响应,如果listitem中有button控件,这时候listitem就不会捕获到点击事件,而默认的是listitem中的button会捕获点击事件.那么如果点击listitem中的button怎么才能这个button是在哪一个item中呢,换句话说,就是点击listitem中的button怎么获取该listitem的索引?得到了这个索引的话,item里边的值就相对容易了. 通过如下方法可以实现: 1.在listitem中初始化button的时候,给

Android——button控件使用

a001.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent&

Windows Phone - 按钮/button 控件

System.Windows.Controls.Button   button控件一.button控件的各种样式的展示可以通过 …… 来给控件定义公共的样式调用样式的方法:在Button控件上添加样式的属性 Style="{StaticResource ButtonStyle1}" Margin属性定义了Button控件的相对整体界面的于左上右下的距离.Height控件的高度Width控件的宽度Content控件显示的内容Click单击触发的事件HorizontalAlignment水