Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6785415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:07:26+00:00 2026-05-26T17:07:26+00:00

I’m coming from C# and very new at this so please bear with me.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:07:27+00:00Added an answer on May 26, 2026 at 5:07 pm

    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 later SetWindowLong(hWnd, GWL_USERDATA, (long) ((LPCREATESTRUCT)lParam)->lpCreateParams); you are saying it is a pointer to class. Did you mean to have:

             hWnd = CreateWindowEx(0, TEXT("AS2MainWindow"),
                                                  TEXT("AS2"),
                                                  WS_BORDER | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
                                                  CW_USEDEFAULT, CW_USEDEFAULT,
                                                  824, 350,
                                                  0, 0,
                                                  hInstance, this);
    

    Looking for other problems. See if that helps. If not maybe your post your main()

    Added:
    CreateWindowEx

    HWND WINAPI CreateWindowEx(
      __in      DWORD dwExStyle,
      __in_opt  LPCTSTR lpClassName,
      __in_opt  LPCTSTR lpWindowName,
      __in      DWORD dwStyle,
      __in      int x,
      __in      int y,
      __in      int nWidth,
      __in      int nHeight,
      __in_opt  HWND hWndParent,
      __in_opt  HMENU hMenu,
      __in_opt  HINSTANCE hInstance,
      __in_opt  LPVOID lpParam
    );
    

    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 your WM_NCCREATE or WM_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:

    (long) ((LPCREATESTRUCT)lParam)->lpCreateParams);
    

    That is saying cast lParam to a pointer to a CREATESTRUCT. Than get lpCreateParams from it. and cast that to a long. 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).

    typedef struct tagCREATESTRUCT {
      LPVOID    lpCreateParams;
      HINSTANCE hInstance;
      HMENU     hMenu;
      HWND      hwndParent;
      int       cy;
      int       cx;
      int       y;
      int       x;
      LONG      style;
      LPCTSTR   lpszName;
      LPCTSTR   lpszClass;
      DWORD     dwExStyle;
    } CREATESTRUCT, *LPCREATESTRUCT;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have an array which has BIG numbers and small numbers in it. I
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.