button修改大小的同时修改他的颜色问题

wzw200 2009-07-02 11:24:26
我现在的程序要达到这种要求 哎 真不知道为什么

void CAbcdDlg::OnButton3()
{
//1>
CWnd *kWnd=GetDlgItem(IDC_BUTTON2);//得到预示窗口指针
kWnd->MoveWindow(10,10,30,30);
//2>
CWnd *Wnd=GetDlgItem(IDC_BUTTON2);//得到预示窗口指针
CClientDC dc(this);
HBRUSH hBrush=CreateSolidBrush(RGB(16,0,16));
CBrush brush(RGB(16,0,16));
CRect rect;
::GetWindowRect(Wnd->m_hWnd, &rect );
ScreenToClient(rect);
dc.FillRect(&rect,&brush);

}

点BT3 bt2大小变了 颜色没变,再点 bt3 ,则bt2颜色变了 ,不知道为什么
1> 2>放在两个函数中同时调用也不行,那位看看,能不能移动后,修改他的颜色呢?
...全文
235 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzw200 2009-07-06
  • 打赏
  • 举报
回复
谢谢各位了 我自己搞了了
写一个自己简单的控件


#ifndef _BTNST_H
#define _BTNST_H
#include <AFXWIN.h>

class CMyBt : public CButton
{
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
#endif
/////////////////////////////
#include "MyBt.h"

void CMyBt::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);;

CBrush brush(RGB(0,0,0));
pDC->FillRect(&(lpDrawItemStruct->rcItem),&brush);
}

结了
wzw200 2009-07-06
  • 打赏
  • 举报
回复
我调试了 他又执行了系统的函数 ,颜色又变回来了 哎有什么好办法不让他执行系统函数吗 ------------------

那位能帮我看看吗
wzw200 2009-07-02
  • 打赏
  • 举报
回复
WM_CTLCOLOR中可以 不过在 这个函数是个好像是个while(1) 不太好 就没用
skyxie 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 wzw200 的帖子:]
我现在的程序要达到这种要求 哎 真不知道为什么

void CAbcdDlg::OnButton3()
{
//1>
CWnd *kWnd=GetDlgItem(IDC_BUTTON2);//得到预示窗口指针
kWnd->MoveWindow(10,10,30,30);
//2>
CWnd *Wnd=GetDlgItem(IDC_BUTTON2);//得到预示窗口指针
CClientDC dc(this);
HBRUSH hBrush=CreateSolidBrush(RGB(16,0,16));
CBrush brush(RGB(16,0,16));
CRect rect;
::GetWindowRect(Wnd->m_hWnd, &rect );
Scr…
[/Quote]

处理WM_CTLCOLOR , 在 OnCtlColor 根据不同的情况返回不同的hbrush
wzw200 2009-07-02
  • 打赏
  • 举报
回复
好的 楼上 我试试 太谢谢了
tuzi0203 2009-07-02
  • 打赏
  • 举报
回复
//.cpp
// UniButton.cpp : implementation file
//

#include "stdafx.h"
#include "MySphereButton.h"
#include "UniButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUniButton

CUniButton::CUniButton()
{
m_nColor = GetSysColor(COLOR_BTNFACE);
m_sColor = m_nColor;
m_hColor = m_nColor;
m_dColor = m_nColor;
m_nBorder = 1;
m_lfEscapement = 0;
m_pNormal = NULL;
m_pSelected = NULL;
m_pHover = NULL;
m_pDisabled = NULL;
m_hRgn = 0;
m_bHover = false;
m_bCapture = false;
m_bMouseDown = false;
m_bNeedBitmaps = true;
}

CUniButton::~CUniButton()
{
delete m_pNormal;
delete m_pSelected;
delete m_pHover;
delete m_pDisabled;
DeleteObject(m_hRgn);
}

BEGIN_MESSAGE_MAP(CUniButton, CButton)
//{{AFX_MSG_MAP(CUniButton)
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUniButton message handlers

BOOL CUniButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID)
{
// store region in member variable
DeleteObject(m_hRgn);
m_hRgn = CreateRectRgn(0, 0, 31, 31);
CRect box(0, 0, 0, 0);
if (m_hRgn != 0)
CombineRgn(m_hRgn, hRgn, 0, RGN_COPY);

// make sure that region bounding rect is located in (0, 0)
GetRgnBox(m_hRgn, &box);
OffsetRgn(m_hRgn, -box.left, -box.top);
GetRgnBox(m_hRgn, &box);

// update position of region center for caption output
m_CenterPoint = CPoint(box.left + box.Width() /2 , box.top + box.Height() /2);
box.OffsetRect(point);

return CButton::Create(lpszCaption, dwStyle, box, pParentWnd, nID);
}

