Solved

C++ Language question

Posted on 1997-09-09
9
505 Views
Last Modified: 2008-02-26
I'm quite new to windows-programming and trying to figure out, how to create a window from a dialog-based application with visual c++ 4.0.

Creating the Dialog-window and subsequent dialog-windows is no problem, but after clicking a button, I want some kind of depend window (for some graphics output) to be displayed . I tried with CreateWindow(...) as well as with CreateWindowEx(...), but no handle is returned, only a NULL-pointer. And the help-files suggest to get information on the possible error by calling  GetLastError(). Nice thing, but it returns nothing. No, it returns a value, but that one isn't either not a member in the error code list, or it is, but means something like "File not found".
And the most interresting thing is that if you set the error to 0 before calling CreateWindow by SetLastError(0), after CreateWindow GetLastError()still returns 0. But no handle is there.

Has anyone an idea how to solve this problem, I'm shure it's just a simple one, but sometimes the simple ones are the worst.

Thanks for any help

Yours

Heiko
0
Comment
Question by:elrond
9 Comments
 
LVL 4

Expert Comment

by:md041797
ID: 1169269
Post the code for the CreateWindow call.  What are you using for lpClassName?
0
 

Author Comment

by:elrond
ID: 1169270
Here is the source code:

LONG FAR PASCAL test(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return TRUE;
}

CEcgtestDlg::MakeWindow()
{
      HWND Child=NULL;
      LPCTSTR lpWindowName;
      LPCTSTR lpClassName;
      char out[20];

    int  x=CW_USEDEFAULT;      //horizontal position of window
      int  y=CW_USEDEFAULT;      // vertical position of window
    int  nWidth=CW_USEDEFAULT;      // window width
    int  nHeight=CW_USEDEFAULT;      // window height
      HINSTANCE hInstance;
      WNDCLASS wndclass;

      lpWindowName="foo_window";
      lpClassName="foo";
      
      hInstance=AfxGetInstanceHandle( );

            wndclass.style      = CS_HREDRAW | CS_VREDRAW;
        wndclass.lpfnWndProc= test;
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hInstance  = hInstance;
        wndclass.hIcon      = NULL; /*LoadIcon (hInstance, AppName);*/
        wndclass.hCursor    = LoadCursor (NULL, IDC_ARROW);
        wndclass.hbrBackground  = HBRUSH(COLOR_WINDOW);//GetStockObject (WHITE_BRUSH);
        wndclass.lpszMenuName   = NULL;//(HMENU)IDR_MENU1;
        wndclass.lpszClassName  = "foo";//lpWindowName;
        if(RegisterClass (&wndclass)==NULL)
                  MessageBox("registering failed","error",MB_OK);
      

      SetLastError(0);      
            sprintf(out,"%d",GetLastError());
            MessageBox(out,"error",MB_OK);

      Child=CreateWindowEx(          // without 'Ex' next line has to be omitted
       WS_EX_OVERLAPPEDWINDOW,
       "foo",//lpClassName,      // pointer to registered class name
     "foo_window",//lpWindowName,      // pointer to window name
     WS_POPUP,      // window style
      10,      // horizontal position of window
      20,      // vertical position of window
      300,      // window width
      300,      // window height
      m_hWnd,      // handle to parent or owner window
      NULL, //HMENU(7777777),      // handle to menu or child-window identifier
        hInstance,      // handle to application instance
      NULL       // pointer to window-creation data
   );
            DWORD help;
            help=GetLastError();

            sprintf(out,"0x%x",help);
      if (Child==NULL)
      {
            MessageBox("couldn't create window","error",MB_OK|MB_ICONSTOP);
            MessageBox(out,"error",MB_OK);
            exit(-1);
      }

      ShowWindow( NULL);     /* Display main window      */
    UpdateWindow();
      
      return 0;
}


0
 

Expert Comment

by:DrGoldie
ID: 1169271
You can even create a window from a console application...
Just do it like this:

long FAR PASCAL myWindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{   return DefWindowProc(hWnd, message, wParam, lParam);   }

HWND myCreateAWindow(HINSTANCE hInstance)
{
HWND            hWnd;
WNDCLASS      wc;
char*            name = "myWindow";

hInstance = NULL;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = myWindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = NULL;
wc.lpszMenuName = name;
wc.lpszClassName = name;
RegisterClass( &wc );

hWnd = CreateWindowEx(WS_EX_TOPMOST, name, name,
              WS_POPUP, 0,0, 0,0,
              NULL, NULL, hInstance, NULL);

ShowWindow(hWnd, SW_SHOW);

return hWnd;
}

If you have a HINSTANCE remark the 'hInstance = NULL;'
Should do it so...

0
 

Author Comment

