VC++ 实现整个对话框透明的效果SetLayeredWindowAttributes

SetLayeredWindowAttributes函数需要WINDOWS2000以上的系统才能支持,包含在新的SDK中。

 

使用方法:在对话框的 OnInitDialog ()加入:

SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL"); 
if(hInst) 
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD); 
MYFUNC fun = NULL;
//取得SetLayeredWindowAttribut es函数指针 
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttribut es");
if(fun)fun(this->GetSafeHwnd(),0,128,2); //128为半透明,0为完全透明
FreeLibrary(hInst); 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 MFC 对话框圆角示例代码: 首先,在对话框类的头文件中添加以下代码: ```cpp class CCustomDialog : public CDialogEx { // ... protected: afx_msg void OnPaint(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); DECLARE_MESSAGE_MAP() private: int m_nRoundRadius; }; ``` 然后,在对话框类的实现文件中添加以下代码: ```cpp BEGIN_MESSAGE_MAP(CCustomDialog, CDialogEx) // ... ON_WM_PAINT() ON_WM_ERASEBKGND() END_MESSAGE_MAP() CCustomDialog::CCustomDialog(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_CUSTOM_DIALOG, pParent) { m_nRoundRadius = 20; // 设置圆角半径为 20 } void CCustomDialog::OnPaint() { CPaintDC dc(this); CRgn rgn; CRect rect; GetClientRect(&rect); rgn.CreateRoundRectRgn(rect.left, rect.top, rect.right, rect.bottom, m_nRoundRadius, m_nRoundRadius); dc.SelectClipRgn(&rgn); dc.FillSolidRect(rect, GetSysColor(COLOR_BTNFACE)); CDialogEx::OnPaint(); } BOOL CCustomDialog::OnEraseBkgnd(CDC* pDC) { return TRUE; } ``` 这里重写了 OnPaint() 和 OnEraseBkgnd() 方法。OnPaint() 方法用于绘制圆角矩形背景,并在最后调用父类的 OnPaint() 方法以绘制对话框的内容。OnEraseBkgnd() 方法返回 TRUE,以避免重绘背景。 最后,在 OnInitDialog() 方法中添加以下代码: ```cpp BOOL CCustomDialog::OnInitDialog() { CDialogEx::OnInitDialog(); // 设置对话框的边框为对话框圆角 ModifyStyleEx(0, WS_EX_LAYERED); SetLayeredWindowAttributes(0, 255, LWA_ALPHA); SetWindowPos(nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); return TRUE; } ``` 这里使用了 SetLayeredWindowAttributes() 方法将对话框透明度设置为 255,以使圆角部分可见。最后,使用 SetWindowPos() 方法更新对话框的大小和位置,以使新的边框效果生效。 这样,一个简单的 MFC 对话框圆角效果实现了。需要注意的是,这种方法只能在 Windows Vista 或更高版本上使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值