Detect Mouse Hover Over Button? - c++

I have an owner-drawn button that I would like to highlight, when the mouse hovers over it. Here is the simplified code, that doesn't seem to work:
case WM_DRAWITEM:
LPDRAWITEMSTRUCT pDraw = (LPDRAWITEMSTRUCT)lParam;
if(pDraw->itemState == ODS_HOTLIGHT) DrawButton(pDraw->hDC, HIcolor);
else DrawButton(pDraw->hDC, normcolor);
return 0;
DrawButton is a custom function that draws the button. The second parameter of this function is the primary color of the button. The function has no trouble drawing the button. The problem in in detecting the "item state".
I have also tried using if(pDraw->itemState & ODS_HOTLIGHT). That does not work either.
Obviously, I am missing something. In my search, I have come across many answers. However, none of them are in C++.

ODS_HOTLIGHT may not be used by the button. See What states are possible in a DRAWITEMSTRUCT structure?.
You can use subclassing, and then use TrackMouseEvent to get the event in SUBCLASSPROC.
LRESULT CALLBACK Subclassproc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
if (uMsg == WM_MOUSEMOVE)
{
TRACKMOUSEEVENT ev = {};
ev.cbSize = sizeof(TRACKMOUSEEVENT);
ev.dwFlags = TME_HOVER | TME_LEAVE;
ev.hwndTrack = hWnd;
ev.dwHoverTime = HOVER_DEFAULT;
TrackMouseEvent(&ev);
}
else if (uMsg == WM_MOUSEHOVER)
{
RECT rc = {};
GetClientRect(hWnd, &rc);
HDC hdc = GetDC(hWnd);
SetDCBrushColor(hdc, RGB(255, 0, 0));
SelectObject(hdc, GetStockObject(DC_BRUSH));
RoundRect(hdc, rc.left, rc.top,
rc.right, rc.bottom, 0, 0);
}
else if (uMsg == WM_MOUSELEAVE)
{
RECT rc = {};
GetClientRect(hWnd, &rc);
HDC hdc = GetDC(hWnd);
SetDCBrushColor(hdc, RGB(0, 255, 0));
SelectObject(hdc, GetStockObject(DC_BRUSH));
RoundRect(hdc, rc.left, rc.top,
rc.right, rc.bottom, 0, 0);
TRACKMOUSEEVENT ev = {};
ev.cbSize = sizeof(TRACKMOUSEEVENT);
ev.dwFlags = TME_HOVER | TME_LEAVE | TME_CANCEL;
ev.hwndTrack = hWnd;
ev.dwHoverTime = HOVER_DEFAULT;
TrackMouseEvent(&ev);
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
...
HWND hButton = CreateWindow(L"BUTTON", L"", WS_CHILD | WS_VISIBLE |
BS_OWNERDRAW, 10, 10, 60, 30, hWnd,
(HMENU)10001, hInst, NULL);
SetWindowSubclass(hButton, Subclassproc, 101, 0);

As stated in the description of the ODS_HOTLIGHT value:
ODS_HOTLIGHT: The item is being hot-tracked, that is, the item will be highlighted when the mouse is on the item.
So, I suppose that in order to receive this message you need to use the TrackMouseEvent function first.

Related

How can I make hover on a static window?

Currently only WM_MOUSEMOVE works. The image changes, but WM_MOUSELEAVE doesn't work anymore.
I want to make a hover effect on the button. What am I doing wrong?
[...]
hWndButton= CreateWindowW(L"Static", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP |
SS_NOTIFY, 10, 25, 109, 30, hwnd, (HMENU)4, NULL, NULL);
[...]
bool TheButtonToggle = false;
[...]
case WM_MOUSEMOVE:
if (TheButtonToggle == false) {
TRACKMOUSEEVENT tm_Event;
tm_Event.cbSize = sizeof(TRACKMOUSEEVENT);
tm_Event.dwFlags = TME_LEAVE;
tm_Event.hwndTrack = GetDlgItem((HWND)dwRefData, IDB_BUTTON);
TrackMouseEvent(&tm_Event);
TheButtonToggle = true;
HBITMAP hbmpButtonHover = LoadBitmap(hinst, MAKEINTRESOURCE(IDB_HOVER_BUTTON));
SendMessage(hWndButton, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmpButtonHover);
}
break;
case WM_MOUSELEAVE:
{
HBITMAP hbmpButton = LoadBitmap(hinst, MAKEINTRESOURCE(IDB_BUTTON));
SendMessage(hWndButton, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmpButton);
TheButtonToggle = false;
break;
}
A workaround:
You can subclass a button to monitor mouse move, if mouse move on the button, WM_MOUSEMOVE message in the subclass callback function will be triggered.
hWndButton = CreateWindowW(L"Static", NULL, WS_CHILD | WS_VISIBLE | SS_BITMAP |
SS_NOTIFY, 10, 25, 109, 30, hWnd, (HMENU)IDC_BUTTON, NULL, NULL);
SetWindowSubclass(hWndButton, &OwnerDrawButtonProc, IDC_BUTTON, 0);
[...]
LRESULT CALLBACK OwnerDrawButtonProc(HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
{
switch (uMsg)
{
case WM_MOUSEMOVE:
{
HBITMAP hbmpButton = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP3));
SendMessage(hWndButton, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmpButton);
}
case WM_NCDESTROY:
RemoveWindowSubclass(hWnd, &OwnerDrawButtonProc, 1);
break;
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
Debug:

How to change border color of a listview

I have created a list view using win32 api.
InitCommonControls();
HWND hwndList1 = CreateWindow(WC_LISTVIEW , L"", WS_VISIBLE|WS_CHILD | LVS_REPORT | LVS_EDITLABELS | LVS_ICON | LV_VIEW_TILE | LVS_EX_GRIDLINES | WS_BORDER | LVS_EX_FULLROWSELECT | ES_LEFT , 10, 10, 300, 190, hwnd, NULL, GetModuleHandle(NULL), 0);
SendMessageW( hwndList1,
LVM_SETEXTENDEDLISTVIEWSTYLE,
LVS_EX_FULLROWSELECT ,
LVS_EX_FULLROWSELECT );
CreateItem(hwndList1 , (char*)L"fault RS458");
CreateItem(hwndList1 , (char*)L"fault RS455");
CreateColumn(hwndList1 , 0 , (char*)L"Insert column" , 300);
I see a black border around the list view. How can i change its color?
You can subclass the window using SetWindowSubclass (requires comctl32.lib) and handle WM_NCPAINT to paint the non-client area of the control as follows:
#include <Windows.h>
#include <CommCtrl.h>
LRESULT CALLBACK ListViewProc(HWND hwnd,
UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR)
{
switch(msg)
{
case WM_NCPAINT:
{
RECT rc;
GetWindowRect(hwnd, &rc);
OffsetRect(&rc, -rc.left, -rc.top);
auto hdc = GetWindowDC(hwnd);
auto hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
auto oldpen = SelectObject(hdc, hpen);
SelectObject(hdc, GetStockObject(NULL_BRUSH));
Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);//draw red frame
SelectObject(hdc, oldpen);
DeleteObject(oldpen);
ReleaseDC(hwnd, hdc);
//*** EDIT
//documentation says we should return 0
//but that causes problem with vertical scrollbar
//maybe we should break for this subclass case
break; //not return 0!
}
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, ListViewProc, 0);
break;
}
return DefSubclassProc(hwnd, msg, wp, lp);
}
...
HWND hwndList1 = CreateWindow(...);
SetWindowSubclass(hwndList1, ListViewProc, 0, NULL);
Side note, (char*)L"text" doesn't make sense. Either use ANSI ((char*)"text") or Unicode ((wchar_t*)L"text", recommended). You can change CreateItem to accept const wchar_t*, then cast to (wchar_t*) for LVITEM at the last step to avoid errors.
Edit
WM_NCPAINT will break, not return zero.