by:elrond
ID: 1169272
Sorry, but I think it's not completely what I wanted, because the Window should be dependend, which means I have to submit a handler to the owner-window which is the dialog-based-application's main window. But I can't access it from outside the class (which is CEcgtestDlg).

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Expert Comment

by:Rameesh
ID: 1169273
Try this in your window procedure:LONG FAR PASCAL test(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hWnd, Message, wParam, lParam);
}The problem in your code: The return value of WM_CREATE message, which should be 0 to tell windows to continue creating the window, is being returned as non-zero (TRUE), which fails window creation.Hope this helps!
0
 

Author Comment

by:elrond
ID: 1169274
Dear Rameesh,
this is true, as DrGoldie already stated, but this is only one point of my problem. The main thing is that I need a dependent window (a window that doesn't show up on the task bar) - means the hWnd (which is NULL in DrGoldie's answer) has to be the window-handler of the main-application (which is the dialog-based one and does allow access to this variable (m_hWnd) only inside classfunctions.
0
 
LVL 1

Expert Comment

by:andla
ID: 1169275
This example demonstrate how to create as many windows you like that is not dialogwindows. I hope it is of some help.
If you have have a samplecode when using ID_  hMenu in CreateWindowEx please write to me: andla@tc.se

Here is the code.

//Resource file:

#include "resource.h"

#include "windows.h"


IDD_DIALOG1 DIALOG DISCARDABLE  0, 0, 186, 94
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
    DEFPUSHBUTTON   "OK",IDOK,129,7,50,14
    PUSHBUTTON      "Cancel",IDCANCEL,129,24,50,14
    PUSHBUTTON      "New window",ID_DINKNAPP,27,20,76,56
    CTEXT           "Text",IDC_STATIC,22,6,97,11,SS_SUNKEN | NOT
                    WS_GROUP | WS_TABSTOP
END

IDR_ACCELERATOR1 ACCELERATORS DISCARDABLE
BEGIN
    "A",            GET_KEY,                VIRTKEY, CONTROL, NOINVERT
END



//Headerfile:
#define GET_KEY                                                                              1002
#define IDC_STATIC                                                                  1001
#define ID_DINKNAPP                     1000
#define IDS_STATISK                                                                  2222
//Remember to put a blank line under this text.




//Programfile:

#include <windows.h>

#include "resource.h"
HWND hwdialog;

LRESULT CALLBACK FonsterProcedur(HWND,UINT,WPARAM,LPARAM);
BOOL CALLBACK DialogProc(HWND,UINT,WPARAM,LPARAM);
HWND hwndw;//This one is global
WINAPI WinMain(HINSTANCE denna,HINSTANCE innan,LPSTR cmdln,int style)
      {

      WNDCLASSEX wcw;
      wcw.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
      wcw.hIcon=LoadIcon(0,IDI_APPLICATION);
      wcw.hIconSm=LoadIcon(0,IDI_APPLICATION);
      wcw.hCursor=LoadCursor(0,IDC_CROSS);
      wcw.lpfnWndProc=FonsterProcedur;
      wcw.lpszMenuName=0;
      wcw.lpszClassName="hejdu";
      wcw.cbSize=sizeof(WNDCLASSEX);
      wcw.cbClsExtra=0;
      wcw.cbWndExtra=0;
      wcw.style=CS_DBLCLKS;
      wcw.hInstance=denna;

      if(!RegisterClassEx(&wcw)) return 0;
      

      hwndw=CreateWindowEx
            (
            0,
            "hejdu",
            "Andy",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            HWND_DESKTOP,
            0,
            denna,
            0
            );
      ShowWindow(hwndw,style);
HACCEL haccel;
haccel=LoadAccelerators(denna,"IDR_ACCELERATOR1");

      MSG msg;
      while(GetMessage(&msg,0,0,0))
            {
      //IsDialogMessage() har egen TranslateMessage() och
      //DispatchMessage(), därför kan det uppstå en bugg
      //när man skickar funktionen vidare till TranslateMessage
      //och DispatchMessage. Detta märker man när man kör
      //programmet och flyttar dialogrutorna.
      //Lösning på detta problem kan vara att skriva så här istället:

           
                  if(IsDialogMessage(hwdialog,&msg));
                  
                  else
                  if(TranslateAccelerator(hwndw,haccel,&msg));
                  
                  else

                  {
                  TranslateMessage(&msg);
                  DispatchMessage(&msg);
                  }

      

                  

            }
      return msg.wParam;
      }

HWND codefun;
//Using global so it doesent erase when i quit
//the dialogbox.
LRESULT CALLBACK FonsterProcedur(HWND hw,UINT msg,WPARAM wp,LPARAM lp)
      {
      HINSTANCE hi;

      
            switch(msg)
                  {
                  case WM_DESTROY:
                        PostQuitMessage(0);
                        break;
                  case WM_COMMAND:
                        //static char pek[2];
                        //pek[0]=(char)wp;
                        //pek[1]='\0';
                        if (LOWORD(wp)&GET_KEY)
                        {
                              SetWindowText(hw,"Skapar en ny dialogruta");
      
                    hi = (HINSTANCE)GetWindowLong(hw,GWL_HINSTANCE);
                        hwdialog=CreateDialog(hi,"IDD_DIALOG1",hw,(DLGPROC)DialogProc);

                        ShowWindow(hwdialog,SW_SHOW);
                        }
                        break;
                  case WM_LBUTTONDOWN:
                        hi = (HINSTANCE)GetWindowLong(hw,GWL_HINSTANCE);
                        hwdialog=CreateDialog(hi,"IDD_DIALOG1",hw,(DLGPROC)DialogProc);

                        ShowWindow(hwdialog,SW_SHOW);
                        
                  break;
                  
                  default:
                        return DefWindowProc(hw,msg,wp,lp);
                  }
     return 0;
      }

BOOL CALLBACK DialogProc(HWND hwdlg,UINT msg,WPARAM wp,LPARAM lp)      
      {
      HWND hwnd;
      HINSTANCE hi;
      
      static HWND n[500];
      int v;
      int modal;
      static int test;
      switch(msg)
            {      
            

            case WM_COMMAND:
                  switch(LOWORD(wp))
                        {
                        case IDOK:
                              MessageBox(hwdlg,"Du tryckte på OK","OK",0);
                              break;
                        case IDCANCEL:
                              MessageBox(hwdlg,"Du tryckte på cancel","cancel",0);
                                    break;
                        case ID_DINKNAPP:





//codefun is a global HWND.
                              
                              codefun=CreateWindowEx(
                                    0,
                                    "hejdu",//use the registered hejdu i wrote in WinMain;
                                    //If you build a class here in dialobox you can only
                                    //build one extra window.
                                    "Test",
                                    WS_OVERLAPPEDWINDOW,//WS_OVERLAPPEDWINDOW
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    CW_USEDEFAULT,
                                    hwndw,//You can use hwndw and hwdlg;
                                    0,//This one MUST!!       be zero in this example.
//zero = You use classmenu;
//(HMENU)number=You use ID-CHILDWINDOW (i don't know how).
                                    (HINSTANCE)GetWindowLong(hwdlg,GWL_HINSTANCE),//it can be sero
                                    NULL);

                                    ShowWindow(codefun,SW_SHOW);
                              break;
                        case IDC_STATIC:
                              MessageBox(hwdlg,"Du klickade på texten.","Centrerad text.",0);
                              break;
                        }
             break;
                  case WM_INITDIALOG:
                        switch (lp)
                        {
                        case 44:
                              SetWindowText(hwdlg,"Hurra det lyckades");
                              for(v=0;v<500;v++)
                              {
                                    if(n[v]==0)
                                    {
                                      n[v]=hwdlg;
                                          break;
                                    }
                              }
                              break;

                        }
                        break;
            case WM_RBUTTONDOWN:
                  //EndDialog(hwdlg,wp);//Denna rad ska du använda i detta program.
                  
                  modal=0;
                  for(v=0;v<500;v++)
                  {
                        if(n[v]==hwdlg)
                        {
                              n[v]=0;
                              modal=1;
                              break;
                        }

                  }
                  if (modal==0)
                  {
                        SetWindowText(GetParent(hwdlg),_itoa(test++,"",10));
                        DestroyWindow(hwdlg); //Används vid ickemodala fönster
                  }
                  else if(modal==1)
                  {
                        SetWindowText(GetParent(hwdlg),_itoa(v,"",10));
                        EndDialog(hwdlg,wp);
                  }
            
                  break;
            case WM_LBUTTONDOWN:
                  hwnd=GetParent(hwdlg);
                  hi=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
                  //hwdialog=CreateDialogParam(hi,"IDD_DIALOG1",hwdlg,(DLGPROC)DialogProc,44);
                  DialogBoxParam(hi,"IDD_DIALOG1",hwdlg,(DLGPROC)DialogProc,44);
                  
                  //ShowWindow(hwdialog,SW_SHOW);
                  break;
            default:
                  return FALSE;
            }
      return TRUE;
      }



0
 

Author Comment

by:elrond
ID: 1169276
Adjusted points to 100
0
 

Accepted Solution

by:
garethl earned 100 total points
ID: 1169277
You said that you can't access the m_hwnd because you are no in a method of you main window's class. But you also stated that you want this to happen after a button push, so why not just pass this.m_hwnd as a parameter to your function from the button handler ?
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now