问题:png载入后,边角上的渐变效果不对,不是渐变到桌面背景,而是渐变到程序对话框默认背景,可是我在initialDlg中已经设置对话框背景是透明的,应该怎么写呢?
按照网上的方法,下载了GDI+的库和头文件,在stdafx.h中初始化(不用看的,就是贴上来以防万一)
#include <comdef.h>//初始化一下com
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#endif
#pragma comment(lib,"gdiplus.lib")
然后在rTz.h中加入了
public:
ULONG_PTR m_gdiplusToken;
GdiplusStartupInput m_gdiplusStartupInput;
再在rTz.cpp中的BOOL CRTzApp::InitInstance()加入
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
随后在rTzDlg.cpp中的BOOL CRTzDlg::OnInitDialog()加入
//调整初始化窗口位置
ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);
MoveWindow(GetSystemMetrics(SM_CXSCREEN) - 262, 30, 253, 605, FALSE);
//将对话框的背景色变成透明
CClientDC dc(this);
COLORREF maskColor = RGB(236,233,216); //掩码颜色,即背景色
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 AlphaFunc = NULL;
AlphaFunc = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(AlphaFunc)AlphaFunc(this->GetSafeHwnd(), maskColor, 255, 1);
FreeLibrary(hInst);
}
最后在void CRTzDlg::OnPaint()中加入
Graphics graphics(GetDC()->m_hDC);
Image image(L"Frame.png", TRUE);
graphics.DrawImage(&image, 0, 0, 253, 605);这是p好的背景png图,边上是渐变透明的:这是程序运行截图,可以很清楚的看出问题:
按照网上的方法,下载了GDI+的库和头文件,在stdafx.h中初始化(不用看的,就是贴上来以防万一)
#include <comdef.h>//初始化一下com
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#endif
#pragma comment(lib,"gdiplus.lib")
然后在rTz.h中加入了
public:
ULONG_PTR m_gdiplusToken;
GdiplusStartupInput m_gdiplusStartupInput;
再在rTz.cpp中的BOOL CRTzApp::InitInstance()加入
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
随后在rTzDlg.cpp中的BOOL CRTzDlg::OnInitDialog()加入
//调整初始化窗口位置
ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW);
MoveWindow(GetSystemMetrics(SM_CXSCREEN) - 262, 30, 253, 605, FALSE);
//将对话框的背景色变成透明
CClientDC dc(this);
COLORREF maskColor = RGB(236,233,216); //掩码颜色,即背景色
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 AlphaFunc = NULL;
AlphaFunc = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(AlphaFunc)AlphaFunc(this->GetSafeHwnd(), maskColor, 255, 1);
FreeLibrary(hInst);
}
最后在void CRTzDlg::OnPaint()中加入
Graphics graphics(GetDC()->m_hDC);
Image image(L"Frame.png", TRUE);
graphics.DrawImage(&image, 0, 0, 253, 605);这是p好的背景png图,边上是渐变透明的:这是程序运行截图,可以很清楚的看出问题:
UpdateLayeredWindow
但是分层窗口是不能显示控件的,所以需要做一个联动窗口上做控件,或者DirectUI全部控件自己画。
如果不是特别需求,建议不要弄这种费力不讨好滴事情。
如果时间多,可以学学DirectUI相关知识。
m_Blend.BlendFlags=0; //nothingelseisspecial...
m_Blend.AlphaFormat=1; //...
m_Blend.SourceConstantAlpha=255;//AC_SRC_ALPHA // 透明度结贴了