标记windows桌面任意区域

1、整体思想,创建一个透明的顶层窗口,标记区域后,展现在屏幕上;

2、区域由rect控制,标记线的类型、颜色、粗细可以在CreatePen时进行控制效果如下:

标记windows桌面任意区域_第1张图片

标记windows桌面任意区域_第2张图片

 

注意:除了register class除外,其余的操作要在相同线程进行,跨线程操作会有无权限等问题。


#include 
#include 

using namespace std;

BLENDFUNCTION blendFunction;
RECT wndRect;
SIZE wndSize;
HDC hdc;
HDC memDC;
HDC screenDC;
HBITMAP memBitmap;
POINT ptSrc;
HBITMAP hOldBmp;
HPEN hPen;
HPEN hOldPen;
RECT rect = { 0, 0, 200, 100 };
HWND hwnd;
HWND hwnd1;
char szClassName[] = "DrawWindowsTest";

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    return DefWindowProc(hwnd, message, wParam, lParam);

}

int DrawImage(HWND hwnd)
{
    ::GetWindowRect(hwnd, &wndRect);
    wndSize = { wndRect.right - wndRect.left,wndRect.bottom - wndRect.top };
    hdc = ::GetDC(hwnd);
    memDC = ::CreateCompatibleDC(hdc);
    memBitmap = ::CreateCompatibleBitmap(hdc, wndSize.cx, wndSize.cy);
    hOldBmp = (HBITMAP)::SelectObject(memDC, memBitmap);
    hPen = ::CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
    // 将笔选入DC
    hOldPen = (HPEN)::SelectObject(memDC, hPen);
    
    // 标记4个角
    int width = (rect.right - rect.left) / 8;
    int height = (rect.bottom - rect.top) / 8;
    int lightLength = width > height ? height : width;
    int nWidth = (rect.right - rect.left);
    int nHeight = rect.bottom - rect.top;

    //SetBkColor(memDC, RGB(255, 0, 0));
    MoveToEx(memDC, rect.left, rect.top, NULL);
    LineTo(memDC, rect.left, rect.top + lightLength);
    MoveToEx(memDC, rect.left, rect.bottom - lightLength, NULL);
    LineTo(memDC, rect.left, rect.bottom);
    LineTo(memDC, rect.left + lightLength, rect.bottom);
    MoveToEx(memDC, rect.right - lightLength, rect.bottom, NULL);
    LineTo(memDC, rect.right, rect.bottom);
    LineTo(memDC, rect.right, rect.bottom - lightLength);
    MoveToEx(memDC, rect.right, rect.top + lightLength, NULL);
    LineTo(memDC, rect.right, rect.top);
    LineTo(memDC, rect.right - lightLength, rect.top);
    MoveToEx(memDC, rect.left + lightLength, rect.top, NULL);
    LineTo(memDC, rect.left, rect.top);

    // 获取屏幕 DC
    screenDC = GetDC(NULL);
    ptSrc = { 0,0 };

    // 绘制窗口
    blendFunction.AlphaFormat = AC_SRC_ALPHA;
    blendFunction.BlendFlags = 0;
    blendFunction.BlendOp = AC_SRC_OVER;
    blendFunction.SourceConstantAlpha = 255;
    UpdateLayeredWindow(hwnd, screenDC, &ptSrc, &wndSize, memDC, &ptSrc, 0, &blendFunction, ULW_COLORKEY);

    // 清理资源
    SelectObject(memDC, hOldPen);
    SelectObject(memDC, hOldBmp);
    ::DeleteDC(memDC);
    ::DeleteObject(memBitmap);
    DeleteObject(hPen);
    ::ReleaseDC(hwnd, hdc);
    ReleaseDC(NULL, screenDC);
    return 0;
}

int CreateWindows()
{
    // 定义窗口结构
    WNDCLASSEX wincl;
    wincl.hInstance = GetModuleHandle(NULL);
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);
    wincl.hIcon = NULL;
    wincl.hIconSm = NULL;
    wincl.hCursor = NULL;
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);//(HBRUSH)COLOR_GRAYTEXT;

    // 注册窗口类
    if (!RegisterClassEx(&wincl))
    {
        printf("RegisterClassEx error : %d \n", GetLastError());
        return 0;
    }

    hwnd = CreateWindowEx(
        WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TOOLWINDOW,
        szClassName,
        "DrawWindowsTest Application",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,
        CW_USEDEFAULT,
        500,
        500,
        NULL,
        NULL,
        GetModuleHandle(NULL),
        NULL
    );
    std::cout << "CreateWindows Getlasteerror:" <

你可能感兴趣的:(图像,windows,c++,开发语言)