BOOL CUniButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, COLORREF color)
{
m_sColor = color;
m_hColor = color;

// call another constructor
return Create(lpszCaption, dwStyle, point, hRgn, pParentWnd, nID);
}

BOOL CUniButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, UINT nBorder, LONG lfEscapement, COLORREF nColor, COLORREF sColor, COLORREF hColor, COLORREF dColor)
{
m_lfEscapement = lfEscapement;
return Create(lpszCaption, dwStyle, point, hRgn, pParentWnd, nID, nBorder, nColor, sColor, hColor, dColor);
}

BOOL CUniButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, UINT nBorder, COLORREF nColor, COLORREF sColor, COLORREF hColor, COLORREF dColor)
{
// change default colors
m_nBorder = nBorder;
m_nColor = nColor;
m_sColor = sColor;
m_hColor = hColor;
m_dColor = dColor;

// call another constructor
return Create(lpszCaption, dwStyle, point, hRgn, pParentWnd, nID);
}

void CUniButton::PreSubclassWindow()
{
// change window style to allow owner draw
ModifyStyle(0, BS_OWNERDRAW | BS_PUSHBUTTON);
CButton::PreSubclassWindow();
}

int CUniButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CButton::OnCreate(lpCreateStruct) == -1)
return -1;

// assign new region to a window
m_bNeedBitmaps = true;
SetWindowRgn(m_hRgn, true);

return 0;
}

void CUniButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// record that mouse is down
m_bMouseDown = true;
if (!m_bCapture) {
SetCapture();
m_bCapture = true;
}
CButton::OnLButtonDown(nFlags, point);
}

void CUniButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// record that mouse is released
CButton::OnLButtonUp(nFlags, point);

m_bMouseDown = false;
if (m_bCapture) {
ReleaseCapture();
m_bCapture = false;
}
CheckHover(point);
}

void CUniButton::OnMouseMove(UINT nFlags, CPoint point)
{
// Test if mouse is above the button.
if (!m_bMouseDown)
CheckHover(point);

CButton::OnMouseMove(nFlags, point);
}

void CUniButton::CheckHover(CPoint point)
{
if (HitTest(point)) {
if (!m_bCapture) {
SetCapture();
m_bCapture = true;
}
if (!m_bHover) {
m_bHover = true;
RedrawWindow();
}
}
else {
if (m_bCapture) {
ReleaseCapture();
m_bCapture = false;
}
m_bHover = false;
RedrawWindow();
}
}

LRESULT CUniButton::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// I have noticed that default windows buttons can be clicked very quickly.
// Double or single click both result in a button being pushed down.
// For owner drawn buttons this is not the case. Double click does
// not push button down. Here is a solution for the problem:
// double click message is substituted for single click.

if (message == WM_LBUTTONDBLCLK)
message = WM_LBUTTONDOWN;

return CButton::DefWindowProc(message, wParam, lParam);
}

BOOL CUniButton::HitTest(CPoint point)
{
BOOL result = false;

// Obtain handle to window region.
HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
GetWindowRgn(hRgn);
CRect rgnRect;
GetRgnBox(hRgn, &rgnRect);

// First check if point is in region bounding rect.
// Then check if point is in the region in adition to being in the bouding rect.
result = PtInRect(&rgnRect, point) && PtInRegion(hRgn, point.x, point.y);

// Clean up and exit.
DeleteObject(hRgn);
return result;
}