Change window's background color with button using winapi

I'm trying to make button change background color of window when clicked. I know that I need to handle this event in WM_COMMAND, where I also check ID of this button, but nothing happens. I tried to debug and my program recognizes ID correctly. The piece of code used for changing color works well when in main loop but it doesn't do anything when in WM_COMMAND. How do I solve this problem? Whole code:
#include <Windows.h>
#define BUTTON_ID 100
struct status_info {
const char* waiting = "Waiting for connection...";
const char* connected = "Connected.\nWaiting for frajer to copy number.";
const char* changed = "Number changed.";
}status_info;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow) {
const wchar_t CLASS_NAME[] = L"Name";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = (LPCSTR)CLASS_NAME;
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
RegisterClass(&wc);
//main window
HWND hwnd = CreateWindowEx(0, (LPCSTR)CLASS_NAME, (LPCSTR)"Hacker", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInstance, NULL);
//number window
HWND number = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("Static"), TEXT("Account number:\n00 1234 1234 1234 1234 1234 1234"), WS_CHILD | WS_VISIBLE, 5, 5, 240, 40, hwnd, NULL, NULL, NULL);
//status window
const char* status_message = status_info.waiting;
HWND status = CreateWindowEx(WS_EX_WINDOWEDGE, TEXT("Static"), TEXT(status_message), WS_CHILD | WS_VISIBLE, 5, 55, 240, 40, hwnd, NULL, NULL, NULL);
//button
HWND button = CreateWindowEx(0, "BUTTON", "Nightmode", WS_CHILD | WS_VISIBLE, 100, 100, 150, 30, hwnd, (HMENU)BUTTON_ID, hInstance, NULL);
MSG msg;
WNDCLASS okno;
while (GetMessage(&msg, (HWND)NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
SetWindowText(status, status_message);
}
return msg.wParam;
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
//MessageBox(hwnd, L"1", L"1", 0);
return (0);
case WM_DESTROY:
//MessageBox(hwnd, L"2", L"2", 0);
PostQuitMessage(0);
return (0);
case WM_COMMAND: {
if (LOWORD(wParam) == BUTTON_ID) {
PAINTSTRUCT ps;
RECT rc;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetBkColor(hdc, BLACK_BRUSH);
ExtTextOut(hdc, 0, 0, ETO_OPAQUE, &rc, 0, 0, 0);
EndPaint(hwnd, &ps);
}
break;
}
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
BeginPaint/EndPaint should be used in response to WM_PAINT only.
You can use GetDC(hwnd)/ReleaseDC(hwnd, hdc) to obtain hdc for painting on device context outside of WM_PAINT, but this will be temporary. The next refresh message causes the window to be erased and repainted according to what's in WM_PAINT
SetDCBrushColor can be used if the goal is to avoid creating brush handle.
static COLORREF bkcolor = RGB(255,255,255);
switch(message)
{
case WM_COMMAND:
if(LOWORD(wparam) == BUTTON_ID)
{
bkcolor = RGB(255, 0, 0);
InvalidateRect(hwnd, NULL, TRUE);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
RECT rc;
HDC hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
SetDCBrushColor(hdc, bkcolor);
FillRect(hdc, &rc, (HBRUSH)GetStockObject(DC_BRUSH));
//or use ps.rcPaint to repaint only the section which requires update
//FillRect(hdc, &ps.rcPaint, (HBRUSH)GetStockObject(DC_BRUSH));
EndPaint(hwnd, &ps);
return 0;
}
case WM_ERASEBKGND:
//return 0 means WM_PAINT handles the background
return 0;
Alternatively, use SetClassLongPtr to replace the background brush:
static HBRUSH bkbrush = NULL;
switch(message)
{
case WM_COMMAND:
if(LOWORD(wparam) == BUTTON_ID)
{
COLORREF bkcolor = RGB(rand() % 256, rand() % 256, rand() % 256);
if(bkbrush)
DeleteObject(bkbrush);
bkbrush = CreateSolidBrush(bkcolor);
SetClassLongPtr(hwnd, GCL_HBRBACKGROUND, (LONG)bkbrush);
InvalidateRect(hwnd, NULL, TRUE);
}
break;

A label whose background is transparent on the one hand and the upper window on the other

I am currently building a ProgressBar in WinApi I want the caption "Loading" to appear on it.
I managed to create a label that would have a transparent background but I can not make the caption to be higher than the ProgressBar window.
This is my code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam)
{
static HWND hwndPrgBar;
static HWND lable;
static int i = 1;
HDC hdcStatic = NULL;
INITCOMMONCONTROLSEX InitCtrlEx;
InitCtrlEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
InitCtrlEx.dwICC = ICC_PROGRESS_CLASS;
InitCommonControlsEx(&InitCtrlEx);
switch (msg)
{
case WM_CREATE:
CenterWindow(hwnd);
/* creating the progress bar */
hwndPrgBar = CreateWindowEx(0, PROGRESS_CLASS, NULL,
WS_CHILD | WS_VISIBLE | PBS_SMOOTH | PBS_MARQUEE, // pbs_smooth makes no difference when manifest sets new windows styles
0, 0, 350, 25, hwnd, NULL, g_hinst, NULL);
/* sets progress bar looks */
SetWindowLongPtr(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(hwndPrgBar));
SendMessage(hwndPrgBar, (UINT)PBM_SETBKCOLOR, 0, RGB(224, 224, 224));
SendMessage(hwndPrgBar, (UINT)PBM_SETBARCOLOR, 0, (LPARAM)RGB(131, 203, 123));
SendMessage(hwndPrgBar, (UINT)PBM_SETMARQUEE, (WPARAM)TRUE, (LPARAM)5);
/* create the lable as child of prgress bar */
lable = CreateWindowEx(WS_EX_TRANSPARENT, L"STATIC", L"", WS_CHILD | WS_VISIBLE | SS_LEFT | WS_SYSMENU, 140, 5, 100, 100, hwnd, (HMENU)(100), (HINSTANCE)GetWindowLong(hwndPrgBar, GWL_HINSTANCE), NULL);
/* sets lable text to Loading */
SendMessage(lable, WM_SETTEXT, NULL, (LPARAM)L"Loading");
SetWindowPos(lable, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
/* I have tried this lines -> it didn't worked
SetForegroundWindow(lable);
BringWindowToTop(lable);
SetActiveWindow(lable);
SwitchToThisWindow(lable, TRUE);
SetWindowPos(lable, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
*/
/* set progress bar components */
SendMessage(hwndPrgBar, PBM_SETRANGE, 0, MAKELPARAM(0, 150));
SendMessage(hwndPrgBar, PBM_SETSTEP, 1, 0);
/* start prorgess bar */
i = 1;
SendMessage(hwndPrgBar, PBM_SETPOS, 0, 0);
SetTimer(hwnd, ID_TIMER, 100, NULL);
break;
case WM_TIMER:
SendMessage(hwndPrgBar, PBM_STEPIT, 0, 0);
i++;
if (i == 150)
{
KillTimer(hwnd, ID_TIMER);
exit(0);
}
break;
case WM_COMMAND:
break;
case WM_DESTROY:
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
break;
case WM_CTLCOLORSTATIC:
/* makes the lable transperent*/
hdcStatic = (HDC)wParam;
SetTextColor(hdcStatic, RGB(0, 0, 0));
SetBkMode(hdcStatic, TRANSPARENT);
return (LRESULT)GetStockObject(NULL_BRUSH);
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
How can I make a label on one side have a transparent background and on the other hand it will be above the progress bar?
Handling WM_CTLCOLORSTATIC will work, but you have to invalidate label each time the progress bar changes the background:
case WM_TIMER:
SendMessage(hwndPrgBar, PBM_STEPIT, 0, 0);
InvalidateRect(lable, NULL, FALSE);
...
Or using subclass instead (remove the static control):
LRESULT CALLBACK PrgBarProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
UINT_PTR, DWORD_PTR)
{
switch(msg) {
case WM_PAINT:
{
LRESULT lres = DefSubclassProc(hwnd, msg, wp, lp);
HDC hdc = GetDC(hwnd);
RECT rc;
GetClientRect(hwnd, &rc);
SetBkMode(hdc, TRANSPARENT);
DrawText(hdc, L"Text", -1, &rc, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
ReleaseDC(hwnd, hdc);
return lres;
}
case WM_NCDESTROY:
RemoveWindowSubclass(hwnd, SubclassProc, 0);
return DefSubclassProc(hwnd, msg, wp, lp);
}
return DefSubclassProc(hwnd, msg, wp, lp);
}
...
SetWindowSubclass(hwndPrgBar, PrgBarProc, 0, 0);
PBS_MARQUEE forces the bar to go back and forth. You don't want that here. And it won't work unless visual styles are enabled through the manifest file, or the following lines in Visual Studio:
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

Win32 API: transparency doesn't work

I wrote a simple program; it's made of a main window and TextBox window above it. TextBox is 50% transparent. When TextBox gets a message, it draws a blue line on the main window.
The problem is that "transparent" actually is not transparent at all. If the blue line crosses a text in TextBox, the text just erased, despite the fact that the text is above. And vice versa: if I start typing, a part of the line in a row of a text just disappears instead of shine through.
Is this a bug? Or am I missing something?
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
void DrawInWindow(HWND hWndToPaint){
HDC hdc = GetDC(hWndToPaint);
if(!hdc)printf("Invalid handle\n");
HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
SelectObject(hdc, hPen);
static float x=620, y=1, tg=0.5, ctg=2;
static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
MoveToEx(hdc,(int)x,(int)y,NULL);
if(tg<1){
y+=tg;
x+=Xone;
}else{
y+=Yone;
x+=ctg;
}
if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
ReleaseDC(hWndToPaint,hdc);
//Now recalculate direction
RECT WndRect;
GetClientRect(hWndToPaint,&WndRect);
if(x>=WndRect.right){
if(ctg>0)ctg*=-1;//make negative
Xone=-1;
}
if(x<=WndRect.left){
if(ctg<0)ctg*=-1;//make positive
Xone=1;
}
if(y>=WndRect.bottom){
if(tg>0)tg*=-1;//make negative
Yone=-1;
}
if(y<=WndRect.top){
if(tg<0)tg*=-1;//make positive
Yone=1;
}
}
int CALLBACK EnumWindowsFunc(HWND hWnd, LPARAM lParam){
DrawInWindow(hWnd);
return false;
}
void PaintInMainWnd(){
EnumWindows(EnumWindowsFunc,0L);//Getting the handle of main window to draw
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
switch(msg){
case WM_KEYDOWN:
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
printf("\ndestroying window\n");
PostQuitMessage(0);
return 0;
case WM_SIZE:{
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
break;
}
default:
return DefWindowProc( window, msg, wp, lp ) ;
}
}
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
PaintInMainWnd();
lpEditWndProc(hWnd, uMsg, wParam, lParam);
}
bool CreateWindows(){
const char* const myclass = "myclass";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, myclass, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering window class\n");
return false;
}
//Creating window
HWND window = CreateWindowEx( 0, myclass, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating window\n");
return false;
}
ShowWindow( window, SW_SHOWDEFAULT );
//creating TextBox on the window
HFONT hfDefault;
HWND hEdit;
hEdit = CreateWindowEx(0, "edit", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
if(hEdit == NULL){
MessageBox(window, "Could not create edit box.", "Error", MB_OK | MB_ICONERROR);
return false;
}
hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Now resize TextBox to fill whole parent window
RECT RectSize;
GetClientRect(window,&RectSize);
hEdit = GetDlgItem(window,IDC_MAIN_EDIT);
SetWindowPos(hEdit, 0,0,0,RectSize.right,RectSize.bottom,SWP_NOZORDER);
//Let's try to catch some messages in TextBox...
lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
//Making hEdit transparent
SetWindowLongPtr(hEdit,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(hEdit, GWL_EXSTYLE) );
SetLayeredWindowAttributes(hEdit, 0, (255*50)/100, LWA_ALPHA);
return true;
//###
}
int main(){
if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
MSG msg;
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Don't know is it important, but I should also mention, that I have tested only under Ubuntu with Wine, as my Windows is screwed up by this bug. Anyway, I hope the problem is not in Wine itself.
And sorry for amount of code, I really don't know what to remove to make it smaller.
I found the workaround. I created background top level window, and made foreground window 50% trasparent. I draw the lines in background window. If the front window moved or resized, the back window reacts accordingly with help of WM_WINDOWPOSCHANGED message, it sent on every little move/resize.
Anyway, this workaround is little dirty because of:
Linux/wine specific problems: 1) Display Manager do not decorate transparent window of wine(but this could be evaded by making second window 0% transparent) 2) Dragged window wobbling, but the second moving straight. All OS specific problem: second window is visible in the taskbar. Theoretically the last could be avoided by adding WS_EX_TOOLWINDOW to unowned window. The quote
To prevent the window button from being placed on the taskbar, create
the unowned window with the WS_EX_TOOLWINDOW extended style.
But, at least, in wine it doesn't work. Well, I hope this is a bug :)
#include <windows.h>
#include <stdio.h>
#define IDC_MAIN_EDIT 101
HWND hBackWnd;
void DrawInWindow(HWND hWndToPaint){
HDC hdc = GetDC(hWndToPaint);
if(!hdc)printf("Invalid handle\n");
HPEN hPen = CreatePen(PS_SOLID,5,RGB(0, 0, 255));
SelectObject(hdc, hPen);
static float x=620, y=1, tg=0.5, ctg=2;
static int Xone = 1, Yone = 1;//depending on later recalculation this may become negative
MoveToEx(hdc,(int)x,(int)y,NULL);
if(tg<1){
y+=tg;
x+=Xone;
}else{
y+=Yone;
x+=ctg;
}
if(!LineTo(hdc, (int)x, (int)y) )printf("There are paint problem\n");
ReleaseDC(hWndToPaint,hdc);
//Now recalculate direction
RECT WndRect;
GetClientRect(hWndToPaint,&WndRect);
if(x>=WndRect.right){
if(ctg>0)ctg*=-1;//make negative
Xone=-1;
}
if(x<=WndRect.left){
if(ctg<0)ctg*=-1;//make positive
Xone=1;
}
if(y>=WndRect.bottom){
if(tg>0)tg*=-1;//make negative
Yone=-1;
}
if(y<=WndRect.top){
if(tg<0)tg*=-1;//make positive
Yone=1;
}
}
LRESULT __stdcall MyMainCallBckProcedure( HWND window, unsigned msg, WPARAM wp, LPARAM lp ){
switch(msg){
case WM_KEYDOWN:
if(wp == VK_ESCAPE)PostQuitMessage(0);
break;
case WM_DESTROY:
printf("\ndestroying window\n");
PostQuitMessage(0);
return 0;
case WM_SIZE:
HWND hEdit;
RECT rcClient;
GetClientRect(window, &rcClient);
hEdit = GetDlgItem(window, IDC_MAIN_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
break;
case WM_WINDOWPOSCHANGED:{//LPARAM is a ptr to WINDOWPOS
RECT BckWndRect;
if(!GetWindowRect(hBackWnd, &BckWndRect) )printf("ERR: getting backwnd rectangle\n");
bool IsRepaint;
WINDOWPOS* pNewPos = (WINDOWPOS*)lp;
if(BckWndRect.left+BckWndRect.right != pNewPos->cx
|| BckWndRect.top+BckWndRect.bottom != pNewPos->cy)IsRepaint = true;
else IsRepaint = false;
MoveWindow(hBackWnd, pNewPos->x, pNewPos->y, pNewPos->cx, pNewPos->cy, IsRepaint);
break;
}
default:
return DefWindowProc( window, msg, wp, lp ) ;
}
}
WNDPROC lpEditWndProc;
LRESULT CALLBACK MyEditCallBckProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
if( (uMsg == WM_CHAR) && (wParam == VK_ESCAPE) )
{
PostQuitMessage(0);
return 0;
}
DrawInWindow(hBackWnd);
lpEditWndProc(hWnd, uMsg, wParam, lParam);
}
bool CreateWindows(){
//creating back window
const char* backwnd = "backwnd";
WNDCLASSEX backwndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, backwnd, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&backwndclass)<0){
printf("ERR: in registering second window class\n");
return false;
}
hBackWnd = CreateWindowEx( 0, backwnd, "title", WS_EX_TOOLWINDOW |
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!hBackWnd){
printf("ERR: in creating background window\n");
return false;
}
ShowWindow( hBackWnd, SW_SHOWDEFAULT );
//Creating front window
const char* const frontwnd = "frontwnd";
WNDCLASSEX wndclass = { sizeof(WNDCLASSEX), CS_DBLCLKS, MyMainCallBckProcedure,
0, 0, GetModuleHandle(0), LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW), HBRUSH(COLOR_WINDOW+1),
0, frontwnd, LoadIcon(0,IDI_APPLICATION) };
if(RegisterClassEx(&wndclass)<0){
printf("ERR: in registering foreground window class\n");
return false;
}
HWND window = CreateWindowEx( 0, frontwnd, "title",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
640, 480, 0, 0, GetModuleHandle(0), 0 );
if(!window){
printf("ERR: in creating foreground window\n");
return false;
}
ShowWindow( window, SW_SHOWDEFAULT );
//creating textbox
HWND hEdit = CreateWindowEx( 0, "edit", "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL
| ES_MULTILINE | ES_AUTOVSCROLL, CW_USEDEFAULT, CW_USEDEFAULT, 640,
480, window, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(0), 0 );
HFONT hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
//Let's try to catch some messages in TextBox...
lpEditWndProc = (WNDPROC)SetWindowLongPtr(hEdit, GWL_WNDPROC, (LONG_PTR)&MyEditCallBckProcedure);
//Making foreground window transparent
SetWindowLongPtr(window,GWL_EXSTYLE, WS_EX_LAYERED | GetWindowLongPtr(window, GWL_EXSTYLE) );
SetLayeredWindowAttributes(window, 0, (255*50)/100, LWA_ALPHA);
return true;
//###
}
int main(){
if(!CreateWindows() ){printf("Something gone wrong\n");return 1;}
MSG msg;
while(GetMessage(&msg,0,0,0) ){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}