我正在尝试找出如何淡出Windows桌面或使其变暗,然后正常显示桌面的矩形部分。这是用于屏幕区域捕获程序的。您可以在Jing中看到我想要的精确效果。通常也可以使网页背景褪色。任何提示/指针/ C++来源非常感谢。谷歌到目前为止还没有帮助。

谢谢,
内维尔

最佳答案

使用覆盖整个屏幕的分层窗口,但用颜色键值对其进行绘画,以使感兴趣的矩形区域(应取消深色的区域)完全用颜色键填充。然后,该区域将完全透明,并且不会像桌面的其余部分一样变暗。分层窗口的其余部分应设置为恒定的alpha值,该值通常是透明的并用深色填充。

这是一个完整的工作示例:

#include“stdafx.h”
#include“ScreenCapper.h”

#定义MAX_LOADSTRING 100

//全局变量:
HINSTANCE hInst; //当前实例
TCHAR szTitle [MAX_LOADSTRING]; //标题栏文字
TCHAR szWindowClass [MAX_LOADSTRING]; //主窗口类名称

//转发此代码模块中包含的函数的声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE,int);
LRESULT回调WndProc(HWND,UINT,WPARAM,LPARAM);
INT_PTR CALLBACK About(HWND,UINT,WPARAM,LPARAM);

const COLORREF transparentColor = RGB(255,0,0); //纯红色是颜色键,或完全透明的颜色
const BYTE totalTranparencyAmount = 90; // 255个中
int DesktopWidth,DesktopHeight;

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

DesktopWidth = GetSystemMetrics(SM_CXSCREEN);
DesktopHeight = GetSystemMetrics(SM_CYSCREEN);

味精味精;
HACCEL hAccelTable;

LoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);
LoadString(hInstance,IDC_SCREENCAPPER,szWindowClass,MAX_LOADSTRING);
MyRegisterClass(hInstance);

如果(!InitInstance(hInstance,nCmdShow))
{
返回FALSE;
}

hAccelTable = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDC_SCREENCAPPER));

while(GetMessage(&msg,NULL,0,0))
{
如果(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

返回(int)msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

memset(&wcex,0,sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_SCREENCAPPER));
wcex.hCursor = LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground =(HBRUSH)(COLOR_WINDOW + 1);
//wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SCREENCAPPER);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));

返回RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
{
hInst = hInstance;
HWND hWnd = CreateWindowEx(WS_EX_LAYERED,szWindowClass,szTitle,WS_POPUP,0、0,DesktopWidth,DesktopHeight,NULL,NULL,hInstance,NULL);
如果(!hWnd)
返回FALSE;
SetLayeredWindowAttributes(hWnd,transparentColor,32,LWA_COLORKEY | LWA_ALPHA);

ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

返回TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT消息,WPARAM wParam,LPARAM lParam)
{
int wmId,wmEvent;
PAINTSTRUCT ps;
HDC hdc;

如果(消息== WM_COMMAND)
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
//解析菜单选择:
开关(wmId)
{
案例IDM_EXIT:
DestroyWindow(hWnd);
打破;
默认:
返回DefWindowProc(hWnd,message,wParam,lParam);
}
}
否则,如果(消息== WM_PAINT)
{
hdc = BeginPaint(hWnd,&ps);

HBRUSH hDarkBackgroundBrush = CreateSolidBrush(RGB(0,0,0));
HBRUSH hRegionOfInterestBrush = CreateSolidBrush(transparentColor);

RECT屏幕
screenRect.left = screenRect.top = 0;
screenRect.right = DesktopWidth;
screenRect.bottom = DesktopHeight;

RECT interestRect;
interestRect.left = interestRect.top = 300;
interestRect.right = interestRect.bottom = 600;
FillRect(hdc,&screenRect,hDarkBackgroundBrush);
FillRect(hdc,&interestRect,hRegionOfInterestBrush);

DeleteObject(hDarkBackgroundBrush);
DeleteObject(hRegionOfInterestBrush);
EndPaint(hWnd,&ps);
}
否则,如果(消息== WM_DESTROY)
{
PostQuitMessage(0);
}
其他
返回DefWindowProc(hWnd,message,wParam,lParam);
返回0;
}

关于c++ - 使用C++淡入Windows桌面,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1591131/

10-11 19:03