Code:
#include <Windowsx.h>
#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>
#include <string.h>
#include <iomanip>
#include <functional>
#define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
typedef COLORREF color;
using namespace std;
const char propname[] = "Cambalinho";
const char classprop[] = "classaddr";
const char debugname[] = "debug.txt";
enum MouseButtons
{
None = 0,
Left = 1,
Right = 2,
Middle = 3,
X1 = 4,
X2 = 5
};
int pos = 0;
const string lab[6] = {"qwerty", "asdfgh", "zxcvbn", "poiuyt", "lkjhgf", "mnbvcx"};
class label
{
private:
HCURSOR hCursor=(HCURSOR)LoadCursor(GetModuleHandle(NULL),IDC_APPSTARTING);
string strCaption="Label ";
string strCursor="";
color clrTextColor=0;
color clrBackColor=0;
bool blnTransparent=false;
bool blnAutoSize=false;
bool blnTabStop=false;
int intTop=0;
int intLeft=0;
int intWidth=0;
int intHeight=0;
void TrackMouse(HWND hwnd, long HoverTime=0)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
tme.dwHoverTime = HoverTime; //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)
{
static bool Tracking = false;
static MouseButtons MBButtons;
static bool blControl=false;
static bool blShift=false;
WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), propname);
if (oldproc == NULL)
MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
label *inst = (label*)GetProp(hwnd, classprop);
if (inst == NULL && msg == WM_NCCREATE)
{
inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
SetProp(hwnd, classprop, (HANDLE)inst);
}
if (inst == NULL)
MessageBox(NULL, "instnull", "instnull", MB_OK);
HBRUSH g_hbrBackground = NULL;
//Working with messages
switch(msg)
{
/*case WM_CREATE:
{
inst->Create(inst->intLeft, inst->intTop);
}
break;
case WM_CTLCOLORSTATIC:
{
if ( inst->blnTransparent == true)
{
SetTextColor((HDC)wParam, inst->clrTextColor);
SetBkMode( (HDC)wParam, TRANSPARENT);
return (LRESULT) GetStockObject(HOLLOW_BRUSH);
}
else
{
DeleteObject(g_hbrBackground);
SetTextColor((HDC)wParam, inst->clrTextColor);
SetBkColor((HDC)wParam,inst->clrBackColor);
SetBkMode((HDC)wParam, TRANSPARENT);
g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
return (LONG)g_hbrBackground;
}
}
break;
case WM_PAINT:
{
if (inst->Paint==NULL) break;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(inst->hwnd, &ps);
inst->Paint(inst->hwnd,hdc);
EndPaint(inst->hwnd, &ps);
}
break;*/
case WM_NCHITTEST:
return DefWindowProc(hwnd, msg, wParam, lParam);
/*case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
{
SetFocus(inst->hwnd);
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
inst->MouseUp(MBButtons, blControl, blShift, xPos, yPos);
}
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
{
SetFocus(inst->hwnd);
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
blShift = ((wParam & MK_SHIFT) == MK_SHIFT);
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;
inst->MouseDown(MBButtons, blControl, blShift, xPos, yPos);
}
break;*/
case WM_MOUSEMOVE:
{
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
inst->setText("position: " + to_string(xPos));
}
break;
default:
break;
}
//If oldproc is NULL then use DefWindowProc - but shouldn't be!
return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
}
int intMouseHover=1000;
HWND hwnd;
bool blnVisible=true;
bool blnEnable=true;
public:
event(Create,(int x, int y))=[](int x, int y){;};
event(Paint,(HWND hwndWindow, HDC hdcWindow))=NULL;
event(ResizeStoped)=[](){;};
event(Resize)=[](){;};
event(Stoped)=[](){;};
event(Move,(int x, int y))=[](int x, int y){;};
event(MouseEnter)=[](){;};
event(MouseLeave)=[](){;};
event(MouseHover)=[](){;};
event(MouseDown,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool control, bool shift,int x, int y){;};
event(MouseUp,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool control, bool shift,int x, int y){;};
event(MouseMove,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool control, bool shift,int x, int y){;};
event(MouseStoped)=[](){;};
event(MouseClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool control, bool shift,int x, int y){;};
event(MouseDoubleClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool control, 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()
{
DestroyCursor(hCursor);
DestroyWindow(hwnd);
hwnd = 0;
}
label()
{
//nothing
}
label(HWND parent)
{
setParent(parent);
}
operator HWND()
{
return hwnd;
}
operator HDC()
{
return GetDC(hwnd);
}
HDC hdc()
{
return GetDC(hwnd);
}
HWND hWnd()
{
return hwnd;
}
void setParent(HWND parent=GetForegroundWindow())
{
if (hwnd==NULL)
{
static int i=0;
i=i+1;
strCaption=strCaption + to_string(i);
WNDCLASS wc;
HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
ZeroMemory(&wc, sizeof(WNDCLASS));
GetClassInfo(mod, "STATIC", &wc);
wc.hInstance = mod;
wc.lpszClassName = "CSTATIC";
// store the old WNDPROC of the EDIT window class
SetProp(parent, propname, (HANDLE)wc.lpfnWndProc);
// replace it with local WNDPROC
wc.lpfnWndProc = WndProc;
wc.hbrBackground = (HBRUSH) ((COLOR_WINDOW)+1);
// register the new window class, "ShEdit"
RegisterClass(&wc);
hwnd = CreateWindowEx(
WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
"CSTATIC",
strCaption.c_str(),
SS_LEFT|WS_CHILD|WS_VISIBLE|SS_LEFTNOWORDWRAP,
0, 0, 60, 30,
parent,
NULL,
mod,
(LPVOID)this);
if (hwnd == NULL)
MessageBox(NULL, "error in create", "error", MB_OK);
if (SetProp(hwnd, classprop, (HANDLE)this) == 0)
MessageBox(NULL, "set class prop error", "error", MB_OK);
clrBackColor= GetBkColor(GetDC(GetParent(hwnd)));
clrTextColor = GetTextColor(GetDC(hwnd));
}
else
{
SetParent(hwnd,parent);
}
RECT a;
GetClientRect(hwnd,&a);
intTop=a.top;
intLeft=a.left;
intWidth=a.right-a.left;
intHeight=a.bottom-a.top;
}
HWND getParent()
{
return GetParent(hwnd);
}
void setTabStop(bool tabstop)
{
blnTabStop=tabstop;
long s;
if (tabstop==false)
{
s = GetWindowLongPtr(hwnd,GWL_STYLE);
s = s & ~(WS_TABSTOP);
}
else
{
s = GetWindowLongPtr(hwnd,GWL_STYLE);
s = s | (WS_TABSTOP);
}
SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
}
bool getTabStop()
{
return blnTabStop;
}
void setMouseHover(int time)
{
intMouseHover=time;
}
int getMouseHover()
{
return intMouseHover;
}
void setMouseCursor()
{
hCursor = (HCURSOR)LoadCursor(NULL,IDC_ARROW);
SetClassLong(hwnd, // window handle
GCL_HCURSOR, // change cursor
(LONG) hCursor);
}
void setMouseCursor(long cursor)
{
hCursor = (HCURSOR)LoadCursor(NULL,(LPCSTR)cursor);
SetClassLong(hwnd, // window handle
GCL_HCURSOR, // change cursor
(LONG) hCursor);
}
HCURSOR getMouseCursor()
{
return hCursor;
}
void setCursor(string cursor)
{
strCursor=cursor;
hCursor = (HCURSOR)LoadCursor((HINSTANCE)GetModuleHandle(NULL),(LPCSTR)cursor.c_str());
SetClassLong(hwnd, // window handle
GCL_HCURSOR, // change cursor
(LONG) hCursor);
}
int getTop()
{
return intTop;
}
void setTop(int top)
{
intTop=top;
SetWindowPos(hwnd, 0,intLeft , intTop, intWidth, intHeight,
SWP_NOZORDER| SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
int getLeft()
{
return intLeft;
}
void setLeft(int left)
{
intLeft=left;
SetWindowPos(hwnd, 0,intLeft , intTop, intWidth, intHeight,
SWP_NOZORDER| SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
int getWidth()
{
return intWidth;
}
void setWidth(int width)
{
if (blnAutoSize==false)
{
intWidth=width;
SetWindowPos(hwnd, 0,intLeft , intTop, intWidth, intHeight,
SWP_NOZORDER|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
}
int getHeight()
{
return intHeight;
}
void setHeight(int height)
{
if (blnAutoSize==false)
{
intHeight=height;
SetWindowPos(hwnd, 0,intLeft , intTop, intWidth, intHeight,
SWP_NOZORDER|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
}
bool getTransparent()
{
return blnTransparent;
}
void setTransparent(bool transparent)
{
blnTransparent = transparent;
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
bool getAutoSize()
{
return blnAutoSize;
}
HFONT getFont()
{
return (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
}
void setFont(HFONT font)
{
SendMessage(hwnd, WM_SETFONT,(WPARAM) font, true);
}
void setEnable(bool enable)
{
blnEnable=enable;
EnableWindow(hwnd,enable);
}
bool getEnable()
{
return blnEnable;
}
void setVisible(bool visible)
{
blnVisible=visible;
if (visible==true)
{
ShowWindow(hwnd,SW_SHOW);
}
else
{
ShowWindow(hwnd,SW_HIDE);
}
}
void setAutoSize(bool autosize)
{
blnAutoSize=autosize;
if (autosize==true)
{
//Getting the DC Font amd select it
HDC hdc = GetDC(hwnd);
HFONT hFont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
//Getting the
RECT c = { 0, 0, 0, 0 };
//Getting the text rectangle from actual caption
char a[256];
GetWindowText(this->hwnd,a,256);
DrawText(hdc, a, strlen(a), &c, DT_CALCRECT | DT_EXPANDTABS);
//add 2 to the size
TEXTMETRIC v;
GetTextMetrics (hdc,&v);
c.right=c.right+(v.tmAveCharWidth / 2);
c.bottom=c.bottom+(v.tmAveCharWidth / 2);
//Clean the Font DC
ReleaseDC(hwnd,hdc);
//Getting the actual styles
LONG s=GetWindowLongPtr(hwnd,GWL_EXSTYLE);
LONG g=GetWindowLongPtr(hwnd,GWL_STYLE);
//change the rectangle size for borders
AdjustWindowRectEx (&c,g,FALSE,s );
//Update the control
SetWindowPos(hwnd, 0, 0, 0, c.right, c.bottom,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
}
void setBorder(int border)
{
long s=0;
if(border==0)
{
s = GetWindowLongPtr(hwnd,GWL_EXSTYLE);
s = s & ~(WS_EX_STATICEDGE) & ~(WS_EX_CLIENTEDGE);
SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
s = GetWindowLongPtr(hwnd,GWL_STYLE);
s = s & ~(WS_BORDER);
SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
}
else if(border==1)
{
s = GetWindowLongPtr(hwnd,GWL_EXSTYLE) | WS_EX_CLIENTEDGE;
SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
}
else if(border==2)
{
s = GetWindowLongPtr(hwnd,GWL_STYLE) | WS_BORDER;
SetWindowLongPtr(hwnd,GWL_STYLE,(LONG_PTR)s);
}
else if(border==3)
{
s = GetWindowLongPtr(hwnd,GWL_EXSTYLE) | WS_EX_STATICEDGE;
SetWindowLongPtr(hwnd,GWL_EXSTYLE,(LONG_PTR)s);
}
//the SetWindowPos() is for update the control
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
string getText()
{
return strCaption;
}
void setText(string text)
{
char* chrText = (char*)text.c_str();
strCaption=text;
SetWindowText(hwnd, chrText);
if (blnAutoSize==true)
{
setAutoSize(true);
}
else
{
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
}
color getBackColor()
{
return clrBackColor;
}
void setBackColor(color backcolor)
{
clrBackColor=backcolor;
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
color getTextColor()
{
return clrTextColor;
}
void setTextColor(color textcolor)
{
clrTextColor=textcolor;
SetWindowPos(hwnd, 0, 0, 0, 0, 0,
SWP_NOZORDER|SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|
SWP_DRAWFRAME | SWP_FRAMECHANGED|SWP_NOCOPYBITS);
}
};
please anyone tell me something.. i don't understand why the error... it's 'far' from me