BOOL CUniButton::OnEraseBkgnd(CDC* pDC)
{
// do not erase background
return 1;
}

//////////////////////// DRAWING ROUTINES ////////////////////////////

void CUniButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// prepare DC
CDC* pDC = CDC::FromHandle(lpDrawItemStruct -> hDC);
CRect rect;
GetClientRect(rect);

// prepare bitmaps they need to be prepared
if (m_bNeedBitmaps)
PrepareStateBitmaps(pDC, &rect);

// draw button to the screen
DrawButton(pDC, &rect, lpDrawItemStruct -> itemState);
}

void CUniButton::PrepareStateBitmaps(CDC * pDC, CRect * pRect)
{
// prepare memory DC
CDC * pMemDC;
pMemDC = new CDC;
pMemDC -> CreateCompatibleDC(pDC);

// prepare bitmaps for all button states and for the mask
PrepareNormalState(pDC, pMemDC, pRect);
PrepareSelectedState(pDC, pMemDC, pRect);
PrepareHoverState(pDC, pMemDC, pRect);
PrepareDisabledState(pDC, pMemDC, pRect);

// clean up
delete pMemDC;
m_bNeedBitmaps = false;
}


void CUniButton::PrepareNormalState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
// prepare MYBS_NORMAL state bitmap
delete m_pNormal;
m_pNormal = new CBitmap;
PaintRgn(pDC, pMemDC, m_pNormal, m_nColor, pRect, true, false);
}

tuzi0203 2009-07-02
  • 打赏
  • 举报
回复
//.h
#if !defined(AFX_UNIBUTTON_H__EBEB2EA0_A067_11D2_9E44_0020182F735A__INCLUDED_)
#define AFX_UNIBUTTON_H__EBEB2EA0_A067_11D2_9E44_0020182F735A__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
/////////////////////////////////////////////////////////////////////////////
// CUniButton window
//
// AUTHOR: Pavel A .Simakov
// DATE: 981226
// E-MAIL: psimakov@outplay.com
// ORGANIZATION: Outplay Consulting
// HTTP: www.outplay.com
// TERM OF USE: unlimited, must cite AUTHOR
// COPYRIHT: 1998 Outplay Consulting
//
/////////////////////////////////////////////////////////////////////////////

class CUniButton : public CButton
{
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUniButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual void PreSubclassWindow();
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL

protected:
//{{AFX_MSG(CUniButton)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
private:
UINT m_nBorder; // width of the border in pixels for 3D highlight
LONG m_lfEscapement; // orientation of the caption (in tenth of the degree as in LOGFONT)
COLORREF m_nColor, m_sColor, m_hColor, m_dColor; // background colors for button states: normal, selected, hover, disabled
CBitmap * m_pNormal; // bitmaps to hold button images
CBitmap * m_pSelected;
CBitmap * m_pHover;
CBitmap * m_pDisabled;
CPoint m_CenterPoint; // button caption will be centered around this point
BOOL m_bMouseDown; // indicated that mouse is pressed down
BOOL m_bHover; // indicates if mouse is over the button
BOOL m_bCapture; // indicates that mouse is captured in the buton
HRGN m_hRgn; // region in screen coordinates
BOOL m_bNeedBitmaps; // flag idicates that state bitmaps must be rebuild
void DrawButton(CDC * pDC, CRect * pRect, UINT state); // draws button to the screen
void PrepareStateBitmaps(CDC * pDC, CRect * pRect); // prepares bitmaps for button states
BOOL HitTest(CPoint point); // determines if point is inside the button region
void RgnPixelWork(CDC * pDC, CRgn * pRgn); // region pixel work - unused
void FrameRgn3D(HDC hDC, const HRGN hRgn, BOOL bSunken); // frames region to show 3D shadows
void CheckHover(CPoint point);
protected:
void PrepareNormalState(CDC * pDC, CDC * pMemDC, CRect * pRect); // prepare normal state button bitmap
void PrepareSelectedState(CDC * pDC, CDC * pMemDC, CRect * pRect); // prepare selectedstate button bitmap
void PrepareHoverState(CDC * pDC, CDC * pMemDC, CRect * pRect); // prepare hover state button bitmap
void PrepareDisabledState(CDC * pDC, CDC * pMemDC, CRect * pRect); // prepare disabled state button bitmap
void DrawButtonCaption(HDC hDC, CRect * pRect, BOOL bEnabled, BOOL bSunken); // draws button caption
void PaintRgn(CDC * pDC, CDC * pMemDC, CBitmap * pBitmap, COLORREF color, CRect * pRect, BOOL bEnabled, BOOL bSunken); // paint button
public:
CUniButton(); // constructor
virtual ~CUniButton(); // destructor
BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID);
// constructor with default colors and border
BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, COLORREF color);
// constructor with hover & selected color specified
BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, UINT nBorder, COLORREF nColor, COLORREF sColor, COLORREF hColor, COLORREF dColor);
// complex parameters constructor
// lpszCaption - window caption
// dwStyle - window styles
// point - position of the button on the parent window (in parent coordinates)
// hRgn - handle to region which represents the button (in button client coordinates)
// pParentWnd - handle to parent window
// nID - control Id
// nBorder - specifies width of the border in pixels for 3D highlight (allowed values are 1, 2)
// nColor - normal color
// sColor - selected color
// hColor - hover color
// dColor - disabled color
BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, UINT nBorder, LONG lfEscapement, COLORREF nColor, COLORREF sColor, COLORREF hColor, COLORREF dColor);
// variation of above
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_UNIBUTTON_H__EBEB2EA0_A067_11D2_9E44_0020182F735A__INCLUDED_)
tuzi0203 2009-07-02
  • 打赏
  • 举报
