thanks for that.
let me ask 1 thing: when i use WM_PAINT in my child window(my label class), why the mouse messages aren't activated?
Printable View
How are you using WM_PAINT?
For your MouseButtons enum, if you changed some of the values, you could somewhat simply the code for the WM_xBUTTONDOWN cases in the control window procedure.
Consider
In child window procedureCode:enum MouseButtons
{
none = -1,
Left = WM_LBUTTONUP,
Right = WM_RBUTTONUP,
Middle = WM_MBUTTONUP,
X1 = WM_XBUTTONUP,
X2
};
Code:case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
{
SetFocus(inst->hwnd);
int xPos = (int)(short) LOWORD(lParam);
int yPos = (int)(short) HIWORD(lParam);
bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
MouseButtons MBButtons = none;
if (msg == WM_XBUTTONUP) {
if (wParam & MK_XBUTTON1)
MBButtons = X1;
else
if (wParam & MK_XBUTTON2)
MBButtons = X2;
} else
MBButtons = (MouseButtons)msg;
inst->MouseUp(MBButtons, blControl, blshift, xPos, yPos);
}
break;
i have 1 problem with my class... heres the enetire class code:
i think i miss something, because when i create a 2nd label, the second label recives from the 1st label.Code:#include <windows.h>
#include <string>
#include <functional>
#define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
typedef COLORREF color;
using namespace std;
const char *labelpropname = "Cambalinho";
const char *labelclassprop = "classaddr";
enum MouseButtons
{
None=-1,
Left=0,
Right=1,
Middle=2,
X1=3,
X2=4
};
struct Position
{
int X;
int Y;
};
struct Size
{
int Width;
int Height;
};
class label
{
private:
HWND hwnd;
color clrTextColor=RGB(0,0,0);
color clrBackColor=RGB(255,255,255);
bool blnTransparent=false;
string caption="label";
void TrackMouse(HWND hwnd)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
tme.dwHoverTime = 5000; //How long the mouse has to be in the window to trigger a hover event.
tme.hwndTrack = hwnd;
TrackMouseEvent(&tme);
}
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
label *inst = (label*)GetProp(GetParent(hwnd), labelclassprop);
if (oldproc == NULL || inst == NULL)
MessageBox(NULL, "null", "null", MB_OK);
static bool TrackingMouse = false;
static bool WindowStopMoving = NULL;
static bool WindowStopResize = NULL;
static const UINT_PTR MouseStopTimerID = 1;
HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
switch(msg)
{
case WM_ERASEBKGND:
{
if (inst->blnTransparent==true)
{
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
RECT c;
GetWindowRect(inst->hwnd,&c);
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
return 1;
}
}
break;
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC)wParam;
if (inst->blnTransparent==false)
{
/*SetTextColor(hdcStatic, inst->clrTextColor);
SetBkMode(hdcStatic, TRANSPARENT);
SetBkColor(hdcStatic,inst->clrBackColor);
g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
return (LONG)g_hbrBackground;*/
}
else
{
SetTextColor(hdcStatic, inst->clrTextColor);
SetBkMode(hdcStatic, TRANSPARENT);
SetBkColor(hdcStatic,inst->clrBackColor);
g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
return (LONG)g_hbrBackground;
}
}
break;
case WM_CREATE:
{
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
inst->Create(xPos,yPos);
}
break;
case WM_MOVE:
{
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
inst->Move(xPos,yPos);
WindowStopMoving=false;
WindowStopResize=true;
}
break;
case WM_SIZE :
{
WindowStopResize=false;
WindowStopMoving=true;
}
break;
case WM_EXITSIZEMOVE:
{
if(WindowStopResize==false)
{
WindowStopResize=true;
inst->NotResize();
}
else if (WindowStopMoving==false)
{
WindowStopMoving=true;
inst->Stop();
}
}
break;
case WM_NCHITTEST:
return DefWindowProc(hwnd, msg, wParam, lParam);
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
{
SetFocus(inst->hwnd);
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
MouseButtons MBButtons;
if((wParam & MK_LBUTTON)!= false)
MBButtons=Left;
else if (wParam & MK_RBUTTON)
MBButtons=Right;
else if (wParam & MK_MBUTTON)
MBButtons=Middle;
else if (wParam & MK_XBUTTON1)
MBButtons=X1;
else if (wParam & MK_XBUTTON2)
MBButtons=X2;
else
MBButtons=None;
inst->MouseDown(MBButtons,blControl,blshift,xPos,yPos);
}
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
{
SetFocus(inst->hwnd);
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
MouseButtons MBButtons;
if (msg == WM_LBUTTONUP)
MBButtons=Left;
else if (msg == WM_RBUTTONUP)
MBButtons=Right;
else if (msg == WM_MBUTTONUP)
MBButtons=Middle;
else if (msg == WM_XBUTTONUP)
{
if (wParam & MK_XBUTTON1)
MBButtons=X1;
else if (wParam & MK_XBUTTON2)
MBButtons=X2;
}
else
MBButtons=None;
inst->MouseUp(MBButtons,blControl,blshift,xPos,yPos);
}
break;
case WM_NCMOUSELEAVE:
case WM_MOUSELEAVE :
{
TrackingMouse = false;
inst->MouseLeave();
}
break;
case WM_NCMOUSEMOVE:
case WM_MOUSEMOVE:
{
if (!TrackingMouse)
{
inst->TrackMouse(hwnd);
TrackingMouse = true;
inst->MouseEnter();
}
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
static bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
static bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
static MouseButtons MBButtons;
if((wParam & MK_LBUTTON)!= false)
MBButtons=Left;
else if ((wParam & MK_RBUTTON)!= false)
MBButtons=Right;
else if ((wParam & MK_MBUTTON)!= false)
MBButtons=Middle;
else if ((wParam & MK_XBUTTON1)!= false)
MBButtons=X1;
else if ((wParam & MK_XBUTTON2)!= false)
MBButtons=X2;
else
MBButtons=None;
inst->MouseMove(MBButtons,blControl,blshift,xPos,yPos);
KillTimer(hwnd, MouseStopTimerID);
SetTimer(hwnd, MouseStopTimerID, 500, NULL);
}
break;
case WM_MOUSEHOVER:
{
inst->MouseHover();
}
break;
case WM_TIMER:
switch(wParam)
{
case MouseStopTimerID:
{
KillTimer(hwnd, MouseStopTimerID);
inst->MouseStoped();
}
}
break;
case WM_KEYUP:
inst->KeyUp(lParam,wParam);
break;
case WM_KEYDOWN:
inst->KeyDown(lParam,wParam);
break;
default:
break;
}
return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
}
public:
//is these 2 lines ok?
//see the #define on top
event(NotResize)=[](){;};
event(Stop)=[](){;};
event(MouseEnter)=[](){;};
event(MouseLeave)=[](){;};
event(Create,(int x, int y))=[](int x, int y){;};
event(Move,(int x, int y))=[](int x, int y){;};
event(MouseDown,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseUp,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseMove,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseStoped)=[](){;};
event(MouseHover)=[](){;};
event(MouseClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseDoubleClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseWhell,(int Whell, MouseButtons Button,bool control, bool shift, int x, int y))=[](int Whell, int Button, bool alt, bool shift,int x, int y){;};
event(KeyDown,(int repeat,int Key))=[](int repeat,int Key){;};
event(KeyUp,(int repeat,int Key))=[](int repeat,int Key){;};
~label()
{
DestroyWindow(hwnd);
hwnd = 0;
}
label()
{
;
}
void setparent(HWND parent)
{
static int i=i+1;
string strclass=labelclassprop + to_string(i);
labelclassprop=strclass.c_str();
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = labelclassprop;
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
SetProp(parent, labelclassprop, (HANDLE)this);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
// register the new window class, "ShEdit"
if (!RegisterClass(&wc))
MessageBox(NULL, "error in register", "error", MB_OK);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
caption.c_str(),
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
NULL);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
UpdateWindow(hwnd);
}
Size GetSize()
{
RECT LabelSize;
GetWindowRect(hwnd,&LabelSize);
Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
return crdSize;
}
void SetSize(int Width, int Height)
{
RECT LabelSize;
GetWindowRect(hwnd,&LabelSize);
LabelSize.right=LabelSize.left+Width;
LabelSize.bottom=LabelSize.top+Height;
SetWindowPos(hwnd,HWND_TOP,LabelSize.left,LabelSize.top,Width,Height,SWP_NOMOVE|SWP_NOZORDER);
}
Position GetPosition()
{
RECT LabelSize;
GetWindowRect(hwnd,&LabelSize);
Position crdSize = {LabelSize.left,LabelSize.top};
return crdSize;
}
void SetPosition(int x, int y)
{
RECT LabelSize;
GetWindowRect(hwnd,&LabelSize);
SetWindowPos(hwnd,HWND_TOP,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
}
void Transparent(bool transparent)
{
blnTransparent=transparent;
}
void SetText(string text)
{
caption=text;
SetWindowText(hwnd,text.c_str());
}
string GetText()
{
return caption;
}
void SetBackColor(COLORREF color)
{
clrBackColor=color;
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwnd,SW_SHOW);
}
color GetBackColor()
{
return clrBackColor;
}
void SetTransparency(std::uint8_t Transperancy)
{
long wAttr = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, wAttr | WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd, 0, Transperancy, 0x2);
const char *text;
text=to_string( GetLastError()).c_str();
MessageBoxA(NULL,text,"erro",MB_OK);
}
void SetColorText(COLORREF color)
{
clrTextColor=color;
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwnd,SW_SHOW);
}
color GetColorText()
{
return clrTextColor;
}
HFONT GetFont()
{
HFONT actual;
actual=(HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
return actual;
}
void SetFont(HFONT newfont)
{
SendMessage(hwnd,WM_SETFONT,(WPARAM)newfont,0);
}
void transparent(bool bltransparent)
{
blnTransparent=bltransparent;
}
HWND GetHWND()
{
return hwnd;
}
};
maybe is because of hwnd and maybe i need use the 'this' too.
can you advice me?
How are you creating the labels? Are you actually seeing the two label windows? From looking at your code you create all static windows at position 100, 100 so it looks like you are creating each label window on top of each other? Are you calling SetPosition() somewhere for the second label?Quote:
i think i miss something, because when i create a 2nd label, the second label recives from the 1st label.
the labels are shopwed in diferent places(like i choose). the problem is:
the text is changed on label2 even when i move the mouse on label1.Code:label1.MouseEnter=[]()
{
label1.SetBackColor(RGB(255,0,0));
label1.SetColorText(RGB(0,255,0));
label1.SetText("mouse enter");
};
label1.MouseLeave=[]()
{
label1.SetBackColor(RGB(0,255,0));
label1.SetColorText(RGB(0,0,255));
label1.SetText("mouse leave");
};
label1.setparent(hwnd);
label1.SetSize(100,100);
label1.SetPosition(3,5);
//label1.transparent(true);
label2.MouseEnter=[]()
{
label2.SetBackColor(RGB(255,0,0));
label2.SetColorText(RGB(0,255,0));
label2.SetText("mouse enter");
};
label2.MouseLeave=[]()
{
label2.SetBackColor(RGB(0,255,0));
label2.SetColorText(RGB(0,0,255));
label2.SetText("mouse leave");
};
label2.setparent(hwnd);
label2.SetPosition(300,5);
Because you are setting the labelclass property for the control parent window rather than the control window. So every control has the same class instance rather than a different one. These changes should do it.
Code:// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
//SetProp(parent, labelclassprop, (HANDLE)this);
Code:SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
Code:label *inst = (label*)GetProp(hwnd, labelclassprop);
heres the code:
i get several problems on running :(Code:#include <windows.h>
#include <string>
#include <functional>
#define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
typedef COLORREF color;
using namespace std;
const char *labelpropname = "Cambalinho";
const char *labelclassprop = "classaddr";
enum MouseButtons
{
None=-1,
Left=0,
Right=1,
Middle=2,
X1=3,
X2=4
};
struct Position
{
int X;
int Y;
};
struct Size
{
int Width;
int Height;
};
class label
{
private:
HWND hwnd;
color clrTextColor=RGB(0,0,0);
color clrBackColor=RGB(255,255,255);
bool blnTransparent=false;
string caption="label";
void TrackMouse(HWND hwnd)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
tme.dwHoverTime = 5000; //How long the mouse has to be in the window to trigger a hover event.
tme.hwndTrack = hwnd;
TrackMouseEvent(&tme);
}
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
label *inst = (label*)GetProp(hwnd, labelclassprop);
if (oldproc == NULL || inst == NULL)
MessageBox(NULL, "null", "null", MB_OK);
static bool TrackingMouse = false;
static bool WindowStopMoving = NULL;
static bool WindowStopResize = NULL;
static const UINT_PTR MouseStopTimerID = 1;
HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
//if(hwnd!=inst->hwnd) return DefWindowProc(hwnd, msg, wParam, lParam);
switch(msg)
{
case WM_ERASEBKGND:
{
if (inst->blnTransparent==true)
{
HDC mdc = CreateCompatibleDC(NULL);//creating the doublebuffer
Size a=inst->GetSize();//get the control size
RECT c;
GetWindowRect(inst->hwnd,&c);
Position b=inst->GetPosition();//get ocntrol position
HBITMAP mbmp = CreateBitmap(a.Width,a.Height,1,24,NULL); //create the bimap with a size
HBITMAP moldbmp = (HBITMAP)SelectObject(mdc,mbmp);//select the DC from that bitmap
HDC labeldc=GetDC(inst->hwnd);//get the label DC
BitBlt(mdc,0,0,a.Width,a.Height,GetDC(GetParent(inst->hwnd)),b.X, b.Y,SRCCOPY);//copy the parent control
TransparentBlt(mdc,0,0,a.Width,a.Height,labeldc,0,0,a.Width,a.Height,(UINT)inst->clrBackColor);//copy the label DC to mdc(double buffer), without backcolor
BitBlt(GetDC(inst->hwnd),0,0,a.Width,a.Height,mdc,b.X, b.Y,SRCCOPY);//now copy the double buffer to label dc
//clean objects
SelectObject(mdc,moldbmp);
DeleteObject(mbmp);
DeleteDC(mdc);
return 1;
}
}
break;
case WM_CTLCOLORSTATIC:
{
HDC hdcStatic = (HDC)wParam;
if (inst->blnTransparent==false)
{
/*SetTextColor(hdcStatic, inst->clrTextColor);
SetBkMode(hdcStatic, TRANSPARENT);
SetBkColor(hdcStatic,inst->clrBackColor);
g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
return (LONG)g_hbrBackground;*/
}
else
{
SetTextColor(hdcStatic, inst->clrTextColor);
SetBkMode(hdcStatic, TRANSPARENT);
SetBkColor(hdcStatic,inst->clrBackColor);
g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
return (LONG)g_hbrBackground;
}
}
break;
case WM_CREATE:
{
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
inst->Create(xPos,yPos);
}
break;
case WM_MOVE:
{
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
inst->Move(xPos,yPos);
WindowStopMoving=false;
WindowStopResize=true;
}
break;
case WM_SIZE :
{
WindowStopResize=false;
WindowStopMoving=true;
}
break;
case WM_EXITSIZEMOVE:
{
if(WindowStopResize==false)
{
WindowStopResize=true;
inst->NotResize();
}
else if (WindowStopMoving==false)
{
WindowStopMoving=true;
inst->Stop();
}
}
break;
case WM_NCHITTEST:
return DefWindowProc(hwnd, msg, wParam, lParam);
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
{
SetFocus(inst->hwnd);
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
MouseButtons MBButtons;
if((wParam & MK_LBUTTON)!= false)
MBButtons=Left;
else if (wParam & MK_RBUTTON)
MBButtons=Right;
else if (wParam & MK_MBUTTON)
MBButtons=Middle;
else if (wParam & MK_XBUTTON1)
MBButtons=X1;
else if (wParam & MK_XBUTTON2)
MBButtons=X2;
else
MBButtons=None;
inst->MouseDown(MBButtons,blControl,blshift,xPos,yPos);
}
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
{
SetFocus(inst->hwnd);
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
MouseButtons MBButtons;
if (msg == WM_LBUTTONUP)
MBButtons=Left;
else if (msg == WM_RBUTTONUP)
MBButtons=Right;
else if (msg == WM_MBUTTONUP)
MBButtons=Middle;
else if (msg == WM_XBUTTONUP)
{
if (wParam & MK_XBUTTON1)
MBButtons=X1;
else if (wParam & MK_XBUTTON2)
MBButtons=X2;
}
else
MBButtons=None;
inst->MouseUp(MBButtons,blControl,blshift,xPos,yPos);
}
break;
case WM_MOUSELEAVE :
{
TrackingMouse = false;
inst->MouseLeave();
}
break;
case WM_NCMOUSEMOVE:
case WM_MOUSEMOVE:
{
if (!TrackingMouse)
{
inst->TrackMouse(hwnd);
TrackingMouse = true;
inst->MouseEnter();
}
static int xPos = (int)(short) LOWORD(lParam);
static int yPos = (int)(short) HIWORD(lParam);
static bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
static bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
static MouseButtons MBButtons;
if((wParam & MK_LBUTTON)!= false)
MBButtons=Left;
else if ((wParam & MK_RBUTTON)!= false)
MBButtons=Right;
else if ((wParam & MK_MBUTTON)!= false)
MBButtons=Middle;
else if ((wParam & MK_XBUTTON1)!= false)
MBButtons=X1;
else if ((wParam & MK_XBUTTON2)!= false)
MBButtons=X2;
else
MBButtons=None;
inst->MouseMove(MBButtons,blControl,blshift,xPos,yPos);
KillTimer(hwnd, MouseStopTimerID);
SetTimer(hwnd, MouseStopTimerID, 500, NULL);
}
break;
case WM_MOUSEHOVER:
{
inst->MouseHover();
}
break;
case WM_TIMER:
switch(wParam)
{
case MouseStopTimerID:
{
KillTimer(hwnd, MouseStopTimerID);
inst->MouseStoped();
}
}
break;
case WM_KEYUP:
inst->KeyUp(lParam,wParam);
break;
case WM_KEYDOWN:
inst->KeyDown(lParam,wParam);
break;
default:
break;
}
return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
}
public:
//is these 2 lines ok?
//see the #define on top
event(NotResize)=[](){;};
event(Stop)=[](){;};
event(MouseEnter)=[](){;};
event(MouseLeave)=[](){;};
event(Create,(int x, int y))=[](int x, int y){;};
event(Move,(int x, int y))=[](int x, int y){;};
event(MouseDown,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseUp,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseMove,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseStoped)=[](){;};
event(MouseHover)=[](){;};
event(MouseClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseDoubleClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
event(MouseWhell,(int Whell, MouseButtons Button,bool control, bool shift, int x, int y))=[](int Whell, int Button, bool alt, bool shift,int x, int y){;};
event(KeyDown,(int repeat,int Key))=[](int repeat,int Key){;};
event(KeyUp,(int repeat,int Key))=[](int repeat,int Key){;};
~label()
{
DestroyWindow(hwnd);
hwnd = 0;
}
label()
{
;
}
void setparent(HWND parent)
{
static int i=i+1;
string strclass=labelclassprop + to_string(i);
labelclassprop=strclass.c_str();
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = labelclassprop;
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
// store the old WNDPROC of the EDIT window class
// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
//SetProp(parent, labelclassprop, (HANDLE)this);
SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
// register the new window class, "ShEdit"
if (!RegisterClass(&wc))
MessageBox(NULL, "error in register", "error", MB_OK);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
labelclassprop,
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
NULL);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
UpdateWindow(hwnd);
}
Size GetSize()
{
RECT LabelSize;
GetWindowRect(this->hwnd,&LabelSize);
Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
return crdSize;
}
void SetSize(int Width, int Height)
{
RECT LabelSize;
GetWindowRect(this->hwnd,&LabelSize);
LabelSize.right=LabelSize.left+Width;
LabelSize.bottom=LabelSize.top+Height;
SetWindowPos(this->hwnd,HWND_TOP,LabelSize.left,LabelSize.top,Width,Height,SWP_NOMOVE|SWP_NOZORDER);
}
Position GetPosition()
{
RECT LabelSize;
GetWindowRect(this->hwnd,&LabelSize);
Position crdSize = {LabelSize.left,LabelSize.top};
return crdSize;
}
void SetPosition(int x, int y)
{
RECT LabelSize;
GetWindowRect(this->hwnd,&LabelSize);
SetWindowPos(this->hwnd,HWND_TOP,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
}
void Transparent(bool transparent)
{
this->blnTransparent=transparent;
}
void SetText(string text)
{
this->caption=text;
SetWindowText(this->hwnd,text.c_str());
}
string GetText()
{
return this->caption;
}
void SetBackColor(COLORREF color)
{
clrBackColor=color;
ShowWindow(this->hwnd,SW_HIDE);
ShowWindow(this->hwnd,SW_SHOW);
}
color GetBackColor()
{
return clrBackColor;
}
void SetTransparency(std::uint8_t Transperancy)
{
long wAttr = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, wAttr | WS_EX_LAYERED);
SetLayeredWindowAttributes(hwnd, 0, Transperancy, 0x2);
const char *text;
text=to_string( GetLastError()).c_str();
MessageBoxA(NULL,text,"erro",MB_OK);
}
void SetColorText(COLORREF color)
{
clrTextColor=color;
ShowWindow(hwnd,SW_HIDE);
ShowWindow(hwnd,SW_SHOW);
}
color GetColorText()
{
return clrTextColor;
}
HFONT GetFont()
{
HFONT actual;
actual=(HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
return actual;
}
void SetFont(HFONT newfont)
{
SendMessage(hwnd,WM_SETFONT,(WPARAM)newfont,0);
}
void transparent(bool bltransparent)
{
blnTransparent=bltransparent;
}
HWND GetHWND()
{
return hwnd;
}
};
No wonder! I meant you to put the SetProp(hwnd..) just before the existing UpdateWindow() function and after the If (hwnd == NULL...) test.Code:SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
//SetProp(parent, labelclassprop, (HANDLE)this);
SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
now i did my friend:
but i recive 1 messagebox with 'NULL' like a loop and i must close the application with windows tell me that the application stops workingCode:void setparent(HWND parent)
{
static int i=i+1;
string strclass=labelclassprop + to_string(i);
labelclassprop=strclass.c_str();
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = labelclassprop;
wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
// store the old WNDPROC of the EDIT window class
// store the old WNDPROC of the EDIT window class
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
SetProp(parent, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
// register the new window class, "ShEdit"
if (!RegisterClass(&wc))
MessageBox(NULL, "error in register", "error", MB_OK);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
labelclassprop,
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
NULL);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
SetProp(hwnd, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
}
Change
toCode:SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
SetProp(parent, labelclassprop, (HANDLE)this);
UpdateWindow(hwnd);
You'll find in the WndProc() that you'll now get inst to be NULL sometimes. This is because you can't set the property of the label window until after the window has been created - but some messages such as WM_CREATE are sent before you can set the property. This is going to cause you a problem as you are using the property in WM_CREATE etc!Code://SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
//SetProp(parent, labelclassprop, (HANDLE)this);
//UpdateWindow(hwnd);
I think you are going to have to pass this also via the lpParam parameter of CreateWindowsEx (the last param). You can then pick it up in the WM_CREATE message via the lParam parameter which points to a CREATESTRUCT struct which contains the value set in CreateWindow.
See
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Code:hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
labelclassprop,
labelclassprop,
SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
100, 100, 100, 100,
parent,
NULL,
mod,
(LPVOID)this);
oldproc needs to be set/get from the label parent as previously as this value doesn't change between different label windows - only inst does.Code:WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
if (oldproc == NULL)
MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
label *inst = (label*)GetProp(hwnd), labelclassprop);
if (inst == NULL && msg == WM_CREATE)
inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
if (inst == NULL)
MessageBox(NULL, "instnull, "instnull", MB_OK);
I hope you can follow this.