显示透明背景在按钮上的文本 [英] Display text with transparent background over button
问题描述
您好,我是b $ b $
我现在已经尝试了几天而无法让它发挥作用。
我想在子按钮上设置一个透明的背景文字子标签/按钮/窗口:
$
这与我的差距很近:
b
$ b
使用此代码:
$
#include< windows.h>
#include< tchar.h>
#include< objidl.h>
#include" stdafx.h"
#include" gdiplus.h"
#include< iostream>
#include< string>
Gdiplus :: Bitmap * m_pBitmap;
#define ID_BTNHI 0
const char windowClassName [] =" myWindowClass" ;;
HWND按钮;
class inputButton {
HWND hWnd;
int xpos,ypos;
public:
void createButton(HWND,int,int,int);
};
void inputButton :: createButton(HWND hWnd,int xpos,int ypos,int nCmdShow){
//创建按钮
// HWND hButton1 = CreateWindow( _T(" BUTTON"),_ T(" Test button"),BS_ICON | WS_VISIBLE | WS_CHILD,xpos,ypos,100,100,hWnd,(HMENU)1,NULL,NULL);
HWND hButton1;
hButton1 = CreateWindowEx(
0,// _In_ DWORD dwExStyle
_T(" BUTTON"),// _ In_opt_ LPCTSTR lpClassName
_T(" Test button"),/ / _In_opt_ LPCTSTR lpWindowName
BS_ICON | WS_VISIBLE | WS_CHILD,// _In_ DWORD dwStyle
xpos,// _In_ int x
ypos,// _In_ int y
100,// _ In_ int nWidth
100,// _In_ int nHeight
hWnd,// _In_opt_ HWND hWndParent
NULL,// _In_opt_ HMENU hMenu
NULL,// _In_opt_ HINSTANCE hInstance
NULL // _In_opt_ LPVOID lpParam
);
ShowWindow(hButton1,nCmdShow);
UpdateWindow(hButton1);
//将图像分配给按钮
Gdiplus :: Bitmap * m_pBitmap;
HICON hicon;
m_pBitmap = Gdiplus :: Bitmap :: FromFile(L" greySwitch.png");
m_pBitmap-> GetHICON(& hicon);
LRESULT lr = SendMessage(hButton1,BM_SETIMAGE,IMAGE_ICON,(LPARAM)hicon);
DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
// DWORD Flags1 = WS_EX_TRANSPARENT;
DWORD Flags2 = WS_POPUP;
HWND hStatic;
hStatic = CreateWindowEx(
WS_EX_TRANSPARENT,// _In_ DWORD dwExStyle
TEXT(" Static"),// _ In_opt_ LPCTSTR lpClassName
TEXT( " text"),// _In_opt_ LPCTSTR lpWindowName
WS_CHILD | WS_VISIBLE,// _In_ DWORD dwStyle
10,// _In_ int x
40,// _In_ int y
80,// _In_ int nWidth
20,// _In_ int nHeight
hButton1,// _In_opt_ HWND hWndParent
NULL,// _In_opt_ HMENU hMenu
NULL,// _In_opt_ HINSTANCE hInstance
NULL // _In_opt_ LPVOID lpParam
);
/ *
HRGN GGG = CreateRectRgn(0,0,50,50);
InvertRgn(GetDC(hStatic),GGG);
SetWindowRgn(hStatic,GGG,false);
COLORREF RRR = RGB(255,0,0);
SetLayeredWindowAttributes(hStatic,RRR,(BYTE)0,LWA_COLORKEY);
ShowWindow(hStatic,nCmdShow);
UpdateWindow(hStatic);
DeleteObject(GGG);
* /
ShowWindow(hStatic,nCmdShow);
UpdateWindow(hStatic);
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam){
int wmId,wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rect;
switch(msg){
case WM_CLOSE:
DestroyWindow(hWnd);
休息;
案例WM_DESTROY:
PostQuitMessage(0);
休息;
case WM_CTLCOLORSTATIC:{
:: MessageBox(NULL," WM_CTLCOLORSTATIC","",MB_OK);
}
休息;
案例WM_CREATE:{
}
break;
case WM_ERASEBKGND:{
/ *
GetClientRect(hWnd,& rect);
FillRect((HDC)wParam,& rect,CreateSolidBrush(RGB(255,0,0)));
* /
}
休息;
默认值:
返回DefWindowProc(hWnd,msg,wParam,lParam);
}
返回0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
FreeConsole(); //隐藏控制台
//初始化GDI +
Gdiplus :: GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(& gdiplusToken,& gdiplusStartupInput,NULL);
WNDCLASSEX wc;
HWND hWnd;
MSG Msg;
//主窗口的注册窗口类
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =(HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = windowClassName;
if(!RegisterClassEx(& wc)){
:: MessageBox(NULL," Window Registration Failed!","",MB_OK);
返回0;
}
//创建主窗口
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
windowClassName,
" C ++ Win32 Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
600,450,
NULL,NULL,hInstance,NULL
);
if(hWnd == NULL){
:: MessageBox(NULL,"Window Creation Failed!",""",MB_OK);
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
inputButton button1;
button1.createButton(hWnd,50,50,1);
inputButton button2;
button2.createButton(hWnd,150,50,1);
inputButton button3;
button3.createButton(hWnd,250,50,1);
inputButton button4;
button3.createButton(hWnd,350,50,1);
while(GetMessage(& Msg,NULL,0,0)> 0){
TranslateMessage(& ; MSG);
DispatchMessage(& Msg);
}
返回Msg.wParam;
}
毫无疑问,我认为我是C ++和C ++的初学者WinAPI所以对我正在做的事情并不是很了解。确切地说有很多结果,但它们似乎都假设了一些先验知识,只有编码'概念'而不是实际有效的
例子...
任何帮助表示感谢!
< span style ="font-size:0.75em"> *只是为了增加挫折程度,我无法发布图像,直到我的Windows帐户,我已经有10年了,显然已经过验证。为什么那里有选项? 唉,
顶部图片是一系列按钮,顶部有文本标签,透明背景
< blockquote>
概念图片:vesav.com.au/logo/demo1.jpg
进展图片:vesav.com.au/logo/demo2.jpg
Hi there,
I've been experimenting with this for a few days now and can't get it to work.
I am trying to get a transparent background text child label/button/window over a child button:
This is as close as I've got:
Using this code:
#include <windows.h> #include <tchar.h> #include <objidl.h> #include "stdafx.h" #include "gdiplus.h" #include <iostream> #include <string> Gdiplus::Bitmap* m_pBitmap; #define ID_BTNHI 0 const char windowClassName[] = "myWindowClass"; HWND button; class inputButton { HWND hWnd; int xpos, ypos; public: void createButton(HWND,int, int, int); }; void inputButton::createButton(HWND hWnd, int xpos, int ypos, int nCmdShow) { // Create button //HWND hButton1 = CreateWindow(_T("BUTTON"), _T("Test button"), BS_ICON | WS_VISIBLE | WS_CHILD, xpos, ypos, 100, 100, hWnd, (HMENU)1, NULL, NULL); HWND hButton1; hButton1 = CreateWindowEx( 0, // _In_ DWORD dwExStyle _T("BUTTON"), // _In_opt_ LPCTSTR lpClassName _T("Test button"), // _In_opt_ LPCTSTR lpWindowName BS_ICON | WS_VISIBLE | WS_CHILD, // _In_ DWORD dwStyle xpos, // _In_ int x ypos, // _In_ int y 100, // _In_ int nWidth 100, // _In_ int nHeight hWnd, // _In_opt_ HWND hWndParent NULL, // _In_opt_ HMENU hMenu NULL, // _In_opt_ HINSTANCE hInstance NULL // _In_opt_ LPVOID lpParam ); ShowWindow(hButton1, nCmdShow); UpdateWindow(hButton1); // Assign image to button Gdiplus::Bitmap* m_pBitmap; HICON hicon; m_pBitmap = Gdiplus::Bitmap::FromFile(L"greySwitch.png"); m_pBitmap->GetHICON(&hicon); LRESULT lr = SendMessage(hButton1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon); DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT; //DWORD Flags1 = WS_EX_TRANSPARENT; DWORD Flags2 = WS_POPUP; HWND hStatic; hStatic = CreateWindowEx( WS_EX_TRANSPARENT, // _In_ DWORD dwExStyle TEXT("Static"), // _In_opt_ LPCTSTR lpClassName TEXT("text"), // _In_opt_ LPCTSTR lpWindowName WS_CHILD | WS_VISIBLE, // _In_ DWORD dwStyle 10, // _In_ int x 40, // _In_ int y 80, // _In_ int nWidth 20, // _In_ int nHeight hButton1, // _In_opt_ HWND hWndParent NULL, // _In_opt_ HMENU hMenu NULL, // _In_opt_ HINSTANCE hInstance NULL // _In_opt_ LPVOID lpParam ); /* HRGN GGG = CreateRectRgn(0, 0, 50, 50); InvertRgn(GetDC(hStatic), GGG); SetWindowRgn(hStatic, GGG, false); COLORREF RRR = RGB(255, 0, 0); SetLayeredWindowAttributes(hStatic, RRR, (BYTE)0, LWA_COLORKEY); ShowWindow(hStatic, nCmdShow); UpdateWindow(hStatic); DeleteObject(GGG); */ ShowWindow(hStatic, nCmdShow); UpdateWindow(hStatic); } LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { int wmId, wmEvent; PAINTSTRUCT ps; HDC hdc; RECT rect; switch (msg) { case WM_CLOSE: DestroyWindow(hWnd); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_CTLCOLORSTATIC: { ::MessageBox(NULL, "WM_CTLCOLORSTATIC", "", MB_OK); } break; case WM_CREATE: { } break; case WM_ERASEBKGND: { /* GetClientRect(hWnd, &rect); FillRect((HDC)wParam, &rect, CreateSolidBrush(RGB(255, 0, 0))); */ } break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { FreeConsole(); //Hides console // Initialize GDI+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); WNDCLASSEX wc; HWND hWnd; MSG Msg; // Register window class for main window wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = windowClassName; if (!RegisterClassEx(&wc)) { ::MessageBox(NULL, "Window Registration Failed!", "", MB_OK); return 0; } // Create main window hWnd = CreateWindowEx( WS_EX_CLIENTEDGE, windowClassName, "C++ Win32 Window", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 450, NULL, NULL, hInstance, NULL ); if (hWnd == NULL) { ::MessageBox(NULL, "Window Creation Failed!", "", MB_OK); } ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); inputButton button1; button1.createButton(hWnd, 50, 50,1); inputButton button2; button2.createButton(hWnd, 150, 50, 1); inputButton button3; button3.createButton(hWnd, 250, 50, 1); inputButton button4; button3.createButton(hWnd, 350, 50, 1); while (GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }
As you can no doubt see I'm a beginner to both C++ and WinAPI so don't really know much about what I'm doing. There are quite a few results for exactly this but they all seem to assume some prior knowledge, with only coding 'concept' as opposed to an
example that actually works...
Any help appreciated!
*just to add to the frustration levels, I can't post images until my Windows account, which I've had for 10 years, is verified, apparently. Why have the option there then? Alas the top image is a series of buttons with a text label over the top with a transparent background
Concept Image: vesav.com.au/logo/demo1.jpg
Progress Image: vesav.com.au/logo/demo2.jpg
这篇关于显示透明背景在按钮上的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!