c++ - Win32 API : transparency doesn't work

标签 c++ winapi transparency

我写了一个简单的程序;它由主窗口和其上方的 TextBox 窗口组成。 TextBox 是 50% 透明的。当 TextBox 收到一条消息时,它会在主窗口上绘制一条蓝线。

问题是“透明”实际上根本不是透明的。如果蓝线穿过 TextBox 中的文本,则该文本将被删除,尽管该文本位于上方。反之亦然:如果我开始打字,一行文本中的一行的一部分就会消失,而不是闪耀。 这是一个错误吗?还是我遗漏了什么?

#include <windows.h>
#include <stdio.h>

#define IDC_MAIN_EDIT 101

void DrawInWindow(HWND hWndToPaint){
    HDC hdc = GetDC(hWndToPaint);
    if(!hdc)printf("Invalid handle\n");
    HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
    SelectObject(hdc, hPen);
    static float x=620, y=1, tg=0.5, ctg=2;
    static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
    MoveToEx(hdc,(int)x,(int)y,NULL);
    if(tg<1){
        y+=tg;
        x+=Xone;
    }else{
        y+=Yone;
        x+=ctg;
    }
    if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
    ReleaseDC(hWndToPaint,hdc);
    //Now recalculate direction
    RECT WndRect;
    GetClientRect(hWndToPaint,&WndRect);
    if(x>=WndRect.right){
        if(ctg>0)ctg*=-1;//make negative
        Xone=-1;
    }
    if(x<=WndRect.left){
        if(ctg<0)ctg*=-1;//make positive
        Xone=1;
    }
    if(y>=WndRect.bottom){
        if(tg>0)tg*=-1;//make negative
        Yone=-1;
    }
    if(y<=WndRect.top){
        if(tg<0)tg*=-1;//make positive
        Yone=1;
    }
}

int CALLBACK EnumWindowsFunc(HWND hWnd, LPARAM lParam){
    DrawInWindow(hWnd);
    return false;
}
void PaintInMainWnd(){
    EnumWindows(EnumWindowsFunc,0L);//Getting the handle of main window to draw
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
    switch(msg){
        case WM_KEYDOWN:
            if(wp == VK_ESCAPE)PostQuitMessage(0);
            break;
        case WM_DESTROY:
            printf("\ndestroying window\n");
            PostQuitMessage(0);
            return 0;
        case WM_SIZE:{
            HWND hEdit;
            RECT rcClient;

            GetClientRect(window, &rcClient);
            hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
            SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
            break;
        }
        default:
            return DefWindowProc( window, msg, wp, lp ) ;
    }
}

WNDPROC lpEditWndProc;

LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
    if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
    {
        PostQuitMessage(0);
        return 0;
    }
    PaintInMainWnd();
    lpEditWndProc(hWnd, uMsg, wParam, lParam);
}

bool CreateWindows(){
    const char* const myclass = "myclass";
    WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
                            0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
                            LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
                            0, myclass, LoadIcon(0,IDI_APPLICATION) };
    if(RegisterClassEx(&wndclass)<0){
        printf("ERR: in registering window class\n");
        return false;
    }
    //Creating window
    HWND window = CreateWindowEx( 0, myclass, "title",
                   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                   640, 480, 0, 0, GetModuleHandle(0), 0 );
    if(!window){
        printf("ERR: in creating window\n");
        return false;
    }
    ShowWindow( window, SW_SHOWDEFAULT );
    //creating TextBox on the window
    HFONT hfDefault;
    HWND hEdit;
    hEdit = CreateWindowEx(0, "edit", "", 
        WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
    if(hEdit == NULL){
        MessageBox(window, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
        return false;
    }
    hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
    //Now resize TextBox to fill whole parent window
    RECT RectSize;
    GetClientRect(window,&RectSize);
    hEdit = GetDlgItem(window,IDC_MAIN_EDIT);
    SetWindowPos(hEdit, 0,0,0,RectSize.right,RectSize.bottom,SWP_NOZORDER);
    //Let's try to catch some messages in TextBox...
    lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
    //Making hEdit transparent
    SetWindowLongPtr(hEdit,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(hEdit, GWL_EXSTYLE) );
    SetLayeredWindowAttributes(hEdit, 0, (255*50)/100, LWA_ALPHA);
    return true;
    //###
}

int main(){
    if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
    MSG msg;
    while(GetMessage(&msg,0,0,0) ){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

不知道这是否重要,但我还应该提一下,我只在 Ubuntu 和 Wine 下进行了测试,因为我的 Windows 被 this bug 搞砸了。 .无论如何,我希望问题不在 Wine 本身。 抱歉,代码量太大了,我真的不知道要删除什么以使其更小。

最佳答案

我找到了解决方法。我创建了背景顶层窗口,并将前景窗口设为 50% 透明。我在背景窗口中画线。如果前窗口移动或调整大小,后窗口会在 WM_WINDOWPOSCHANGED 消息的帮助下做出相应的 react ,它会在每次移动/调整大小时发送。

无论如何,这个变通办法有点脏,因为: Linux/wine 特定问题:1) 显示管理器不修饰 wine 的透明窗口(但这可以通过使第二个窗口 0% 透明来避免)2) 拖动的窗口摇晃, 但第二个直线移动。所有操作系统特定问题:第二个窗口在任务栏中可见。理论上,可以通过将 WS_EX_TOOLWINDOW 添加到无主窗口来避免最后一个。 The quote

To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style.

但是,至少在 wine 中它不起作用。好吧,我希望这是一个错误 :)