回复
你去网上找一个UniButton类,那个能改变大小和颜色,我把代码贴给你吧
wzw200 2009-07-02
  • 打赏
  • 举报
回复
放在OnPaint中 上面的程序好像不能用
softist 2009-07-02
  • 打赏
  • 举报
回复
oyljerry 2009-07-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 wzw200 的帖子:]
我现在的程序要达到这种要求 哎 真不知道为什么

void CAbcdDlg::OnButton3()
{
//1>
CWnd *kWnd=GetDlgItem(IDC_BUTTON2);//得到预示窗口指针
kWnd->MoveWindow(10,10,30,30);
//2>
CWnd *Wnd=GetDlgItem(IDC_BUTTON2);//得到预示窗口指针
CClientDC dc(this);
HBRUSH hBrush=CreateSolidBrush(RGB(16,0,16));
CBrush brush(RGB(16,0,16));
CRect rect;
::GetWindowRect(Wnd->m_hWnd, &rect );
ScreenToClient(re…
[/Quote]
修改button颜色,要放到OnPaint等中间,不然刷新就没了,
tuzi0203 2009-07-02
  • 打赏
  • 举报
回复
sf,嘿嘿
wu_qing_yun 2009-07-02
  • 打赏
  • 举报
回复
学习自绘的话,推荐用CButtonST类
wzw200 2009-07-02
  • 打赏
  • 举报
回复
楼上说的对 我调试了 他又执行了系统的函数 ,颜色又变回来了 哎有什么好办法不让他执行系统函数吗 ------------------
wzw200 2009-07-02
  • 打赏
  • 举报
回复
楼上说的对 我调试了 他又执行了系统的函数 ,颜色又变回来了 哎有什么好办法吗 ------------------

是的 9楼的可以 我用过了 不过在这个color函数中一直while(1) 有点那个,因为我要有这个BT做显示视频用 ,只不过临面把他变成黑色的

BST我也用了 ,不怎么好用,对于我的程序
Ghost90 2009-07-02
  • 打赏
  • 举报
回复
这样不行 你用2改变了颜色 再按BUTTON2的时候又变回原来的颜色了

要不用9楼的方法

或用CButtonST类

http://www.vckbase.com/document/viewdoc/?id=1414
wzw200 2009-07-02
  • 打赏
  • 举报
回复
有那位能回答我的问题吗
不有控件
wzw200 2009-07-02
  • 打赏
  • 举报
回复
哎 用这控件不怎么如意

我还是想想 移动 画黑吧
tuzi0203 2009-07-02
  • 打赏
  • 举报
回复
void CUniButton::PrepareSelectedState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
// prepare MYBS_SELECTED state bitmap
delete m_pSelected;
m_pSelected = new CBitmap;
PaintRgn(pDC, pMemDC, m_pSelected, m_sColor, pRect, true, true);
}

void CUniButton::PrepareHoverState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
// prepare MYBS_HOVER state bitmap
delete m_pHover;
m_pHover = new CBitmap;
PaintRgn(pDC, pMemDC, m_pHover, m_hColor, pRect, true, false);
}

