使用WinAPI使用透明背景创建文本标签 [英] Creating a text label with a transparent background using WinAPI

查看:636
本文介绍了使用WinAPI使用透明背景创建文本标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用WinAPI命令在窗口中创建带有透明背景的标签?



我试图将图像添加到对话框窗口用作背景图像,然后在该图像上显示文本。



这是我迄今为止我的代码的一个例子(显示了一个例子)对话框的整个消息处理程序):

  INT_PTR CALLBACK OfferWindowProc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
HWND hWndBackground;
HWND hWndLabel;
HBRUSH hLabelBackColour = NULL;
HFONT hfFont;
HWND hWndTitleLabel;
HDC hdcStatic = NULL;

UNREFERENCED_PARAMETER(lParam);
switch(message)
{
case WM_INITDIALOG:

//加载背景图片
HANDLE hBitmap;
hBitmap = LoadImage(NULL,LC:\\Users\\DavidHall\\Documents\\bg.bmp,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hBitmap!= NULL)
{
hWndBackground = CreateWindow(LSTATIC,Limage box,WS_CHILD | WS_VISIBLE | SS_BITMAP,0,0,100,100,hDlg ,(HMENU)2000,NULL,NULL);
SendMessage(hWndBackground,STM_SETIMAGE,IMAGE_BITMAP,LPARAM(hBitmap));
}

//使用CreateWindowEx创建标签
hfFont = CreateFont(20,0,0,0,fontWeight,FALSE,FALSE,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
DEFAULT_PITCH,LVerdana);

hWndTitleLabel = CreateWindowEx(WS_EX_TRANSPARENT,LSTATIC,L,WS_CHILD | WS_VISIBLE | SS_LEFT | WS_SYSMENU,xPos,yPos,width,height,hwnd,(HMENU)id,hInst,NULL) ;

SendMessage(hWndTitleLabel,WM_SETTEXT,NULL,(LPARAM)labelText.c_str());

SendMessage(hWndTitleLabel,WM_SETFONT,(WPARAM)hfFont,NULL);

return(INT_PTR)TRUE;

case WM_CLOSE:
EndDialog(hDlg,LOWORD(wParam));

//删除画笔 - 是正确的吗?
DeleteObject(hLabelBackColour);
return(INT_PTR)TRUE;

case WM_COMMAND:
if(LOWORD(wParam)== IDCANCEL)
{
EndDialog(hDlg,LOWORD(wParam)
return(INT_PTR)TRUE;
}
break;
默认值:
break;
}
return(INT_PTR)FALSE;
}

我尝试过各种组合,包括:




  • SetLayeredWindowAttributes

  • SetBkMode ,TRANSPARENT)

  • 处理 WM_CTLCOLORSTATIC 讯息


解决方案

我发现一些似乎是工作,虽然由于我是新的编程在纯WinAPI这可能是非常糟糕的做法

 

我正在处理WM_CTLCOLORSTATIC消息,如下所示:

code> case WM_CTLCOLORSTATIC:

hdcStatic =(HDC)wParam;
SetTextColor(hdcStatic,RGB(0,0,0));
SetBkMode(hdcStatic,TRANSPARENT);

return(LRESULT)GetStockObject(NULL_BRUSH);

这使我的标签出现在我的图像顶部,背景。


Is it possible to create a label with a transparent background within a window using only WinAPI commands?

I am trying to add an image to a Dialog window that will serve as a background image and then display text upon that image. Everything I have tried so far shows the text label on top of the background image with a grey rectangle drawn around it.

This is an example of my code so far (showing the entire message handler for the dialog):

INT_PTR CALLBACK OfferWindowProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{       
    HWND hWndBackground;
    HWND hWndLabel;
    HBRUSH hLabelBackColour = NULL;
      HFONT hfFont;
      HWND hWndTitleLabel;
    HDC hdcStatic = NULL;

    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:             

        //Load the background image
        HANDLE hBitmap;
        hBitmap = LoadImage(NULL, L"C:\\Users\\DavidHall\\Documents\\bg.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
        if (hBitmap != NULL)
        {
            hWndBackground = CreateWindow(L"STATIC", L"image box", WS_CHILD | WS_VISIBLE | SS_BITMAP, 0, 0, 100, 100, hDlg, (HMENU) 2000, NULL, NULL);          
            SendMessage(hWndBackground, STM_SETIMAGE, IMAGE_BITMAP, LPARAM(hBitmap));
        }           

        // Create the label using CreateWindowEx
        hfFont = CreateFont(20, 0, 0, 0, fontWeight, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
            DEFAULT_PITCH, L"Verdana");

        hWndTitleLabel = CreateWindowEx( WS_EX_TRANSPARENT, L"STATIC", L"", WS_CHILD | WS_VISIBLE | SS_LEFT | WS_SYSMENU , xPos, yPos, width, height, hwnd, (HMENU) id, hInst, NULL);           

        SendMessage(hWndTitleLabel, WM_SETTEXT, NULL, (LPARAM) labelText.c_str());

        SendMessage(hWndTitleLabel, WM_SETFONT, (WPARAM)hfFont, NULL);  

        return (INT_PTR)TRUE;   

    case WM_CLOSE:
        EndDialog(hDlg, LOWORD(wParam));    

        // Delete the brush - is that correct?
        DeleteObject(hLabelBackColour);
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    default:
        break;
    }
    return (INT_PTR)FALSE;
}

I've tried various combinations of things including:

  • SetLayeredWindowAttributes
  • SetBkMode(hdc, TRANSPARENT)
  • Handling the WM_CTLCOLORSTATIC message

解决方案

I've found something that appears to be working, though since I am very new to programming in pure WinAPI this could well be very bad practice and I would have no idea!

I am handling the WM_CTLCOLORSTATIC message as shown:

case WM_CTLCOLORSTATIC:

    hdcStatic = (HDC) wParam; 
    SetTextColor(hdcStatic, RGB(0,0,0));    
    SetBkMode (hdcStatic, TRANSPARENT);

    return (LRESULT)GetStockObject(NULL_BRUSH);

This gives the effect that I'm after of my label appearing on top of my image with a transparent background.

这篇关于使用WinAPI使用透明背景创建文本标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