#include <windows.h>
#include <stdio.h>

#define IDC_MAIN_EDIT 101

HWND hBackWnd;

void DrawInWindow(HWND hWndToPaint){
    HDC hdc = GetDC(hWndToPaint);
    if(!hdc)printf("Invalid handle\n");
    HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
    SelectObject(hdc, hPen);
    static float x=620, y=1, tg=0.5, ctg=2;
    static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
    MoveToEx(hdc,(int)x,(int)y,NULL);
    if(tg<1){
        y+=tg;
        x+=Xone;
    }else{
        y+=Yone;
        x+=ctg;
    }
    if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
    ReleaseDC(hWndToPaint,hdc);
    //Now recalculate direction
    RECT WndRect;
    GetClientRect(hWndToPaint,&WndRect);
    if(x>=WndRect.right){
        if(ctg>0)ctg*=-1;//make negative
        Xone=-1;
    }
    if(x<=WndRect.left){
        if(ctg<0)ctg*=-1;//make positive
        Xone=1;
    }
    if(y>=WndRect.bottom){
        if(tg>0)tg*=-1;//make negative
        Yone=-1;
    }
    if(y<=WndRect.top){
        if(tg<0)tg*=-1;//make positive
        Yone=1;
    }
}

LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
    switch(msg){
        case WM_KEYDOWN:
            if(wp == VK_ESCAPE)PostQuitMessage(0);
            break;
        case WM_DESTROY:
            printf("\ndestroying window\n");
            PostQuitMessage(0);
            return 0;
        case WM_SIZE:
            HWND hEdit;
            RECT rcClient;

            GetClientRect(window, &rcClient);
            hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
            SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
            break;
        case WM_WINDOWPOSCHANGED:{//LPARAM is a ptr to WINDOWPOS
            RECT BckWndRect;
            if(!GetWindowRect(hBackWnd, &BckWndRect) )printf("ERR: getting backwnd rectangle\n");
            bool IsRepaint;
            WINDOWPOS* pNewPos = (WINDOWPOS*)lp;
            if(BckWndRect.left+BckWndRect.right != pNewPos->cx
            || BckWndRect.top+BckWndRect.bottom != pNewPos->cy)IsRepaint = true;
            else IsRepaint = false;
            MoveWindow(hBackWnd, pNewPos->x, pNewPos->y, pNewPos->cx, pNewPos->cy, IsRepaint);
            break;
        }
        default:
            return DefWindowProc( window, msg, wp, lp ) ;
    }
}

WNDPROC lpEditWndProc;

LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
    if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
    {
        PostQuitMessage(0);
        return 0;
    }
    DrawInWindow(hBackWnd);
    lpEditWndProc(hWnd, uMsg, wParam, lParam);
}

bool CreateWindows(){
    //creating back window
    const char* backwnd = "backwnd";
    WNDCLASSEX backwndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
                            0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
                            LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
                            0, backwnd, LoadIcon(0,IDI_APPLICATION) };
    if(RegisterClassEx(&backwndclass)<0){
        printf("ERR: in registering second window class\n");
        return false;
    }
    hBackWnd = CreateWindowEx( 0, backwnd, "title", WS_EX_TOOLWINDOW |
                   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                   640, 480, 0, 0, GetModuleHandle(0), 0 );
    if(!hBackWnd){
        printf("ERR: in creating background window\n");
        return false;
    }
    ShowWindow( hBackWnd, SW_SHOWDEFAULT );
    //Creating front window
    const char* const frontwnd = "frontwnd";
    WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
                            0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
                            LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
                            0, frontwnd, LoadIcon(0,IDI_APPLICATION) };
    if(RegisterClassEx(&wndclass)<0){
        printf("ERR: in registering foreground window class\n");
        return false;
    }
    HWND window = CreateWindowEx( 0, frontwnd, "title",
                   WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
                   640, 480, 0, 0, GetModuleHandle(0), 0 );
    if(!window){
        printf("ERR: in creating foreground window\n");
        return false;
    }
    ShowWindow( window, SW_SHOWDEFAULT );
    //creating textbox
    HWND hEdit = CreateWindowEx( 0, "edit", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL 
                                | ES_MULTILINE | ES_AUTOVSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, 640,
                                480, window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(0), 0 );
    HFONT hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
    //Let's try to catch some messages in TextBox...
    lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
    //Making foreground window transparent
    SetWindowLongPtr(window,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(window, GWL_EXSTYLE) );
    SetLayeredWindowAttributes(window, 0, (255*50)/100, LWA_ALPHA);
    return true;
    //###
}

int main(){
    if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
    MSG msg;
    while(GetMessage(&msg,0,0,0) ){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

关于c++ - Win32 API : transparency doesn't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21471769/

相关文章:

c++ - 是否可以将统一初始化和构造函数结合起来?

delphi - catch 启动应用程序

c# - 我不断在 DLL "Unable to find an entry point named ' 中收到 'user32.dll' GetWindowLongPtrA' ”

JavaFx 透明窗口 - 是的,请。鼠标透明 - 不,谢谢

c++ - 在 QtableWidget 中将列中的文本隐藏为密码点

c++ - LIBZIP 是否提供了一种方法来检查 zip_file 是否是目录?

c++ - OpenFrameworks - 减去 GL 窗口

c++ - 除了 SetCursor 之外还有什么可以重置光标形状?

java - 是否可以有一个带装饰的透明 JavaFX 窗口(最小化、最大化、关闭)

uitableview - 短暂暂停后,iOS 7 UITableViewCell 背景透明度变得不透明