C ++ Win32静态控件透明背景

时间:2014-05-03 07:19:20

标签: c++ winapi static controls

解决方案:如下所述,最好为文本创建自己的方法,而不是试图使控件异常行为。因此,为此创建自定义控件将是最佳选择。我找到了一个解释它的教程:http://www.codeproject.com/Articles/559385/Custom-Controls-in-Win-API-The-Basics

这已被问到,但没有实际的解决方案。

我正在尝试使用静态控件来显示文本,因此更新就像发送消息一样简单。我可以轻松地抓住控件,只使用普通的DrawText(),但它看起来像一个" sloppier"溶液

这是所有者绘制方法。

else if (message == WM_DRAWITEM) {  
    LPDRAWITEMSTRUCT pDIS;
    pDIS = (LPDRAWITEMSTRUCT)lParam;
    RECT rc;

    SetTextColor(pDIS->hDC, RGB(200,10,60));
    SelectObject(pDIS->hDC, (HPEN)GetStockObject(NULL_PEN));
    SelectObject(pDIS->hDC, (HBRUSH)GetStockObject(NULL_BRUSH));
    SetBkMode(pDIS->hDC, TRANSPARENT);
    // Start Drawing
    Rectangle(pDIS->hDC, 0, 0, pDIS->rcItem.right+1, pDIS->rcItem.bottom+1);
    DrawText(pDIS->hDC, "teststring", 10, &pDIS->rcItem, 0); 

    return 0;
}

我得到:  static_controls

左边是我得到的,正是我想要的。

CreateWindow("STATIC", "teststring", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW, 20, 20, 120, 40, hwnd, (HMENU)(IDC_STATIC_TEST), GetModuleHandle(NULL), NULL);   

这就是我用来创建静态的。

我花了4个多小时不停地尝试这样做,我已经尝试了一切

感谢任何帮助。

只是忘记静态控件并使用DrawText()重新开始。

感谢。

// create window
hwnd = CreateWindowEx (0, szClassName, "Test Transparent Static Main Window", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX , 100, 100, 300, 200, HWND_DESKTOP, NULL, hThisInstance, NULL);         
ShowWindow (hwnd, nFunsterStil);
// set globals
hWnd = hwnd;
hInstance = hThisInstance;

// main window message loop
while (GetMessage (&messages, NULL, 0, 0)) {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
}
return messages.wParam;
}






// Main Window Procedure
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
// local variables
PAINTSTRUCT ps;
HDC hdc;    


switch (message) {

    case WM_CREATE:   
        {     
        LRESULT lRes = DefWindowProc(hwnd, message, wParam, lParam);
        HWND hWndStatic = CreateWindowEx(0, "Static", NULL, WS_CHILD | WS_VISIBLE, 10, 10, 200, 100, hwnd, NULL, hInstance, NULL);
        StaticWndProc = (WNDPROC)SetWindowLong(hWndStatic, GWL_WNDPROC, (LPARAM)MyStaticWndProc);
        return lRes;            
        }
        break;

    case WM_PAINT: 
        hdc = BeginPaint(hwnd, &ps); 
        SetBkMode(hdc, TRANSPARENT);
        SetBkColor(hdc, RGB(110,110,110));
        EndPaint(hwnd, &ps);
        break;

   case WM_DESTROY:
        PostQuitMessage(0);       
        break;

    default:  
        return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}







LRESULT CALLBACK MyStaticWndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)     {   

if (Message == WM_PAINT) {   
    RECT rc;
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);

    GetClientRect(hwnd, &rc);
    SetBkMode(hdc, TRANSPARENT);
    SetTextColor(hdc, RGB(0,100,200));
    DrawText(hdc, "TESTTEXT", 8, &rc, DT_CENTER | DT_VCENTER | SS_LEFT);

    EndPaint(hwnd, &ps);

    return 0;
}

return StaticWndProc(hwnd, Message, wparam, lparam);
}

--------- EDIT ------------------------------------- --------------------

Example 2

1 个答案:

答案 0 :(得分:3)

无需做Draw​​ Draw,您只需使用SetWindowText()并处理WM_CTLCOLORSTATIC消息,请参阅this SO Answer 中的代码< - this如果窗口具有图案背景,则无法工作,我们需要子类化静态控件并在绘制文本时使用透明背景模式:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{   MSG msg;
    WNDCLASS w;

    hInst = hInstance;
    memset(&w,0,sizeof(WNDCLASS));
    w.style = CS_HREDRAW | CS_VREDRAW;
    w.lpfnWndProc = WndProc;
    w.hInstance = hInst;
    w.hbrBackground = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 0));
    w.lpszClassName = L"My Class";
    w.hCursor = LoadCursor(NULL, IDC_ARROW); 
    RegisterClass(&w);

    HWND hWndWindow = CreateWindow(L"My Class", L"My title", WS_OVERLAPPEDWINDOW, 300, 200, 800, 600, NULL, NULL, hInst, NULL);

    ShowWindow(hWndWindow, nCmdShow);
    UpdateWindow(hWndWindow);

    while(GetMessage(&msg, NULL, 0, 0))
    {   TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    DeleteObject(w.hbrBackground);

    return msg.wParam;
}

WNDPROC StaticWndProc = NULL;
TCHAR szText[] = _T("TestString");

LRESULT CALLBACK MyStaticWndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{   if (Message == WM_PAINT)
    {   RECT rc;
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rc);
        SetBkMode(hdc, TRANSPARENT);
        DrawText(hdc, szText, _tcslen(szText), &rc, DT_CENTER | DT_VCENTER);
        EndPaint(hwnd, &ps);
        return 0;
    }

      //v2 StaticWndProc(hwnd, Message, wparam, lparam);
    return return CallWindowProc(StaticWndProc, hwnd, Message, wparam, lparam); //v2
}

HWND hWndStatic; //v2
LONG WINAPI WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
{   switch (Message)
    {   case WM_CREATE:
        {   LRESULT lRes = DefWindowProc(hwnd, Message, wparam, lparam);
            hWndStatic = CreateWindowEx(0, L"Static", NULL, WS_CHILD| WS_VISIBLE |SS_LEFT, 10, 130, 200, 40, hwnd, NULL, hInst, NULL); //v2 deleted HWND
            StaticWndProc = (WNDPROC) SetWindowLong(hWndStatic, GWL_WNDPROC, (LPARAM)MyStaticWndProc);
            return lRes;
        }

        case WM_DESTROY: 
            SetWindowLong(hWndStatic, GWL_WNDPROC, (LPARAM)StaticWndProc); //v2
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, Message, wparam, lparam);
    }

    return 0;
}