5 Replies - 460 Views - Last Post: 05 December 2012 - 05:35 AM Rate Topic: -----

#1 Sammdahamm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-February 12

C++ WinAPI Object problem

Posted 04 December 2012 - 10:24 AM

Hey!
I have a problem with my winAPI GUI I'm coding for my 2nd Year A-Level Computing coursework. I'm still trying to get my head around the winAPI stuff, and I'm sure It's a very simple problem I'm overlooking. The main form works fine, and just has a simple edit-box and button for testing, but when I close and open a new form, the new form takes on the characteristics I outlined in the CreateWindowEx() function (i.e a different size to the main form), but the problem is that it also contains the objects from the first form.

Here is the handle for the main form being initialised:
hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Child-minding App", /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           640,                 /* The programs width */
           480,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );



Here's the handle for the new form I'm opening:
hwndLogin = CreateWindowEx (0, szClassName, "Login Form", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 330, HWND_DESKTOP, NULL, hInst, NULL);



And here's an example of an object which appears on both forms
hwndButton = CreateWindowEx(0,                    /* more or ''extended'' styles */
                          TEXT("BUTTON"),                         /* GUI ''class'' to create */
                          TEXT("Push Me"),                        /* GUI caption */
                          WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,   /* control styles separated by | */
                          10,                                     /* LEFT POSITION (Position from left) */
                          40,                                     /* TOP POSITION  (Position from Top) */
                          200,                                    /* WIDTH OF CONTROL */
                          30,                                     /* HEIGHT OF CONTROL */
                          hwnd,                                   /* Parent window handle */
                          (HMENU)IDBUTTON,                        /* control''s ID for WM_COMMAND */
                          g_hInst,                                /* application instance */
                          NULL);




So just to clarify, when the hwnd form closes, and the hwndLogin form opens, the hwndLogin form also contains the button, wheras I on'y want the hwnd form to contain the button.
Can anyone see what I've done wrong?

(P.S. sorry if this is worded horribly.)

Is This A Good Question/Topic? 0
  • +

Replies To: C++ WinAPI Object problem

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,460
  • Joined: 20-March 10

Re: C++ WinAPI Object problem

Posted 04 December 2012 - 02:34 PM

Hi ,

This is happening because you are using the same classname as the previous window

so Windows thinks you want to instantiate a new instance of this class

There are system class names for instance a dialog for a login would be #32770

so you could define a constant call it say DIALOG and do this


hwndLogin = CreateWindowEx (0, DIALOG, "Login Form", WS_OVERLAPPEDWINDOW,
                                             CW_USEDEFAULT, CW_USEDEFAULT, 640, 330, HWND_DESKTOP,
                                              NULL, NULL, NULL);




Regards



Snoopy.
Was This Post Helpful? 1
  • +
  • -

#3 Sammdahamm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-February 12

Re: C++ WinAPI Object problem

Posted 04 December 2012 - 02:52 PM

Hi Snoopy, thanks a lot for the reply.

I'm still having a problem because I didn't quite understand what you meant by defining a constant (I assume you mean define DIALOG as 32770), I tried this (#define DIALOG 32770) and got the error "initializing argument 2 of 'HWND__* CreateWindowExA(DWORD, const CHAR*, const CHAR*, DWORD, int, int, int, int, HWND__*, HMENU__*, HINSTANCE__*, void*)'"

I also took what you said about giving a different classname, and declared (char szLoginClassName[ ] = "LoginForm";" and replaced "szClassName" in the CreateWindowEx() function, but then however, the Login window just doesn't show up when the button is clicked anymore (although the program doesn't crash)

any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,460
  • Joined: 20-March 10

Re: C++ WinAPI Object problem

Posted 04 December 2012 - 03:01 PM

No,

Its a string so

#define DIALOG "#32770"

Regards


Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 Sammdahamm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 14-February 12

Re: C++ WinAPI Object problem

Posted 05 December 2012 - 04:10 AM

Thank you very much :) it works with the "#32770" constant.
However, I don't fully understand why replacing the class name with the #32770 string works, and simply giving it a different classname doesn't? Not really important, but if you have time to explain it would be greatly appreciated :)
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 837
  • View blog
  • Posts: 2,460
  • Joined: 20-March 10

Re: C++ WinAPI Object problem

Posted 05 December 2012 - 05:35 AM

To give it a different classname

you would have to use the RegisterClass function...

you just cant use anything....

Snoopy.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1