void CUniButton::PrepareDisabledState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
// prepare MYBS_DISABLED state bitmap
delete m_pDisabled;
m_pDisabled = new CBitmap;
PaintRgn(pDC, pMemDC, m_pDisabled, m_dColor, pRect, false, false);
}

void CUniButton::PaintRgn(CDC * pDC, CDC * pMemDC, CBitmap * pBitmap, COLORREF color, CRect * pRect, BOOL bEnabled, BOOL bSunken)
{
// create bitmap
pBitmap -> CreateCompatibleBitmap(pDC, pRect -> Width(), pRect -> Height());
CBitmap * pOldBitmap = pMemDC -> SelectObject(pBitmap);

// prepare region
HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
GetWindowRgn(hRgn);

// fill rect a with transparent color and fill rgn
HBRUSH hBrush = CreateSolidBrush(color);
pMemDC -> FillSolidRect(pRect, RGB(0, 0, 0));
FillRgn(pMemDC -> GetSafeHdc(), hRgn, hBrush);
DeleteObject(hBrush);

// draw 3D border and text
DrawButtonCaption(pMemDC -> GetSafeHdc(), pRect, bEnabled, bSunken);
FrameRgn3D(pMemDC -> GetSafeHdc(), hRgn, bSunken);

// clean up
DeleteObject(hRgn);
pMemDC -> SelectObject(pOldBitmap);
}

void CUniButton::DrawButtonCaption(HDC hDC, CRect * pRect, BOOL bEnabled, BOOL bSunken)
{
// select parent font
int nOldMode = SetBkMode(hDC, TRANSPARENT);
CString text;
GetWindowText(text);
LOGFONT lf;
GetParent() -> GetFont() -> GetLogFont(&lf);
HFONT hFont = CreateFontIndirect(&lf);
HFONT hOldFont = (HFONT) SelectObject(hDC, hFont);

// determine point where to output text
TEXTMETRIC tm;
GetTextMetrics(hDC, &tm);
CPoint p = CPoint(m_CenterPoint.x, m_CenterPoint.y + tm.tmHeight/ 2);
if (bSunken)
p.Offset(m_nBorder, m_nBorder);

// draw button caption depending upon button state
if (bEnabled) {
SetTextColor(hDC, GetSysColor(COLOR_BTNTEXT));
SetTextAlign(hDC, TA_CENTER | TA_BOTTOM);
TextOut(hDC, p.x, p.y, text, text.GetLength());
}
else {
SetTextColor(hDC, GetSysColor(COLOR_3DHILIGHT));
TextOut(hDC, p.x + 1, p.y + 1, text, text.GetLength());
SetTextColor(hDC, GetSysColor(COLOR_3DSHADOW));
TextOut(hDC, p.x, p.y, text, text.GetLength());
}

SelectObject(hDC, hOldFont);
DeleteObject(hFont);
SetBkMode(hDC, nOldMode);
}

void CUniButton::FrameRgn3D(HDC hDC, const HRGN hRgn, BOOL bSunken)
{
// we need two differenr regions to keep base region and border region
HBRUSH hBrush;
HRGN hBaseRgn = CreateRectRgn(0, 0, 0, 0);

COLORREF ltOuter, ltInner, rbOuter, rbInner; // colors of inner and outer shadow for top-left and right-bottom corners

// decide on color scheme
if (!bSunken) {
ltOuter = GetSysColor(COLOR_3DLIGHT);
ltInner = GetSysColor(COLOR_3DHILIGHT);
rbOuter = GetSysColor(COLOR_3DDKSHADOW);
rbInner = GetSysColor(COLOR_3DSHADOW);
}
else {
rbInner = GetSysColor(COLOR_3DLIGHT);
rbOuter = GetSysColor(COLOR_3DHILIGHT);
ltInner = GetSysColor(COLOR_3DDKSHADOW);
ltOuter = GetSysColor(COLOR_3DSHADOW);
}

// offset highlight and shadow regions
// substract them from the base region
switch (m_nBorder)
{
case 2:
CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
OffsetRgn(hBaseRgn, 2, 2);
CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
hBrush = CreateSolidBrush(ltInner);
FillRgn(hDC, hBaseRgn, hBrush);
DeleteObject(hBrush);

CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
OffsetRgn(hBaseRgn, -2, -2);
CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
hBrush = CreateSolidBrush(rbInner);
FillRgn(hDC, hBaseRgn, hBrush);
DeleteObject(hBrush);

CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
OffsetRgn(hBaseRgn, 1, 1);
CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
hBrush = CreateSolidBrush(ltOuter);
FillRgn(hDC, hBaseRgn, hBrush);
DeleteObject(hBrush);

CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
OffsetRgn(hBaseRgn, -1, -1);
CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
hBrush = CreateSolidBrush(rbOuter);
FillRgn(hDC, hBaseRgn, hBrush);
DeleteObject(hBrush);
break;
default:
CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
OffsetRgn(hBaseRgn, 1, 1);
CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
hBrush = CreateSolidBrush(ltInner);
FillRgn(hDC, hBaseRgn, hBrush);
DeleteObject(hBrush);

CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
OffsetRgn(hBaseRgn, -1, -1);
CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
hBrush = CreateSolidBrush(rbOuter);
FillRgn(hDC, hBaseRgn, hBrush);
DeleteObject(hBrush);
break;
}

// clean up regions
DeleteObject(hBaseRgn);
}

void CUniButton::DrawButton(CDC * pDC, CRect * pRect, UINT state)
{
// create memory DC
CDC * pMemDC = new CDC;
pMemDC -> CreateCompatibleDC(pDC);
CBitmap * pOldBitmap;

// get region
HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
GetWindowRgn(hRgn);

// select bitmap to paint depending upon button state
if (state & ODS_DISABLED)
pOldBitmap = pMemDC -> SelectObject(m_pDisabled);
else {
if (state & ODS_SELECTED)
pOldBitmap = pMemDC -> SelectObject(m_pSelected);
else {
if (m_bHover)
pOldBitmap = pMemDC -> SelectObject(m_pHover);
else
pOldBitmap = pMemDC -> SelectObject(m_pNormal);
}
}

// paint using region for clipping
::SelectClipRgn(pDC -> GetSafeHdc(), hRgn);
pDC -> BitBlt(0, 0, pRect -> Width(), pRect -> Height(), pMemDC, 0, 0, SRCCOPY);
::SelectClipRgn(pDC -> GetSafeHdc(), NULL);

// clean up
DeleteObject(hRgn);
pMemDC -> SelectObject(pOldBitmap);
delete pMemDC;
}

void CUniButton::RgnPixelWork(CDC * pDC, CRgn * pRgn)
{
// get size of data composing region
int size = pRgn -> GetRegionData(NULL, 0);
HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size);
RGNDATA * pData = (RGNDATA *)GlobalLock(hData);

// retrieve region data
int res = pRgn -> GetRegionData(pData, size);
RECT * pRect = (RECT *) pData -> Buffer;

// now we know how region is represented and we are able to manipulate it as we like
for (DWORD i = 0; i < pData -> rdh.nCount; i++) {
RECT rect = *(pRect + i);
for (int x = rect.left; x < rect.right; x++)
for (int y = rect.top; y < rect.bottom; y++) {
// use SetPixel(x, y, color) to do pixel work
}
}

// free region data
GlobalUnlock(hData);
GlobalFree(hData);
}

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