I'm coming from C# and very new at this so please bear with me.
I have a MainWindow
class which has some private HWND
variables. One for the window itself, and one for each of the controls. I assume I need to keep track of them, or that it will make things easier later?
Anyway, I've got:
class GUIMain
{
private:
HINSTANCE hInstance;
HWND hWnd; // The windows itself
HWND cmdGenerate, cmdQuit; // 2 buttons
I've got a private method called initialise(HWND hWnd)
which is called on WM_CREATE
and it adds all the controls to the window:
void MainWindow::initialise(HWND hWnd)
{
this->hWnd = hWnd;
cmdGenerate = CreateWindow(TEXT("BUTTON"), TEXT("&Generate..."),
WS_VISIBLE | WS_CHILD,
6, 6, 150, 25,
hWnd, (HMENU)1, 0, 0);
cmdQuit = CreateWindow(TEXT("BUTTON"), TEXT("&Quit"),
WS_VISIBLE | WS_CHILD,
6, 37, 150, 25,
hWnd, (HMENU)2, 0, 0);
}
however this does not seem to put the buttons on the window. In fact, when I debug I can see that it's not even getting past the first line. What is strange is that when I change it to this:
void MainWindow::initialise(HWND hWnd)
{
//this->hWnd = hWnd;
/*cmdGenerate = */CreateWindow(TEXT("BUTTON"), TEXT("&Generate..."),
WS_VISIBLE | WS_CHILD,
6, 6, 150, 25,
hWnd, (HMENU)1, 0, 0);
/*cmdQuit = */CreateWindow(TEXT("BUTTON"), TEXT("&Quit"),
WS_VISIBLE | WS_CHILD,
6, 37, 150, 25,
hWnd, (HMENU)2, 0, 0);
}
it seems to work fine.
Logic would seem to suggest that assigning the private HWND
variables the value of the CreateWindow
function return is causing problems, but I have done this before and not had a problem?
The only difference between my previous code and this code is that I am now using classes whereas before (while I was learning) I just had everything in WinMain
and WndProc
.
WinMain: http://pastebin.com/j54vW9gc
Header File: http://pastebin.com/cUs4vVJ6
CPP File: http://pastebin.com/B5KUXTvx
Welcome to world of win32 that was not designed for C++. That's a good first try. I redid classes trying to make a generic framework hundreds of times before saying it was not worth any more time.
Your WinMain() would also be helpful, but a big issue i see is your call to
CreateWindowEx()
. The last parameter you send is 0. Than when you retrieve it laterSetWindowLong(hWnd, GWL_USERDATA, (long) ((LPCREATESTRUCT)lParam)->lpCreateParams);
you are saying it is a pointer to class. Did you mean to have:Looking for other problems. See if that helps. If not maybe your post your main()
Added: CreateWindowEx
The last parameter
lpParam
is optional. So when you had it set to 0 it was not hurting anything. But this is how you "send" something to yourWM_NCCREATE
orWM_CREATE
. It can be any LPVOID. In C you may send a pointer to a struct or anything you want. In this case you want to send it a pointer to the object that is about your window.To get this parameter in WM_NCCREATE or WM_CREATE you use the below code:
That is saying cast
lParam
to apointer
to aCREATESTRUCT
. Than getlpCreateParams
from it. and cast that to along
. This is slightly different than how i have written this hard to understand piece of code. If you break it into several steps it looks easier. Let me know if you need further explanation here.Just so you get the full picture below is the definition of CreateStruct. It has more than just lpCreateParams in it. (which you chose to be a pointer to your class).
After understanding all this. Check out ATL thunking. Its the way to go if you want all your code inside classes. I find it better to get away from EVERY piece of code being in a class when it doesn't have to be. Depends on the program I am writing.