Advertisement
Guest User

meh

a guest
Oct 21st, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include "Form.hpp"
  2. #include "MouseListener.hpp"
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. class Form : public BaseForm
  7. {
  8.     private:
  9.         std::function<void(HWND, HDC)> onPaint;
  10.         std::function<LRESULT(HWND, HDC)> onBackgroundErase;
  11.  
  12.     public:
  13.         Form(const char *Title, DWORD flags, DWORD flagsex, POINT Location, unsigned short Width, unsigned short Height, HWND Parent = NULL);
  14.         LRESULT WinProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);
  15.  
  16.         void setBackgroundListener(const std::function<LRESULT(HWND, HDC)>& onBackgroundErase) {this->onBackgroundErase = onBackgroundErase;}
  17.         void setPaintListener(const std::function<void(HWND, HDC)>& onPaint) {this->onPaint = onPaint;}
  18. };
  19.  
  20. Form::Form(const char *Title, DWORD flags, DWORD flagsex, POINT Location, unsigned short Width, unsigned short Height, HWND Parent)
  21. {
  22.     if (flags == 0)
  23.         flags = WS_OVERLAPPEDWINDOW | WS_TABSTOP;
  24.     BaseForm::Create(flagsex, Title, flags, Location, Width, Height, Parent);
  25. }
  26.  
  27. LRESULT Form::WinProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  28. {
  29.     if (onBackgroundErase && Msg == WM_ERASEBKGND)
  30.     {
  31.         return onBackgroundErase(Hwnd, (HDC)wParam);
  32.     }
  33.  
  34.     switch(Msg)
  35.     {
  36.         case WM_PAINT:
  37.         {
  38.             PAINTSTRUCT ps = {0};
  39.             HDC hdc = BeginPaint(Hwnd, &ps);
  40.  
  41.             if (onPaint)
  42.             {
  43.                 onPaint(Hwnd, hdc);
  44.             }
  45.  
  46.             EndPaint(Hwnd, &ps);
  47.         }
  48.         break;
  49.  
  50.         case WM_DESTROY:
  51.             PostQuitMessage(0);
  52.             return 0;
  53.  
  54.         default:
  55.             return DefWindowProc(Hwnd, Msg, wParam, lParam);
  56.     }
  57.     return 0;
  58. }
  59.  
  60. int main()
  61. {
  62.     Form parent("Parent", 0, 0, {CW_USEDEFAULT, CW_USEDEFAULT}, 500, 500);
  63.     parent.setBackgroundListener([](HWND hwnd, HDC dc) -> LRESULT{
  64.         SetBkColor(dc, RGB(0xFF, 0xFF, 0xFF));
  65.         return true;
  66.     });
  67.  
  68.     HBITMAP hbmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), "test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  69.  
  70.     std::thread([&] {
  71.         while(parent.GetHWND() == NULL && !IsWindowVisible(parent.GetHWND()))
  72.         {
  73.             std::this_thread::sleep_for(std::chrono::milliseconds(0));
  74.         }
  75.  
  76.         Form child("Baby", WS_OVERLAPPEDWINDOW, WS_EX_LAYERED, {CW_USEDEFAULT, CW_USEDEFAULT}, 500, 500, parent.GetHWND());
  77.         SetLayeredWindowAttributes(child.GetHWND(), RGB(0x80, 0x00, 0xFF), 0, LWA_COLORKEY);
  78.  
  79.         child.setPaintListener([&](HWND hwnd, HDC dc) {
  80.             BITMAP bitmap;
  81.             HDC hdcMem = CreateCompatibleDC(dc);
  82.             HGDIOBJ old = SelectObject(hdcMem, hbmp);
  83.  
  84.             GetObject(hbmp, sizeof(bitmap), &bitmap);
  85.             BitBlt(dc, 150, 100, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
  86.             SelectObject(hdcMem, old);
  87.             DeleteDC(hdcMem);
  88.         });
  89.  
  90.         child.Show();
  91.     }).detach();
  92.  
  93.     int res = parent.Show();
  94.     DeleteObject(hbmp);
  95.     return res;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement