Vc2017对话框背景透明及控件背景透明
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Vc2017对话框背景透明及控件背景透明
说明:
1、对话框上有背景图片,控件背景透明。
2、例子中有static、edit、button三种控件实现了背景透明。
3、解决了以下问题:
(1)编辑框内文字重叠现象。
(2)按钮无变化现象。
#include"stdafx.h"
#include"afxdialogex.h"
#include"resource.h"
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
CMyAccessDlg::CMyAccessDlg(CWnd* pParent/*=nullptr*/)
: CDialogEx(IDD_MYACCESS_DIALOG, pParent)
, m_strBj(_T(""))
{
}
CMyAccessDlg::~CMyAccessDlg()
{
}
void CMyAccessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_strBj);
}
BEGIN_MESSAGE_MAP(CMyAccessDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SHOW, &CMyAccessDlg::OnBnClickedButtonShow)
ON_BN_CLICKED(IDC_BUTTON_NEXT, &CMyAccessDlg::OnBnClickedButtonNext)
ON_WM_CTLCOLOR()
ON_WM_ERASEBKGND()
ON_EN_CHANGE(IDC_EDIT1, &CMyAccessDlg::OnChangeEdit1)
ON_EN_CHANGE(IDC_EDIT_SHUNXU, &CMyAccessDlg::OnChangeEditShunxu)
END_MESSAGE_MAP()
// CMyAccessDlg 消息处理程序
BOOL CMyAccessDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。
当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
//这两句可实现对话框透明但控件不透明(需要注释掉背景图片部分)
//SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_LAYERED);
//SetLayeredWindowAttributes(RGB(1,2,3), 1, LWA_COLORKEY);
return TRUE;
}
void CMyAccessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
void CMyAccessDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
//CDialogEx::OnPaint();
//---------添加背景图片,自己加入的,要将上一句注释掉
CPaintDC dc(this);
CRect rect;
GetClientRect(&rect);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);
BITMAP bitmap;
bmpBackground.GetBitmap(&bitmap);
CBitmap *pbmpOld = dcMem.SelectObject(&bmpBackground);
dc.StretchBlt(0, 0, rect.Width(), rect.Height(), &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);
dcMem.SelectObject(pbmpOld);
dcMem.DeleteDC();
bmpBackground.DeleteObject();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CMyAccessDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CMyAccessDlg::OnBnClickedButtonShow()
{
// TODO: 在此添加控件通知处理程序代码
CString str = L”adaafasfaf”;
int i = 5;
SetDlgItemInt(IDC_EDIT_SHUNXU, i);
//-------刷新编辑框,否则文字会重叠
CRect rect1, rect2;
CWnd * peditWnd1 = GetDlgItem(IDC_EDIT1);
peditWnd1->GetClientRect(&rect1);
peditWnd1->ClientToScreen(&rect1);
ScreenToClient(&rect1);
InvalidateRect(&rect1);
CWnd * peditWnd2 = GetDlgItem(IDC_EDIT_SHUNXU);
peditWnd2->GetClientRect(&rect2);
peditWnd2->ClientToScreen(&rect2);
ScreenToClient(&rect2);
InvalidateRect(&rect2);
}
void CMyAccessDlg::OnBnClickedButtonNext()
{
// TODO: 在此添加控件通知处理程序代码
}
HBRUSH CMyAccessDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: 在此更改 DC 的任何特性
CBrush *brush = new CBrush(RGB(1, 2, 3));
switch (nCtlColor)
{
case CTLCOLOR_STATIC: //对所有静态文本控件的设置
pDC->SetBkMode(TRANSPARENT);//设置文字背景透明
pDC->SetTextColor(RGB(0, 255, 0)); //设置字体颜色
hbr = (HBRUSH)GetStockObject(NULL_BRUSH); // 设置控件背景透明
break;
case CTLCOLOR_EDIT: //对所有编辑框控件的设置
{
int cx,cy;
pDC->SetBkMode(TRANSPARENT);//设置文字背景透明
pDC->SetTextColor(RGB(255, 0, 0)); //设置字体颜色
CRect rect,rect2,rect3;
pWnd->GetClientRect(&rect);
pWnd->GetWindowRect(&rect2);
ScreenToClient(&rect2);
pWnd->GetUpdateRect(&rect3);
if(rect3.Width()==0)
{
cx = (rect2.Width() - rect.Width()) / 2;
cy = (rect2.Height() - rect.Height()) / 2;
CDC *dc = GetDC();
pDC->BitBlt(0, 0, rect2.Width(), rect2.Height(), dc, rect2.left + cx, rect2.top + cy, SRCCOPY);
ReleaseDC(dc);
}
return (HBRUSH)GetStockObject(NULL_BRUSH);
//hbr = (HBRUSH)brush->m_hObject;
break;
}
case CTLCOLOR_DLG:
hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
//hbr = (HBRUSH)brush->m_hObject;
break;
case CTLCOLOR_BTN: //按钮的owndraw属性必须设置
{
if (pWnd->GetDlgCtrlID() == IDC_BUTTON_SHOW)
{
CPen pen(PS_SOLID, 1, RGB(0, 255, 0));
CPen *oldpen = pDC->SelectObject(&pen);
CBrush *brush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *oldbrush = pDC->SelectObject(brush);
CRect rect;
pWnd->GetClientRect(&rect);
pDC->Rectangle(&rect);
pDC->SelectObject(oldpen);
pDC->SelectObject(oldbrush);
pDC->SetBkMode(TRANSPARENT);//设置文字背景透明
pDC->SetTextColor(RGB(255, 0, 0)); //设置字体颜色
CString str = L"确定";
pDC->DrawText(str, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
hbr = (HBRUSH)GetStockObject(NULL_BRUSH);
//hbr = (HBRUSH)brush->m_hObject;
break;
}
}
}
return hbr;
}
BOOL CMyAccessDlg::OnEraseBkgnd(CDC* pDC)
{
return CDialogEx::OnEraseBkgnd(pDC);
}
void CMyAccessDlg::OnChangeEdit1()
{
CRect rect;
CWnd * peditWnd = GetDlgItem(IDC_EDIT1);
peditWnd->GetClientRect(&rect);
peditWnd->ClientToScreen(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect);
}
void CMyAccessDlg::OnChangeEditShunxu()
{
CRect rect;
CWnd * peditWnd = GetDlgItem(IDC_EDIT_SHUNXU);
peditWnd->GetClientRect(&rect);
peditWnd->ClientToScreen(&rect);
ScreenToClient(&rect);
InvalidateRect(&rect);
}
效果图如下:。