GUI problems

I'm trying to make my GUI more object-oriented but I have some trouble. I'm trying to pass this as a parameter to SetWindowLong() function so I could use one callback function for all instances of my program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

LRESULT CALLBACK wndproc(HWND,UINT,WPARAM,LPARAM);


class GUI
{
int x;
public:

friend LRESULT CALLBACK wndproc(HWND,UINT,WPARAM,LPARAM);

void Run(HINSTANCE hi,HWND  hwnd);
bool wm_create(HWND hwnd,UINT msg ,WPARAM wp,LPARAM lp); // handle the WM_CREATE message

};

bool GUI::wm_create(HWND hwnd,UINT msg ,WPARAM wp,LPARAM lp)
  {
     MessageBox(hwnd,"It works!","",64);
     cout <<x<<endl;
  }


void GUI::Run(HINSTANCE hi,HWND  hwnd)
  {

    MSG messages;
    WNDCLASSEX wincl;

    wincl.hInstance = hi;
    wincl.lpszClassName = "mywnd";
    wincl.lpfnWndProc = wndproc;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);

    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;
    wincl.cbClsExtra = 4;
    wincl.cbWndExtra = 0;

    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           "mywnd",         /* Classname */
           "Code::Blocks Template Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hi,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

  
    ShowWindow (hwnd, 1);
    
    SetWindowLong(hwnd,GWL_USERDATA,(LONG)this);
    
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }



  }




LRESULT CALLBACK wndproc(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp)
{
GUI *gui=(GUI*)GetWindowLong(hwnd,GWL_USERDATA);

switch(msg)
  {
     case WM_CREATE:
         gui->wm_create(hwnd,msg,wp,lp);
         return 0;
  }

return DefWindowProc(hwnd,msg,wp,lp);
}



int main()
{
HWND hwnd;

GUI gui;
gui.Run(GetModuleHandle(0),hwnd);

}

Everything seems good but it crashes. I think there's something wrong with SetWindowLong() function but I can't figure out.
Any ideas what could be wrong here?

Thanks for help!
It looks like you're not handling the WM_CREATE message properly. This should get you going: http://msdn.microsoft.com/en-us/library/ff381400(VS.85).aspx

EDIT: you're also casting the this pointer to a LONG instead of a LONG_PTR.
Last edited on
Thanks for the link! It works now.
Topic archived. No new replies allowed.