elrond
asked on
C++ Language question
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
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
Post the code for the CreateWindow call. What are you using for lpClassName?
ASKER
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=AfxGetInstanceHa ndle( );
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);//Get StockObjec t (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",GetLastEr ror());
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",//lpWindowNam e, // 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_I CONSTOP);
MessageBox(out,"error",MB_ OK);
exit(-1);
}
ShowWindow( NULL); /* Display main window */
UpdateWindow();
return 0;
}
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=AfxGetInstanceHa
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);//Get
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",GetLastEr
MessageBox(out,"error",MB_
Child=CreateWindowEx( // without 'Ex' next line has to be omitted
WS_EX_OVERLAPPEDWINDOW,
"foo",//lpClassName, // pointer to registered class name
"foo_window",//lpWindowNam
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_I
MessageBox(out,"error",MB_
exit(-1);
}
ShowWindow( NULL); /* Display main window */
UpdateWindow();
return 0;
}
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_TOPMO ST, 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...
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_TOPMO
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...
ASKER
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).
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!
{
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!
ASKER
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.
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.
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,5 0,14
PUSHBUTTON "New window",ID_DINKNAPP,27,20, 76,56
CTEXT "Text",IDC_STATIC,22,6,97, 11,SS_SUNK EN | 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,LPA RAM);
BOOL CALLBACK DialogProc(HWND,UINT,WPARA M,LPARAM);
HWND hwndw;//This one is global
WINAPI WinMain(HINSTANCE denna,HINSTANCE innan,LPSTR cmdln,int style)
{
WNDCLASSEX wcw;
wcw.hbrBackground=(HBRUSH) GetStockOb ject(WHITE _BRUSH);
wcw.hIcon=LoadIcon(0,IDI_A PPLICATION );
wcw.hIconSm=LoadIcon(0,IDI _APPLICATI ON);
wcw.hCursor=LoadCursor(0,I DC_CROSS);
wcw.lpfnWndProc=FonsterPro cedur;
wcw.lpszMenuName=0;
wcw.lpszClassName="hejdu";
wcw.cbSize=sizeof(WNDCLASS EX);
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(de nna,"IDR_A CCELERATOR 1");
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(hwdialo g,&msg));
else
if(TranslateAccelerator(hw ndw,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(h w,GWL_HINS TANCE);
hwdialog=CreateDialog(hi," IDD_DIALOG 1",hw,(DLG PROC)Dialo gProc);
ShowWindow(hwdialog,SW_SHO W);
}
break;
case WM_LBUTTONDOWN:
hi = (HINSTANCE)GetWindowLong(h w,GWL_HINS TANCE);
hwdialog=CreateDialog(hi," IDD_DIALOG 1",hw,(DLG PROC)Dialo gProc);
ShowWindow(hwdialog,SW_SHO W);
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_O VERLAPPEDW INDOW
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(h wdlg,GWL_H INSTANCE), //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);//De nna 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(hw dlg),_itoa (test++,"" ,10));
DestroyWindow(hwdlg); //Används vid ickemodala fönster
}
else if(modal==1)
{
SetWindowText(GetParent(hw dlg),_itoa (v,"",10)) ;
EndDialog(hwdlg,wp);
}
break;
case WM_LBUTTONDOWN:
hwnd=GetParent(hwdlg);
hi=(HINSTANCE)GetWindowLon g(hwnd,GWL _HINSTANCE );
//hwdialog=CreateDialogPar am(hi,"IDD _DIALOG1", hwdlg,(DLG PROC)Dialo gProc,44);
DialogBoxParam(hi,"IDD_DIA LOG1",hwdl g,(DLGPROC )DialogPro c,44);
//ShowWindow(hwdialog,SW_S HOW);
break;
default:
return FALSE;
}
return TRUE;
}
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,5
PUSHBUTTON "New window",ID_DINKNAPP,27,20,
CTEXT "Text",IDC_STATIC,22,6,97,
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,
BOOL CALLBACK DialogProc(HWND,UINT,WPARA
HWND hwndw;//This one is global
WINAPI WinMain(HINSTANCE denna,HINSTANCE innan,LPSTR cmdln,int style)
{
WNDCLASSEX wcw;
wcw.hbrBackground=(HBRUSH)
wcw.hIcon=LoadIcon(0,IDI_A
wcw.hIconSm=LoadIcon(0,IDI
wcw.hCursor=LoadCursor(0,I
wcw.lpfnWndProc=FonsterPro
wcw.lpszMenuName=0;
wcw.lpszClassName="hejdu";
wcw.cbSize=sizeof(WNDCLASS
wcw.cbClsExtra=0;
wcw.cbWndExtra=0;
wcw.style=CS_DBLCLKS;
wcw.hInstance=denna;
if(!RegisterClassEx(&wcw))
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(de
MSG msg;
while(GetMessage(&msg,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(hwdialo
else
if(TranslateAccelerator(hw
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(h
hwdialog=CreateDialog(hi,"
ShowWindow(hwdialog,SW_SHO
}
break;
case WM_LBUTTONDOWN:
hi = (HINSTANCE)GetWindowLong(h
hwdialog=CreateDialog(hi,"
ShowWindow(hwdialog,SW_SHO
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_O
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(h
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
for(v=0;v<500;v++)
{
if(n[v]==0)
{
n[v]=hwdlg;
break;
}
}
break;
}
break;
case WM_RBUTTONDOWN:
//EndDialog(hwdlg,wp);//De
modal=0;
for(v=0;v<500;v++)
{
if(n[v]==hwdlg)
{
n[v]=0;
modal=1;
break;
}
}
if (modal==0)
{
SetWindowText(GetParent(hw
DestroyWindow(hwdlg); //Används vid ickemodala fönster
}
else if(modal==1)
{
SetWindowText(GetParent(hw
EndDialog(hwdlg,wp);
}
break;
case WM_LBUTTONDOWN:
hwnd=GetParent(hwdlg);
hi=(HINSTANCE)GetWindowLon
//hwdialog=CreateDialogPar
DialogBoxParam(hi,"IDD_DIA
//ShowWindow(hwdialog,SW_S
break;
default:
return FALSE;
}
return TRUE;
}
ASKER
Adjusted points to 100
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.