Advertisement
Guest User

Untitled

a guest
May 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string>
  3. #include <map>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class WinControl {
  9. public:
  10.     HWND Handle;
  11.     WinControl::WinControl() : Handle(0) {}
  12.     std::string getCaption() {
  13.         char Text[255];
  14.         GetWindowText(Handle, Text, 255);
  15.         return std::string(Text);
  16.     }
  17.  
  18.     void setCaption(std::string NewVal) {
  19.         SetWindowText(Handle, NewVal.c_str());
  20.     }
  21.     inline HWND getHandle() const {
  22.         return this->Handle;
  23.     }
  24. };
  25.  
  26. class Form: public WinControl {
  27. private:
  28.     HINSTANCE hInst;
  29. public:
  30.     int ID;
  31.     int getNextID() {
  32.         return ID++;
  33.     }
  34.     std::vector<WinControl*> controls;
  35.     Form::Form(HINSTANCE hInst, int W, int H, char * Caption);
  36.     static LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam,
  37.             LPARAM lparam);
  38.  
  39.     bool Show();
  40.     inline HINSTANCE getInstance() const {
  41.         return this->hInst;
  42.     }
  43.     void OnCreate() {};
  44.     void OnDestroy() {};
  45.     void OnLButtonDown(int X, int Y) {};
  46. };
  47.  
  48. class Control: public WinControl {
  49. public:
  50.     Control::Control(Form* parent, int x, int y, int w, int h,
  51.             std::string caption, std::string class_name, DWORD styles,
  52.             DWORD styles_ex = 0) {
  53.         int ID = parent->getNextID();
  54.         if (styles_ex > 0) {
  55.             Handle = CreateWindowEx(styles_ex, class_name.c_str(),
  56.                     caption.c_str(), styles, x, y, w, h, parent->getHandle(),
  57.                     (HMENU) ID, parent->getInstance(), NULL);
  58.         } else {
  59.             Handle = CreateWindow(class_name.c_str(), caption.c_str(), styles, x,y,w,h, parent->getHandle(), (HMENU)ID, parent->getInstance(), NULL);
  60.         }
  61.     }
  62. };
  63.  
  64. class Edit : public Control
  65. {
  66. public:
  67.     Edit::Edit(Form* parent, int x, int y, int w, int h, std::string caption)
  68.         : Control(parent, x, y, w, h, caption, "EDIT", ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE, WS_EX_CLIENTEDGE)
  69.           {
  70.           }
  71. };
  72.  
  73. class Global {
  74. public:
  75.     static std::map<std::string, Form*> handle_map;
  76. };
  77.  
  78. std::map<std::string, Form*> Global::handle_map;
  79.  
  80. bool Form::Show() {
  81.     MSG msg;
  82.     ShowWindow(Handle, SW_SHOWNORMAL);
  83.     UpdateWindow(Handle);
  84.     while (GetMessage(&msg, NULL, 0, 0) > 0) {
  85.         TranslateMessage(&msg);
  86.         DispatchMessage(&msg);
  87.     }
  88.     return true;
  89. }
  90.  
  91. Form::Form(HINSTANCE hInst, int W, int H, char * Caption) :
  92.     hInst(hInst), ID(0) {
  93.     this->hInst = hInst;
  94.     WNDCLASS wc;
  95.     wc.style = CS_HREDRAW | CS_VREDRAW;
  96.     wc.lpfnWndProc = WindowProc;
  97.     wc.cbClsExtra = 0;
  98.     wc.cbWndExtra = 0;
  99.     wc.hInstance = hInst;
  100.     wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
  101.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  102.     wc.hbrBackground = (HBRUSH) COLOR_WINDOW;
  103.     wc.lpszMenuName = NULL;
  104.     wc.lpszClassName = Caption;
  105.  
  106.     assert(RegisterClass(&wc));
  107.  
  108.     Global::handle_map[std::string(Caption)] = this;
  109.  
  110.     Handle = CreateWindow(Caption, Caption,
  111.                 WS_OVERLAPPEDWINDOW,
  112.                 CW_USEDEFAULT,CW_USEDEFAULT, W, H,
  113.                 NULL,NULL,hInst,NULL);
  114.     assert(Handle);
  115. }
  116.  
  117. LRESULT CALLBACK Form::WindowProc(HWND hwnd, UINT msg, WPARAM wparam,
  118.         LPARAM lparam)
  119. {
  120.     char ClassName[100];
  121.     GetClassName(hwnd, ClassName, 100);
  122.     Form*Sender = Global::handle_map[ClassName];
  123.  
  124.     if (!Sender) {
  125.         printf("Handle: %d\n", hwnd);
  126.         return DefWindowProc(hwnd, msg, wparam, lparam);
  127.     }
  128.  
  129.     switch (msg) {
  130.     case WM_SHOWWINDOW:
  131.         printf("WM_SHOWWINDOW: %s\n", Sender->getCaption().c_str());
  132.         break;
  133.  
  134.     case WM_CREATE:
  135.         Sender->OnCreate();
  136.         break;
  137.     case WM_DESTROY:
  138.         Sender->OnDestroy();
  139.         Global::handle_map.erase(ClassName);
  140.         PostQuitMessage(0);
  141.         break;
  142.     case WM_LBUTTONDOWN:
  143.         puts("Klik");
  144.         printf("WM_LBUTTONDOWN: %s\n", Sender->getCaption().c_str());
  145.         Sender->OnLButtonDown(LOWORD(lparam), HIWORD(lparam));
  146.         break;
  147.  
  148.     default:
  149.         return DefWindowProc(hwnd, msg, wparam, lparam);
  150.     }
  151.     return 0;
  152. }
  153.  
  154. class MainForm : public Form
  155. {
  156. public:
  157.     MainForm::MainForm(HINSTANCE hInst)
  158.     : Form(hInst, 320, 240, "No hej xD")
  159.     {
  160.         puts("MainForm()");
  161.     }
  162.     void OnCreate()
  163.     {
  164.         Form::OnCreate();
  165.         puts("WM_CREATE2");
  166.         this->setCaption("Zmienilem captiona xDxD");
  167.     }
  168.     void OnDestroy()
  169.     {
  170.         puts("Destroy");
  171.     }
  172.     void OnLButtonDown(int X, int Y)
  173.     {
  174.         printf("Left Button Down (%d, %d)\n", X, Y);
  175.     }
  176. };
  177.  
  178. int WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, int nCmdShow) {
  179.  
  180.     Form f = MainForm(hInst);
  181.     return f.Show();
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement