透明背景窗口 - C ++ / CLI .net

时间:2012-02-08 01:24:00

标签: c++ .net window command-line-interface transparent

这真是一种压力,

我需要制作一个透明背景的表格,或者甚至在屏幕上画画,我想有能力在屏幕上绘制我想要的任何东西,但它仍然应该作为一个表格,所以我可以得到所有的事件出来.. 在调用UpdateLayeredWindow时,有人知道为什么我得到0?

我有以下结构

[StructLayout(LayoutKind::Sequential)]
public ref struct wPoint

[StructLayout(LayoutKind::Sequential)]
public ref struct wSize

[StructLayout(LayoutKind::Sequential, Pack = 1)]
public ref struct BLENDFUNCTION

我这些是从原生c ++导入的函数

        [DllImport("user32.dll")]
    static IntPtr GetDC(IntPtr hWnd);
    [DllImport("user32.dll")]
    static int ReleaseDC(IntPtr hWnd, IntPtr dc);   
    [DllImport("Kernel32.dll")]
    static unsigned long GetLastError(void);
    [DllImport("user32.dll")]
    static bool ShowWindow(unsigned long hWnd, int nCmdShow);
    [DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
    static int UpdateLayeredWindow(
        IntPtr hwnd,
        IntPtr hdcDst,
        [System::Runtime::InteropServices::In()]
        wPoint ^ pptDst,
        [System::Runtime::InteropServices::In()]
        wSize ^ psize,
        IntPtr hdcSrc,
        [System::Runtime::InteropServices::In()]
        wPoint ^ pptSrc,
        int crKey,
        [System::Runtime::InteropServices::In()]
        BLENDFUNCTION ^ pblend,
        int dwFlags);
    [DllImport("gdi32.dll")]
    static IntPtr CreateCompatibleDC(IntPtr hDC);
    [DllImport("gdi32.dll")]
    static int DeleteDC(IntPtr hdc);
    [DllImport("gdi32.dll")]
    static int DeleteObject(IntPtr hObject);
    [DllImport("gdi32.dll")]
    static IntPtr SelectObject(IntPtr hDC, IntPtr hObject);

我清空OnPaint函数(覆盖它)并覆盖OnShow函数以调用以下函数,并将Bitmap绘制到屏幕中。

void SmartToolTips::SetLayeredBitmap(Bitmap^ bitmap, Byte opacity){
IntPtr dc = Win32::GetDC(IntPtr::Zero);
IntPtr memorydc = Win32::CreateCompatibleDC(IntPtr::Zero);
IntPtr bitmaphandle = IntPtr::Zero;
IntPtr bitmaphandleold = IntPtr::Zero;
try
{
    bitmaphandle = bitmap->GetHbitmap(Color::FromArgb(0));
    bitmaphandleold = Win32::SelectObject(memorydc, bitmaphandle);

    wSize^ size = gcnew wSize(bitmap->Width, bitmap->Height);
    wPoint^ pointSource = gcnew wPoint(0, 0);
    wPoint^ topPos = gcnew wPoint(Left, Top);
    BLENDFUNCTION^ blend = gcnew BLENDFUNCTION();
    blend->BlendOp = AC_SRC_OVER;
    blend->BlendFlags = 0;
    blend->SourceConstantAlpha = opacity;
    blend->AlphaFormat = AC_SRC_ALPHA;
    int res = Win32::UpdateLayeredWindow(Handle, dc, topPos, size, memorydc, pointSource, 0, blend, ULW_ALPHA);
    if (!res)
    {
        Debug::WriteLine("Failed to update layered window");
        Debug::WriteLine(Win32::GetLastError());
    }
}
finally
{
    if (bitmaphandle != IntPtr::Zero)
    {
        Win32::SelectObject(memorydc, bitmaphandleold);
        Win32::DeleteObject(bitmaphandle);
    }
    Win32::DeleteDC(memorydc);
    Win32::ReleaseDC(IntPtr::Zero, dc); 
}

}

我覆盖的另一件事是:

        property System::Windows::Forms::CreateParams^ CreateParams 
    {
        virtual System::Windows::Forms::CreateParams^ get()override
        {
            System::Windows::Forms::CreateParams^ createParams = __super::CreateParams;
            createParams->ExStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT;
            return createParams;
        }
    }

有谁知道为什么没有弹出窗口?和UpdateLayeredWindow返回错误代码87:无效参数?

谢谢!

0 个答案:

没有答案