fork download
  1. #include <windows.h>
  2. #include <string>
  3. #include <functional>
  4. #define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname
  5.  
  6. typedef COLORREF color;
  7.  
  8. using namespace std;
  9.  
  10. const char *labelpropname = "Cambalinho";
  11. const char *labelclassprop = "classaddr";
  12.  
  13. enum MouseButtons
  14. {
  15. None=-1,
  16. Left=0,
  17. Right=1,
  18. Middle=2,
  19. X1=3,
  20. X2=4
  21. };
  22.  
  23. struct Position
  24. {
  25. int X;
  26. int Y;
  27. };
  28.  
  29. struct Size
  30. {
  31. int Width;
  32. int Height;
  33. };
  34.  
  35. class label
  36. {
  37. private:
  38. HWND hwnd;
  39. color clrTextColor=RGB(0,0,0);
  40. color clrBackColor=RGB(255,255,255);
  41. bool blnTransparent=false;
  42. string caption="label";
  43.  
  44. HRGN CreateBitmapRgn(HWND image,color transparent=-1)
  45. {
  46. int x=0, y=0;
  47. HRGN hrgn;
  48. HRGN hrgnTmp;
  49. RECT LabelSize;
  50. GetWindowRect(image,&LabelSize);
  51. int width=LabelSize.right-LabelSize.left;
  52. int height=LabelSize.bottom-LabelSize.top;
  53. hrgn = CreateRectRgn(0, 0, 0, 0);
  54. color pixelcolor=0;
  55. HDC hdcPixel=GetDC(image);
  56. for (y = 0; y < height; y++)
  57. {
  58. for (x = 0; x < width; x++)
  59. {
  60. pixelcolor=GetPixel(hdcPixel,x,y);
  61. if (pixelcolor!=transparent)
  62. {
  63. hrgnTmp = CreateRectRgn(x, y, x + 1, y + 1);
  64. CombineRgn(hrgn, hrgn, hrgnTmp, RGN_OR);
  65. DeleteObject(hrgnTmp);
  66. }
  67. }
  68. }
  69.  
  70. return hrgn;
  71. }
  72.  
  73. void TrackMouse(HWND hwnd)
  74. {
  75. TRACKMOUSEEVENT tme;
  76. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  77. tme.dwFlags = TME_HOVER | TME_LEAVE; //Type of events to track & trigger.
  78. tme.dwHoverTime = 5000; //How long the mouse has to be in the window to trigger a hover event.
  79. tme.hwndTrack = hwnd;
  80. TrackMouseEvent(&tme);
  81. }
  82.  
  83. static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  84. {
  85. WNDPROC oldproc = (WNDPROC)GetProp(GetParent(hwnd), labelpropname);
  86. if (oldproc == NULL)
  87. MessageBox(NULL, "oldprocnull", "oldprocnull", MB_OK);
  88.  
  89. label *inst = (label*)GetProp(hwnd, labelclassprop);
  90. if (inst == NULL && msg == WM_CREATE)
  91. inst = (label*)(((LPCREATESTRUCT)lParam)->lpCreateParams);
  92.  
  93. if ((inst == NULL) && (msg != WM_NCCREATE && msg != WM_NCCALCSIZE))
  94. MessageBox(NULL, "instnull", "instnull", MB_OK);
  95. static bool TrackingMouse = false;
  96. static bool WindowStopMoving = NULL;
  97. static bool WindowStopResize = NULL;
  98. static const UINT_PTR MouseStopTimerID = 1;
  99. HBRUSH g_hbrBackground = CreateSolidBrush(NULL_BRUSH);
  100.  
  101. switch(msg)
  102. {
  103. case WM_CTLCOLORSTATIC:
  104. {
  105. HDC hdcStatic = (HDC)wParam;
  106. SetTextColor(hdcStatic, inst->clrTextColor);
  107. SetBkMode(hdcStatic, TRANSPARENT);
  108. SetBkColor(hdcStatic,inst->clrBackColor);
  109. g_hbrBackground = CreateSolidBrush(inst->clrBackColor);
  110. return (LONG)g_hbrBackground;
  111. }
  112. break;
  113. case WM_CREATE:
  114. {
  115. static int xPos = (int)(short) LOWORD(lParam);
  116. static int yPos = (int)(short) HIWORD(lParam);
  117. inst->Create(xPos,yPos);
  118. }
  119. break;
  120.  
  121. case WM_MOVE:
  122. {
  123. static int xPos = (int)(short) LOWORD(lParam);
  124. static int yPos = (int)(short) HIWORD(lParam);
  125. inst->Move(xPos,yPos);
  126. WindowStopMoving=false;
  127. WindowStopResize=true;
  128. }
  129. break;
  130. case WM_SIZE :
  131. {
  132. WindowStopResize=false;
  133. WindowStopMoving=true;
  134. }
  135. break;
  136. case WM_EXITSIZEMOVE:
  137. {
  138. if(WindowStopResize==false)
  139. {
  140. WindowStopResize=true;
  141. inst->NotResize();
  142. }
  143. else if (WindowStopMoving==false)
  144. {
  145. WindowStopMoving=true;
  146. inst->Stop();
  147. }
  148. }
  149. break;
  150.  
  151. case WM_NCHITTEST:
  152. return DefWindowProc(hwnd, msg, wParam, lParam);
  153.  
  154. case WM_LBUTTONDOWN:
  155. case WM_RBUTTONDOWN:
  156. case WM_MBUTTONDOWN:
  157. case WM_XBUTTONDOWN:
  158. {
  159. SetFocus(inst->hwnd);
  160. static int xPos = (int)(short) LOWORD(lParam);
  161. static int yPos = (int)(short) HIWORD(lParam);
  162. bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
  163. bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
  164. MouseButtons MBButtons;
  165. if((wParam & MK_LBUTTON)!= false)
  166. MBButtons=Left;
  167. else if (wParam & MK_RBUTTON)
  168. MBButtons=Right;
  169. else if (wParam & MK_MBUTTON)
  170. MBButtons=Middle;
  171. else if (wParam & MK_XBUTTON1)
  172. MBButtons=X1;
  173. else if (wParam & MK_XBUTTON2)
  174. MBButtons=X2;
  175. else
  176. MBButtons=None;
  177. inst->MouseDown(MBButtons,blControl,blshift,xPos,yPos);
  178. }
  179. break;
  180. case WM_LBUTTONUP:
  181. case WM_RBUTTONUP:
  182. case WM_MBUTTONUP:
  183. case WM_XBUTTONUP:
  184. {
  185. SetFocus(inst->hwnd);
  186. static int xPos = (int)(short) LOWORD(lParam);
  187. static int yPos = (int)(short) HIWORD(lParam);
  188. bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
  189. bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
  190. MouseButtons MBButtons;
  191. if (msg == WM_LBUTTONUP)
  192. MBButtons=Left;
  193. else if (msg == WM_RBUTTONUP)
  194. MBButtons=Right;
  195. else if (msg == WM_MBUTTONUP)
  196. MBButtons=Middle;
  197. else if (msg == WM_XBUTTONUP)
  198. {
  199. if (wParam & MK_XBUTTON1)
  200. MBButtons=X1;
  201. else if (wParam & MK_XBUTTON2)
  202. MBButtons=X2;
  203. }
  204. else
  205. MBButtons=None;
  206. inst->MouseUp(MBButtons,blControl,blshift,xPos,yPos);
  207. }
  208. break;
  209. case WM_MOUSELEAVE :
  210. {
  211. TrackingMouse = false;
  212. inst->MouseLeave();
  213. }
  214. break;
  215. case WM_NCMOUSEMOVE:
  216. case WM_MOUSEMOVE:
  217. {
  218. if (!TrackingMouse)
  219. {
  220. inst->TrackMouse(hwnd);
  221. TrackingMouse = true;
  222. inst->MouseEnter();
  223. }
  224. static int xPos = (int)(short) LOWORD(lParam);
  225. static int yPos = (int)(short) HIWORD(lParam);
  226. static bool blControl = ((wParam & MK_CONTROL) == MK_CONTROL);
  227. static bool blshift = ((wParam & MK_SHIFT) == MK_SHIFT);
  228. static MouseButtons MBButtons;
  229. if((wParam & MK_LBUTTON)!= false)
  230. MBButtons=Left;
  231. else if ((wParam & MK_RBUTTON)!= false)
  232. MBButtons=Right;
  233. else if ((wParam & MK_MBUTTON)!= false)
  234. MBButtons=Middle;
  235. else if ((wParam & MK_XBUTTON1)!= false)
  236. MBButtons=X1;
  237. else if ((wParam & MK_XBUTTON2)!= false)
  238. MBButtons=X2;
  239. else
  240. MBButtons=None;
  241.  
  242. inst->MouseMove(MBButtons,blControl,blshift,xPos,yPos);
  243. KillTimer(hwnd, MouseStopTimerID);
  244. SetTimer(hwnd, MouseStopTimerID, 500, NULL);
  245. }
  246. break;
  247. case WM_MOUSEHOVER:
  248. {
  249. inst->MouseHover();
  250. }
  251. break;
  252. case WM_TIMER:
  253. switch(wParam)
  254. {
  255. case MouseStopTimerID:
  256. {
  257. KillTimer(hwnd, MouseStopTimerID);
  258. inst->MouseStoped();
  259. }
  260. }
  261. break;
  262. case WM_KEYUP:
  263. inst->KeyUp(lParam,wParam);
  264. break;
  265. case WM_KEYDOWN:
  266. inst->KeyDown(lParam,wParam);
  267. break;
  268.  
  269. default:
  270. break;
  271. }
  272.  
  273. return oldproc ? CallWindowProc(oldproc, hwnd, msg, wParam, lParam) : DefWindowProc(hwnd, msg, wParam, lParam);
  274. }
  275.  
  276.  
  277. public:
  278.  
  279. //is these 2 lines ok?
  280. //see the #define on top
  281. event(NotResize)=[](){;};
  282. event(Stop)=[](){;};
  283. event(MouseEnter)=[](){;};
  284. event(MouseLeave)=[](){;};
  285. event(Create,(int x, int y))=[](int x, int y){;};
  286. event(Move,(int x, int y))=[](int x, int y){;};
  287. event(MouseDown,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
  288. event(MouseUp,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
  289. event(MouseMove,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
  290. event(MouseStoped)=[](){;};
  291. event(MouseHover)=[](){;};
  292. event(MouseClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
  293. event(MouseDoubleClick,(MouseButtons Button,bool control, bool shift, int x, int y))=[](int Button, bool alt, bool shift,int x, int y){;};
  294. 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){;};
  295. event(KeyDown,(int repeat,int Key))=[](int repeat,int Key){;};
  296. event(KeyUp,(int repeat,int Key))=[](int repeat,int Key){;};
  297.  
  298. ~label()
  299. {
  300. DestroyWindow(hwnd);
  301. hwnd = 0;
  302. }
  303. label()
  304. {
  305. ;
  306. }
  307.  
  308. void setparent(HWND parent)
  309. {
  310. static int i = 0;
  311. i++;
  312. string strclass=labelclassprop + to_string(i);
  313. labelclassprop=strclass.c_str();
  314. WNDCLASS wc;
  315. HINSTANCE mod = (HINSTANCE)GetModuleHandle(NULL);
  316.  
  317. ZeroMemory(&wc, sizeof(WNDCLASS));
  318. GetClassInfo(mod, "STATIC", &wc);
  319.  
  320. wc.hInstance = mod;
  321. wc.lpszClassName = labelclassprop;
  322. wc.hbrBackground = CreateSolidBrush(RGB(255,0,0));
  323.  
  324. // store the old WNDPROC of the EDIT window class
  325. SetProp(parent, labelpropname, (HANDLE)wc.lpfnWndProc);
  326.  
  327. // replace it with local WNDPROC
  328. wc.lpfnWndProc = WndProc;
  329.  
  330. // register the new window class, "ShEdit"
  331. if (!RegisterClass(&wc))
  332. MessageBox(NULL, "error in register", "error", MB_OK);
  333.  
  334. hwnd = CreateWindowEx(
  335. WS_EX_LEFT| WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR | WS_EX_TRANSPARENT,
  336. labelclassprop,
  337. labelclassprop,
  338. SS_LEFT|WS_CHILD|WS_VISIBLE|WS_OVERLAPPED,
  339. 100, 100, 100, 100,
  340. parent,
  341. NULL,
  342. mod,
  343. (LPVOID)this);
  344.  
  345. if (hwnd == NULL)
  346. MessageBox(NULL, "error in create", "error", MB_OK);
  347.  
  348. SetProp(hwnd, labelclassprop, (HANDLE)this);
  349. //SetWindowLongPtr(this->hwnd,GWLP_USERDATA,(LONG) this);
  350. UpdateWindow(hwnd);
  351. clrBackColor=GetPixel(GetDC(hwnd),0,0);
  352. }
  353.  
  354. Size GetSize()
  355. {
  356. RECT LabelSize;
  357. GetWindowRect(this->hwnd,&LabelSize);
  358. Size crdSize = {LabelSize.right-LabelSize.left,LabelSize.bottom-LabelSize.top};
  359. return crdSize;
  360. }
  361.  
  362. void SetSize(int Width, int Height)
  363. {
  364. RECT LabelSize;
  365. GetWindowRect(this->hwnd,&LabelSize);
  366. LabelSize.right=LabelSize.left+Width;
  367. LabelSize.bottom=LabelSize.top+Height;
  368. SetWindowPos(this->hwnd,HWND_TOP,LabelSize.left,LabelSize.top,Width,Height,SWP_NOMOVE|SWP_NOZORDER);
  369. }
  370.  
  371. Position GetPosition()
  372. {
  373. RECT LabelSize;
  374. GetWindowRect(this->hwnd,&LabelSize);
  375. Position crdSize = {LabelSize.left,LabelSize.top};
  376. return crdSize;
  377. }
  378.  
  379. void SetPosition(int x, int y)
  380. {
  381. RECT LabelSize;
  382. GetWindowRect(this->hwnd,&LabelSize);
  383. SetWindowPos(this->hwnd,HWND_TOP,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER);
  384. }
  385.  
  386.  
  387.  
  388. void Transparent()
  389. {
  390. RECT LabelSize;
  391. GetWindowRect(this->hwnd,&LabelSize);
  392. HRGN hRgn = CreateBitmapRgn(this->hwnd, this->clrBackColor);
  393. SetWindowRgn(this->hwnd, hRgn, true);
  394. }
  395.  
  396. void SetText(string text)
  397. {
  398. this->caption=text;
  399. SetWindowText(this->hwnd,text.c_str());
  400. }
  401.  
  402. string GetText()
  403. {
  404. return this->caption;
  405. }
  406.  
  407. void SetBackColor(COLORREF color)
  408. {
  409. clrBackColor=color;
  410. ShowWindow(this->hwnd,SW_HIDE);
  411. ShowWindow(this->hwnd,SW_SHOW);
  412. }
  413.  
  414. color GetBackColor()
  415. {
  416. return clrBackColor;
  417. }
  418.  
  419. void SetColorText(COLORREF color)
  420. {
  421. clrTextColor=color;
  422. ShowWindow(this->hwnd,SW_HIDE);
  423. ShowWindow(this->hwnd,SW_SHOW);
  424. }
  425.  
  426. color GetColorText()
  427. {
  428. return this->clrTextColor;
  429. }
  430.  
  431. HFONT GetFont()
  432. {
  433. HFONT actual;
  434. actual=(HFONT)SendMessage(hwnd,WM_GETFONT,0,0);
  435. return actual;
  436. }
  437. void SetFont(HFONT newfont)
  438. {
  439. SendMessage(hwnd,WM_SETFONT,(WPARAM)newfont,0);
  440. }
  441. void SetImage(string FileName)
  442. {
  443. HBITMAP bmpSource = NULL;
  444. bmpSource = (HBITMAP)LoadImage(NULL,FileName.c_str(), IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
  445. SendMessage(this->hwnd,STM_SETICON,(WPARAM) IMAGE_ICON,(LPARAM) bmpSource);
  446. }
  447.  
  448. HWND GetHWND()
  449. {
  450. return hwnd;
  451. }
  452. };
  453.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: windows.h: No such file or directory
 #include <windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty