-
May 22nd, 2018, 07:04 PM
#1
Transparent
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:
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 'concepts' as opposed to an example that actually works...
Any help appreciated!
-
May 31st, 2018, 10:56 AM
#2
Re: Transparent
Is there any reason why you need it to be a child window?
It would be much easier to just draw the text on top of the button image with Gdiplus::Graphics:: DrawString.
That would have the same visual appearance as your first image (demo.jpg).
Last edited by TubularX; May 31st, 2018 at 10:58 AM.
-
June 1st, 2018, 10:32 PM
#3
Re: Transparent
As TabularX said, I don't understand why you are making it a child control.
You can either just draw the entire button, or use custom drawing.
See https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx and https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|