Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Form.hpp"
- #include "MouseListener.hpp"
- #include <thread>
- #include <chrono>
- class Form : public BaseForm
- {
- private:
- std::function<void(HWND, HDC)> onPaint;
- std::function<LRESULT(HWND, HDC)> onBackgroundErase;
- public:
- Form(const char *Title, DWORD flags, DWORD flagsex, POINT Location, unsigned short Width, unsigned short Height, HWND Parent = NULL);
- LRESULT WinProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);
- void setBackgroundListener(const std::function<LRESULT(HWND, HDC)>& onBackgroundErase) {this->onBackgroundErase = onBackgroundErase;}
- void setPaintListener(const std::function<void(HWND, HDC)>& onPaint) {this->onPaint = onPaint;}
- };
- Form::Form(const char *Title, DWORD flags, DWORD flagsex, POINT Location, unsigned short Width, unsigned short Height, HWND Parent)
- {
- if (flags == 0)
- flags = WS_OVERLAPPEDWINDOW | WS_TABSTOP;
- BaseForm::Create(flagsex, Title, flags, Location, Width, Height, Parent);
- }
- LRESULT Form::WinProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
- {
- if (onBackgroundErase && Msg == WM_ERASEBKGND)
- {
- return onBackgroundErase(Hwnd, (HDC)wParam);
- }
- switch(Msg)
- {
- case WM_PAINT:
- {
- PAINTSTRUCT ps = {0};
- HDC hdc = BeginPaint(Hwnd, &ps);
- if (onPaint)
- {
- onPaint(Hwnd, hdc);
- }
- EndPaint(Hwnd, &ps);
- }
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- default:
- return DefWindowProc(Hwnd, Msg, wParam, lParam);
- }
- return 0;
- }
- int main()
- {
- Form parent("Parent", 0, 0, {CW_USEDEFAULT, CW_USEDEFAULT}, 500, 500);
- parent.setBackgroundListener([](HWND hwnd, HDC dc) -> LRESULT{
- SetBkColor(dc, RGB(0xFF, 0xFF, 0xFF));
- return true;
- });
- HBITMAP hbmp = (HBITMAP)LoadImage(GetModuleHandle(NULL), "test.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- std::thread([&] {
- while(parent.GetHWND() == NULL && !IsWindowVisible(parent.GetHWND()))
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(0));
- }
- Form child("Baby", WS_OVERLAPPEDWINDOW, WS_EX_LAYERED, {CW_USEDEFAULT, CW_USEDEFAULT}, 500, 500, parent.GetHWND());
- SetLayeredWindowAttributes(child.GetHWND(), RGB(0x80, 0x00, 0xFF), 0, LWA_COLORKEY);
- child.setPaintListener([&](HWND hwnd, HDC dc) {
- BITMAP bitmap;
- HDC hdcMem = CreateCompatibleDC(dc);
- HGDIOBJ old = SelectObject(hdcMem, hbmp);
- GetObject(hbmp, sizeof(bitmap), &bitmap);
- BitBlt(dc, 150, 100, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);
- SelectObject(hdcMem, old);
- DeleteDC(hdcMem);
- });
- child.Show();
- }).detach();
- int res = parent.Show();
- DeleteObject(hbmp);
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement