通过点击CStatic控件响应;具体VC工程,可以到我的csdn资源下载;
这个类初次编写,本来想找一些开源的,还不如自己实现的好,欢迎下载,扩充使用:
//h文件:
#if !defined(AFX_STATICBUTTON_H__8F2F5737_C55D_4E59_808B_6F72B8949265__INCLUDED_)
#define AFX_STATICBUTTON_H__8F2F5737_C55D_4E59_808B_6F72B8949265__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StaticButton.h : header file
//
/* YAO;
Version£º1.0.20120517;
*/
/////////////////////////////////////////////////////////////////////////////
// CStaticButton window
class CStaticButton : public CStatic
{
// Construction
public:
CStaticButton();
// Attributes
public:
// Operations
public:
public:
CString m_strtext;
VOID DrawString(HDC hdc,WCHAR * str);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStaticButton)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CStaticButton();
// Generated message map functions
protected:
//{{AFX_MSG(CStaticButton)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//}}AFX_MSG
afx_msg void OnMouseLeave(WPARAM wParam = 0, LPARAM lParam = 0);
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STATICBUTTON_H__8F2F5737_C55D_4E59_808B_6F72B8949265__INCLUDED_)
//cpp文件:
// StaticButton.cpp : implementation file
//
#include "stdafx.h"
#include "StaticButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <AtlBase.h>
#include <atlconv.h>
/////////////////////////////////////////////////////////////////////////////
// CStaticButton
CStaticButton::CStaticButton()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
}
CStaticButton::~CStaticButton()
{
}
BEGIN_MESSAGE_MAP(CStaticButton, CStatic)
//{{AFX_MSG_MAP(CStaticButton)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStaticButton message handlers
VOID CStaticButton::DrawString(HDC hdc,WCHAR * str)
{
Graphics graphics(hdc); graphics.Clear(Color::White);
// Create a string.
WCHAR string[256]={0};
wcscpy(string, /*L"Sample Text"*/str);
// Initialize arguments.
Font myFont(L"Arial", 10);
PointF origin(0.0f, 0.0f);
SolidBrush blackBrush(Color(255, 255, 0, 0));
// Draw string.
graphics.DrawString(
string,
wcslen(string),
&myFont,
origin,
&blackBrush);
}
void CStaticButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
GetParent()->GetWindowText(str); //这样就可以发消息到上一层窗口或指定窗口了;
MessageBox(CString("»ñÈ¡ÉÏÒ»²ã´°¿Útitle")+" "+str);
CStatic::OnLButtonDown(nFlags, point);
}
void CStaticButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
GetWindowText(m_strtext);
#ifdef UNICODE
DrawString(CClientDC(this), m_strtext );
#else
USES_CONVERSION;
DrawString(CClientDC(this),A2W(m_strtext.GetBuffer(0))); m_strtext.ReleaseBuffer();
#endif
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.hwndTrack = this->m_hWnd;
tme.dwFlags = TME_LEAVE;
_TrackMouseEvent(&tme);
}
CStatic::OnMouseMove(nFlags, point);
}
void CStaticButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
SetWindowText( m_strtext ); //MessageBox("");
}
// StaticButton.cpp : implementation file
//
#include "stdafx.h"
#include "StaticButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <AtlBase.h>
#include <atlconv.h>
/////////////////////////////////////////////////////////////////////////////
// CStaticButton
CStaticButton::CStaticButton()
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
}
CStaticButton::~CStaticButton()
{
}
BEGIN_MESSAGE_MAP(CStaticButton, CStatic)
//{{AFX_MSG_MAP(CStaticButton)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStaticButton message handlers
VOID CStaticButton::DrawString(HDC hdc,WCHAR * str)
{
Graphics graphics(hdc); graphics.Clear(Color::White);
// Create a string.
WCHAR string[256]={0};
wcscpy(string, /*L"Sample Text"*/str);
// Initialize arguments.
Font myFont(L"Arial", 10);
PointF origin(0.0f, 0.0f);
SolidBrush blackBrush(Color(255, 255, 0, 0));
// Draw string.
graphics.DrawString(
string,
wcslen(string),
&myFont,
origin,
&blackBrush);
}
void CStaticButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CString str;
GetParent()->GetWindowText(str); //ÕâÑù¾Í¿ÉÒÔ·¢ÏûÏ¢µ½ÉÏÒ»²ã´°¿Ú»òÖ¸¶¨´°¿ÚÁË£»
MessageBox(CString("»ñÈ¡ÉÏÒ»²ã´°¿Útitle")+" "+str);
CStatic::OnLButtonDown(nFlags, point);
}
void CStaticButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
GetWindowText(m_strtext);
#ifdef UNICODE
DrawString(CClientDC(this), m_strtext );
#else
USES_CONVERSION;
DrawString(CClientDC(this),A2W(m_strtext.GetBuffer(0))); m_strtext.ReleaseBuffer();
#endif
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.hwndTrack = this->m_hWnd;
tme.dwFlags = TME_LEAVE;
_TrackMouseEvent(&tme);
}
CStatic::OnMouseMove(nFlags, point);
}
void CStaticButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
SetWindowText( m_strtext ); //MessageBox("");
}