RedKMan

I've got a dialog box up and running which has various controls such as radio buttons and listbox's. I am now trying to add some functionality to the controls. First off I am trying to populate the listbox. To do this I have the following code:-

// Put some text in the list box

HWND hListBox = ::GetDlgItem(g_hWnd, IDC_MODES);

for (int i = 0 ; i < 8 ; i++)

:endMessage( hListBox, LB_ADDSTRING, 0, (LPARAM) _T("Modes List Box"));

Compiles no worries but does not seem to do anything. At the moment I have this code in the Dialog box messageProc function in a case IDC_MODES which is the name of the listbox in the resource.h file. I figured it should go here because it causes compile errors if I put it in the Win MSG Proc. Also in the Dialog Msg proc I have the case IDOK which when clicked closes the dialog box and this works fine.

Any help really appreciated as there do not seem to be any simple complete step by step tutorials out there not even on MSDN.




Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

kirants

Is that code even executed For what message are you executing the code I am assumign you have a DialogProc and typically, this kind of thing is done in response to WM_INITDIALOG. Are you doing somethign similar

Also, have you tried using breakpoints to see if the code is reached, and if it does, if you are having the right hWnd etc.





Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

RedKMan

Hi,

I have a DialogProc and have code in the WM_INITDIALOG. See code below, I'm just doing something basic to learn how to use dialogs so the code is rough.

// Include the Windows header file

#include <windows.h>

// Include the string header file for use with the Window

// class and title name decleration

#include <tchar.h>

// Inlude the resource header file which contains information about

// the dialogue box.

#include "resource.h"

// Application global variables

HWND g_hWnd = 0;

HINSTANCE g_hInstance = 0;

BOOL g_bActive = FALSE;

TCHAR g_szAppClass[] = TEXT( "Application" );

TCHAR g_szAppTitle[] = TEXT( "Windows Application" );

// Application function prototypes

// Windows specific function prototypes

// ------------------------------------

// Windows Application entry point

INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT );

// Windows Message Handler

LRESULT WINAPI MsgProc( HWND, UINT, WPARAM, LPARAM );

// Dialogue Message Handlr

BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM );

// Dialogue Listbox control data populating function

VOID AdapterListBox( HWND );

//---------------------------------------------------------------

// Name: WinMain()

// Desc: Called as soon as the Application is loaded, much like

// Main().

//---------------------------------------------------------------

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow )

{

WNDCLASSEX wndclass;

HWND hWnd;

MSG msg;

// HRESULT hr;

// Setup the window attributes

wndclass.cbSize = sizeof( wndclass );

wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;

wndclass.lpfnWndProc = MsgProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );

wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );

wndclass.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW );

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = g_szAppClass; // Registered class name

wndclass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );

// Register the new window

if( RegisterClassEx( &wndclass ) == 0 )

exit(1);

// Create the main window. Window parameters make the main window

// 640X480 only

hWnd = CreateWindow( g_szAppClass, // Class Name

g_szAppTitle, // Name displayed on title bar

WS_OVERLAPPEDWINDOW, // Window style, using Popup

0, // X Position (Top Left)

0, // Y Position (Top Right)

640, // X Position (Bottom Right)

480, // Y Position (Bottom Left)

NULL,

NULL,

hInstance,

NULL );

// Tell windows to show the application

ShowWindow( hWnd, nCmdShow );

// Update the client by sending an initial WM_PAINT msg

UpdateWindow( hWnd );

// Save the main window handle

g_hWnd = hWnd;

// Save the main application instance

g_hInstance = hInstance;

// Main Message Loop. Keep looping through this application until

// the user chooses to exit. When the user chooses exit by clicking

// the red X box top right of the window for example. A WM_QUIT

// message will be sent to the application which prompts the application

// to exit.

ZeroMemory( &msg, sizeof( msg ) );

while( msg.message != WM_QUIT )

{

if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

else

{

// If the application is not minimised on the taskbar then

// go through the update and rendering loop

if( g_bActive )

{

}

}

}

UnregisterClass( g_szAppClass, hInstance );

// Return back to Windows

return( ( int ) msg.wParam );

}

//---------------------------------------------------------------

// Name: MsgProc()

// Desc: This is the main window message handling function

//---------------------------------------------------------------

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

{

switch( msg )

{

case WM_CREATE:

// Here we will create the Dialog box

DialogBoxParam(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG1),

NULL, (DLGPROC)DlgProc, 0);

case WM_ACTIVATE:

// The message WM_ACTIVE is sent whenever the application has focus.

// For example if the application is minimised then it is not in focus.

// As the application is not in focus it is pointless wasting clock

// cycles and graphics hardware to render to what cannot be seen.

g_bActive = ( BOOL ) wParam;

break;

case WM_KEYDOWN:

// Whenever a key is pressed (non-accelerated ie shift and f5, then

// this message is sent.

switch( wParam )

{

case VK_ESCAPE:

// If the user presses the Escape key then we will quit the

// application

PostMessage( g_hWnd, WM_QUIT, 0, 0 );

return 0;

};

break;

case WM_DESTROY:

// The message WM_DESTROY is sent by the Windows Messaging System

// when our message queue recieves the WM_QUIT message from the

// application. It just cleans everything up and quits the

// application

PostQuitMessage( 0 );

return 0;

}

// If any messages reach here then it was not handled by our application.

// So we will return it back to the queue so the next Windows application

// like a background task.

return DefWindowProc( hWnd, msg, wParam, lParam );

}

//---------------------------------------------------------------

// Name: DLGPROC()

// Desc: CallBack function to handle the Device / Mode selection

// in the Dialog

//---------------------------------------------------------------

BOOL CALLBACK DlgProc( HWND hWnd, UINT Message, WPARAM wParam,

LPARAM lParam )

{

switch( Message )

{

case WM_INITDIALOG:

{

AdapterListBox( g_hWnd );

return ( INT_PTR ) TRUE;

}

case WM_COMMAND:

{

// Control Changed

switch( LOWORD( wParam ) )

{

// OK Button => Read Selections

case IDOK:

{

EndDialog( hWnd, IDOK );

return TRUE;

} break;

case IDCANCEL:

{

// Cancel Button

EndDialog( hWnd, IDCANCEL );

return TRUE;

} break;

}

} break;

}

return FALSE;

}

//---------------------------------------------------------------

// Name: AdapterListBox()

// Desc: Populate the Adapter List Box

//---------------------------------------------------------------

VOID AdapterListBox( HWND hWnd )

{

// Put some text in the list box

HWND hListBox = ::GetDlgItem(g_hWnd, IDC_MODES);

for (int i = 0 ; i < 8 ; i++)

:Tongue TiedendMessage( hListBox, LB_ADDSTRING, 0,

(LPARAM) _T("Modes List Box"));

}






Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

kirants

Put a breakpoint in the AdapterListBox function ( F9 ) and run your program within debugger by hitting F5. Then single step the code one line by line using F10. After each step see what the variable values are, for e.g. hWnd, g_hWnd, hListBox etc.

You will narrow down on the problem very soon. It is a good learning experience for you to understand the power of the debugger in finding problems like this.





Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

RedKMan

Hi,

I see the problem as hWnd and hListBox cannot be evaluated. I've tried moving a few things around and tried replacing hWnd with g_hWnd but I cannot solve this. I get the impression its embarrasingly simple but I cannot think of how to correct it.






Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

kirants

You need to understand one thing here.

GetDlgItem takes a window handle as first parameter and an ID of a control as second parameter.

Now, the relation ship between these is important. The ID is not just any ID. When you issue a GetDlgItem, what you are saying is, here is a window handle ( representing a window , of course ), now go and look in the immediate child windows of this window and locate the one with ID = second parameter for me. So, you see, the relationship is important here.

So, if you pass in a handle to a window and an ID which is an invalid child window , it won't work.

Similary, if a window hWnd1 has a child window with ID = 10 and and you pass in hWnd2 and 100 to GetDlgItem it doesn't work either.

So, if the GetDlgItem is failing, most likely, the parameters you are passing do not make sense logically as a valid relationship. Once you have figured that out, back track from the callee to the caller and see, if you are really passing what you need to passing for the function to do what it does. Perhaps, what you are passing in into the function itself is messed up.

Note down in a paper all the possible windows you have and draw and mark them with the appropriate variable names you are using and sketch out the relationship.





Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

Paul Marriott

I can see what the problem is..

BOOL CALLBACK DlgProc( HWND hWnd, UINT Message, WPARAM wParam,

LPARAM lParam )

{

switch( Message )

{

case WM_INITDIALOG:

{

AdapterListBox( g_hWnd );

return ( INT_PTR ) TRUE;

}

Should be

BOOL CALLBACK DlgProc( HWND hWnd, UINT Message, WPARAM wParam,

LPARAM lParam )

{

switch( Message )

{

case WM_INITDIALOG:

{

AdapterListBox(hWnd);

return ( INT_PTR ) TRUE;

}

g_hWnd has been initialised to be the parent hWnd so if calling

AdapterListBox( g_hWnd ); this would be looking for the listbox on the parent instead on on the dialog.





Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

RedKMan

Thanks for the post on this, if only it was that simple. I've changed the code but it still does not work. I get the feeling I am missing some dialog control code somewhere but there are few tutorials on this subject out there. I think I need to add code rather than alter whats already there because I've tried changing a number of things and nothing works. Thanks again though Smile






Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

RedKMan

I've modded my code a bit however after debugging the all the Window handles have the "Expression cannot be evaluated". I understand that until I solve that problem any code I try to add text to the list box control will fail. Problem I've been on this for a few days solid now, researching, trying new things but nothing will work. Doesn't help that there is next to nothing on a simple win32 non mfc listbox control example. Seems like everyone uses MFC which I don't want to do.

If anyone can help with this I'd be grateful as I have completely run out of any ideas!!

//---------------------------------------------------------------

// File: WinMain.cpp

//

// Desc: Simple functioning Windows application

//

// Date: July 29th, 2007.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//---------------------------------------------------------------

// Include the Windows header file

#include <windows.h>

// Include the string header file for use with the Window

// class and title name decleration

#include <tchar.h>

// Include the dialog resource header

#include "resource.h"

// Application global variables

HWND g_hWnd = 0;

HINSTANCE g_hInstance = 0;

BOOL g_bActive = FALSE;

TCHAR g_szAppClass[] = TEXT( "Application" );

TCHAR g_szAppTitle[] = TEXT( "Windows Application" );

HWND g_hDialog = NULL; // Win handle for dialog

TCHAR buf[] = TEXT( "Text In List Box" );

// Application function prototypes

// Windows specific function prototypes

// ------------------------------------

// Windows Application entry point

INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT );

// Windows Message Handler

LRESULT WINAPI MsgProc( HWND, UINT, WPARAM, LPARAM );

// Application specific function prototypes

// ----------------------------------------

// Dialog Message Handler

BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM );

// Put some text in the listbox

BOOL OnInitDialog();

//---------------------------------------------------------------

// Name: WinMain()

// Desc: Called as soon as the Application is loaded, much like

// Main().

//---------------------------------------------------------------

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow )

{

WNDCLASSEX wndclass;

HWND hWnd;

MSG msg;

// Setup the window attributes

wndclass.cbSize = sizeof( wndclass );

wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;

wndclass.lpfnWndProc = MsgProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );

wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );

wndclass.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW );

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = g_szAppClass; // Registered class name

wndclass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );

// Register the new window

if( RegisterClassEx( &wndclass ) == 0 )

exit(1);

// Create the main window. Window parameters make the main window

// 640X480 only

hWnd = CreateWindow( g_szAppClass, // Class Name

g_szAppTitle, // Name displayed on title bar

WS_OVERLAPPEDWINDOW, // Window style, using Popup

0, // X Position (Top Left)

0, // Y Position (Top Right)

640, // X Position (Bottom Right)

480, // Y Position (Bottom Left)

NULL,

NULL,

hInstance,

NULL );

// Tell windows to show the application

ShowWindow( hWnd, nCmdShow );

// Update the client by sending an initial WM_PAINT msg

UpdateWindow( hWnd );

// Save the main window handle

g_hWnd = hWnd;

// Save the main application instance

g_hInstance = hInstance;

// Main Message Loop. Keep looping through this application until

// the user chooses to exit. When the user chooses exit by clicking

// the red X box top right of the window for example. A WM_QUIT

// message will be sent to the application which prompts the application

// to exit.

ZeroMemory( &msg, sizeof( msg ) );

while( msg.message != WM_QUIT )

{

if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

else

{

// If the application is not minimised on the taskbar then

// go through the update and rendering loop

if( g_bActive )

{

}

}

}

UnregisterClass( g_szAppClass, hInstance );

// Return back to Windows

return( ( int ) msg.wParam );

}

//---------------------------------------------------------------

// Name: OnInitDialog()

// Desc: Put some text in the lst box

//---------------------------------------------------------------

BOOL OnInitDialog()

{

// Put some text in the list box

HWND hListBox = ::GetDlgItem( g_hDialog, IDC_MODE);

for (int i = 0 ; i < 8 ; i++)

:Tongue TiedendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM) _T("List Box"));

return true;

}

//---------------------------------------------------------------

// Name: DlgProc()

// Desc: Dialog Message Handler

//---------------------------------------------------------------

BOOL CALLBACK DlgProc( HWND g_hDialog, UINT Message, WPARAM wParam,

LPARAM lParam )

{

switch( Message )

{

case WM_INITDIALOG:

return TRUE;

break;

case WM_COMMAND:

switch( LOWORD( wParam ) )

{

case IDOK:

MessageBox( g_hDialog, L"Dialog", L"You clicked OK button",

MB_OK | MB_ICONEXCLAMATION );

EndDialog( g_hDialog, IDOK );

break;

case IDCANCEL:

MessageBox( g_hDialog, L"Dialog", L"You clicked CANCEL button",

MB_OK | MB_ICONEXCLAMATION );

EndDialog( g_hDialog, IDCANCEL );

break;

case IDC_MODE:

break;

}

break;

default:

return FALSE;

}

return TRUE;

}

//---------------------------------------------------------------

// Name: MsgProc()

// Desc: This is the main window message handling function

//---------------------------------------------------------------

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

{

switch( msg )

{

case WM_CREATE:

g_hDialog = CreateDialog( GetModuleHandle( NULL ),

MAKEINTRESOURCE( IDD_DIALOG1 ),hWnd,

DlgProc );

if(g_hDialog != NULL)

{

ShowWindow( g_hDialog, SW_SHOW );

}

else

{

MessageBox( hWnd, L"MsgProc() case WM_CREATE CreateDialog() returned NULL", L"Warning!",

MB_OK | MB_ICONINFORMATION );

}

break;

case WM_ACTIVATE:

// The message WM_ACTIVE is sent whenever the application has focus.

// For example if the application is minimised then it is not in focus.

// As the application is not in focus it is pointless wasting clock

// cycles and graphics hardware to render to what cannot be seen.

g_bActive = ( BOOL ) wParam;

break;

case WM_KEYDOWN:

// Whenever a key is pressed (non-accelerated ie shift and f5, then

// this message is sent.

switch( wParam )

{

case VK_ESCAPE:

// If the user presses the Escape key then we will quit the

// application

PostMessage( g_hWnd, WM_QUIT, 0, 0 );

return 0;

break;

};

break;

case WM_DESTROY:

// The message WM_DESTROY is sent by the Windows Messaging System

// when our message queue recieves the WM_QUIT message from the

// application. It just cleans everything up and quits the

// application

DestroyWindow( g_hDialog );

PostQuitMessage( 0 );

return 0;

break;

}

// If any messages reach here then it was not handled by our application.

// So we will return it back to the queue so the next Windows application

// like a background task.

return DefWindowProc( hWnd, msg, wParam, lParam );

}






Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

Paul Marriott

The funny thing with CreateDialog is as follows:

Calling:

{

...

ghDlg=CreateDialog(etc)

...

}

MyDlgProc(HWND hDlg...)

{

WM_INITDIALOG:

//hDlg=valid

//ghDlg=NOT VALID

SetTimer(hDlg,1,1000,NULL);

return;

WM_TIMER:

//hDlg=valid

//ghDlg=valid

return ;

}

When you call CreateDialog, the order it is created as follows:

Create Dialog, then do WM_INITDIALOG ghDlg will not be valid yet, but hDlg will be.

When WM_INITDIALOG is finished and CreateDialog returns, ghDlg will then be valid.

Thus: cannot use ghDlg in the dialogs WM_INITDIALOG as its value wont be initialised yet.





Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

kirants

Hints:

1. When you are within the DlgProc, instead of relying on the g_hDialog variable, use the HWND parameter passed in where required. This will avoid some confusion. Use the global g_hDialog variable only for cross component functions.

2. Also, if you call any functions from within DlgProc that needs the dialog handle, instead of writing those functions to use the global variable, pass in the dialog HWND as parameter, for example, you can Rewrite OnInitDialog as OnInitDialog(HWND hWndDlg) and call it as OnInitDialog(hWnd);

3. This is not good:

BOOL CALLBACK DlgProc( HWND g_hDialog, UINT Message, WPARAM wParam,
LPARAM lParam )

Replace with:

BOOL CALLBACK DlgProc( HWND hDialog, UINT Message, WPARAM wParam,
LPARAM lParam )

Reason:

in the first declaration, you are having g_hDialog variable twice in , one in global scope, and one in the scope of the function. The compiler will pick the local one for all calls within the function that uses g_hDialog. This might be ok, but , you see, it causes confusion. You might think it is using the global one, while it is actually using the one in local function scope, and the two values may be totally different.

Again, all this causes confusion and give you sleepless nights Wink

4. Do not post a WM_QUIT message yourself. Leave it to the system to do it. Use PostQuitMessage instead. IN your case, you should instead call DestroyWindow there ( which in turn will result in WM_DESTORY and a PostQuitMessage )

Otherwise, your code is good.




Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

RedKMan

Again thanks for the hints, definitely helping me understand things better. I still think the code is missing something though. There must be some command for writing text to a ListBox control that I'm missing because it's still not listed in the control. I thought OnInitDialog() with the SendMessage() line would work but it does not seem to be. Do I have to use WM_PAINT to display the text in the listbox control or something Below is my code updated with the changes recommended. I've put in the call to OnInitDialog() also.

I'm not going to bed until I nail this Smile

//---------------------------------------------------------------

// File: WinMain.cpp

//

// Desc: Simple functioning Windows application

//

// Date: July 29th, 2007.

//

// Copyright (c) Paul Kirkbride. All rights reserved.

//---------------------------------------------------------------

// Include the Windows header file

#include <windows.h>

// Include the string header file for use with the Window

// class and title name decleration

#include <tchar.h>

// Include the dialog resource header

#include "resource.h"

// Application global variables

HWND g_hWnd = 0;

HINSTANCE g_hInstance = 0;

BOOL g_bActive = FALSE;

TCHAR g_szAppClass[] = TEXT( "Application" );

TCHAR g_szAppTitle[] = TEXT( "Windows Application" );

HWND g_hDialog = NULL; // Win handle for dialog

TCHAR buf[] = TEXT( "Text In List Box" );

// Application function prototypes

// Windows specific function prototypes

// ------------------------------------

// Windows Application entry point

INT WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, INT );

// Windows Message Handler

LRESULT WINAPI MsgProc( HWND, UINT, WPARAM, LPARAM );

// Application specific function prototypes

// ----------------------------------------

// Dialog Message Handler

BOOL CALLBACK DlgProc( HWND, UINT, WPARAM, LPARAM );

// Put some text in the listbox

BOOL OnInitDialog( HWND hDialog );

//---------------------------------------------------------------

// Name: WinMain()

// Desc: Called as soon as the Application is loaded, much like

// Main().

//---------------------------------------------------------------

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdShow )

{

WNDCLASSEX wndclass;

HWND hWnd;

MSG msg;

// Setup the window attributes

wndclass.cbSize = sizeof( wndclass );

wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;

wndclass.lpfnWndProc = MsgProc;

wndclass.cbClsExtra = 0;

wndclass.cbWndExtra = 0;

wndclass.hInstance = hInstance;

wndclass.hIcon = LoadIcon( NULL, IDI_APPLICATION );

wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );

wndclass.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW );

wndclass.lpszMenuName = NULL;

wndclass.lpszClassName = g_szAppClass; // Registered class name

wndclass.hIconSm = LoadIcon( NULL, IDI_APPLICATION );

// Register the new window

if( RegisterClassEx( &wndclass ) == 0 )

exit(1);

// Create the main window. Window parameters make the main window

// 640X480 only

hWnd = CreateWindow( g_szAppClass, // Class Name

g_szAppTitle, // Name displayed on title bar

WS_OVERLAPPEDWINDOW, // Window style, using Popup

0, // X Position (Top Left)

0, // Y Position (Top Right)

640, // X Position (Bottom Right)

480, // Y Position (Bottom Left)

NULL,

NULL,

hInstance,

NULL );

// Tell windows to show the application

ShowWindow( hWnd, nCmdShow );

// Update the client by sending an initial WM_PAINT msg

UpdateWindow( hWnd );

// Save the main window handle

g_hWnd = hWnd;

// Save the main application instance

g_hInstance = hInstance;

// Main Message Loop. Keep looping through this application until

// the user chooses to exit. When the user chooses exit by clicking

// the red X box top right of the window for example. A WM_QUIT

// message will be sent to the application which prompts the application

// to exit.

ZeroMemory( &msg, sizeof( msg ) );

while( msg.message != WM_QUIT )

{

if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

else

{

// If the application is not minimised on the taskbar then

// go through the update and rendering loop

if( g_bActive )

{

}

}

}

UnregisterClass( g_szAppClass, hInstance );

// Return back to Windows

return( ( int ) msg.wParam );

}

//---------------------------------------------------------------

// Name: OnInitDialog()

// Desc: Put some text in the lst box

//---------------------------------------------------------------

BOOL OnInitDialog( HWND hDialog)

{

// Put some text in the list box

HWND hListBox = ::GetDlgItem( hDialog, IDC_MODE);

for (int i = 0 ; i < 8 ; i++)

:Tongue TiedendMessage( hListBox, LB_ADDSTRING, 0, (LPARAM) _T("List Box"));

return true;

}

//---------------------------------------------------------------

// Name: DlgProc()

// Desc: Dialog Message Handler

//---------------------------------------------------------------

BOOL CALLBACK DlgProc( HWND hDialog, UINT Message, WPARAM wParam,

LPARAM lParam )

{

switch( Message )

{

case WM_INITDIALOG:

OnInitDialog( hDialog );

return TRUE;

break;

case WM_COMMAND:

switch( LOWORD( wParam ) )

{

case IDOK:

MessageBox( hDialog, L"Dialog", L"You clicked OK button",

MB_OK | MB_ICONEXCLAMATION );

EndDialog( hDialog, IDOK );

break;

case IDCANCEL:

MessageBox( hDialog, L"Dialog", L"You clicked CANCEL button",

MB_OK | MB_ICONEXCLAMATION );

EndDialog( hDialog, IDCANCEL );

break;

case IDC_MODE:

break;

}

break;

default:

return FALSE;

}

return TRUE;

}

//---------------------------------------------------------------

// Name: MsgProc()

// Desc: This is the main window message handling function

//---------------------------------------------------------------

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

{

switch( msg )

{

case WM_CREATE:

g_hDialog = CreateDialog( GetModuleHandle( NULL ),

MAKEINTRESOURCE( IDD_DIALOG1 ),hWnd,

DlgProc );

if(g_hDialog != NULL)

{

ShowWindow( g_hDialog, SW_SHOW );

}

else

{

MessageBox( hWnd, L"MsgProc() case WM_CREATE CreateDialog() returned NULL", L"Warning!",

MB_OK | MB_ICONINFORMATION );

}

break;

case WM_ACTIVATE:

// The message WM_ACTIVE is sent whenever the application has focus.

// For example if the application is minimised then it is not in focus.

// As the application is not in focus it is pointless wasting clock

// cycles and graphics hardware to render to what cannot be seen.

g_bActive = ( BOOL ) wParam;

break;

case WM_KEYDOWN:

// Whenever a key is pressed (non-accelerated ie shift and f5, then

// this message is sent.

switch( wParam )

{

case VK_ESCAPE:

// If the user presses the Escape key then we will quit the

// application

PostMessage( g_hWnd, WM_QUIT, 0, 0 );

return 0;

break;

};

break;

case WM_DESTROY:

// The message WM_DESTROY is sent by the Windows Messaging System

// when our message queue recieves the WM_QUIT message from the

// application. It just cleans everything up and quits the

// application

DestroyWindow( g_hDialog );

PostQuitMessage( 0 );

return 0;

break;

}

// If any messages reach here then it was not handled by our application.

// So we will return it back to the queue so the next Windows application

// like a background task.

return DefWindowProc( hWnd, msg, wParam, lParam );

}






Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

Paul Marriott

I am not sure about how you are calling...

HWND hListBox = ::GetDlgItem( hDialog, IDC_MODE);

for (int i = 0 ; i < 8 ; i++)

:endMessage( hListBox, LB_ADDSTRING, 0, (LPARAM) _T("List Box"));

In standard win32 code, I would do

HWND hControl;

hControl=GetDlgItem(hDlg,IDC_MODE);

SendMessage(hControl,LB_ADDSTRING,0,(LPARAM) _T("My Text String"));

or else

SendDlgItemMessage(hDialog,IDC_MODE,LB_ADDSTRING,0,(LPARAM) _T("My Text String"));

Have you tried creating an edit control on the same dialog and entering text via the dialogproc as

SetDlgItemText(hDlg,IDC_TEXT,"A Text String");

Can you post the programs rc file so we can look at the dialog template





Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

RedKMan

I tried the code suggetions above and put them in the WM_INITDIALOG case instead of the call to OnInitDialog() but no joy. I've not tried adding an edit control as I don't really need one. Below is the RC Resouce file code

#include "resource.h"

#include "winresrc.h"


IDD_DIALOG1 DIALOG DISCARDABLE 0, 0, 229, 151
STYLE DS_MODALFRAME | DS_3DLOOK | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Change Device Dialogue"
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "&OK",IDOK,60,130,50,14
PUSHBUTTON "&Cancel",IDCANCEL,120,130,50,14
LTEXT "Graphics Adapter :",IDC_STATIC,100,5,100,10
LTEXT "Available Modes:",IDC_STATIC,100,35,100,10
CONTROL "Fullscreen",IDC_FULL,"Button",BS_AUTORADIOBUTTON |
BS_LEFTTEXT | BS_NOTIFY,10,65,50,10
CONTROL "Windowed",IDC_WND,"Button",BS_AUTORADIOBUTTON |
BS_LEFTTEXT | BS_NOTIFY,10,80,50,10
LTEXT "Adapter Format:",IDC_STATIC,100,65,100,10
CONTROL "Back Buffer Format:",IDC_STATIC,"Static",
SS_LEFTNOWORDWRAP | WS_GROUP,100,95,100,10
LTEXT "Device Type:",IDC_STATIC,10,95,75,10
COMBOBOX IDC_DEVICE,10,105,80,45,CBS_DROPDOWNLIST | WS_VSCROLL |
WS_TABSTOP
COMBOBOX IDC_ADAPTER,100,15,120,130,CBS_DROPDOWNLIST | WS_VSCROLL |
WS_TABSTOP
COMBOBOX IDC_MODE,100,45,120,85,CBS_DROPDOWNLIST | WS_VSCROLL |
WS_TABSTOP
COMBOBOX IDC_ADAPTERFMT,100,75,120,70,CBS_DROPDOWNLIST |
WS_VSCROLL | WS_TABSTOP
COMBOBOX IDC_BACKFMT,100,105,120,45,CBS_DROPDOWNLIST | WS_VSCROLL |
WS_TABSTOP
END

The Resource.h header file as well.

#ifndef RESOURCE_H

#define RESOURCE_H

#define IDD_DIALOG1 1000

#define IDC_ADAPTER 1001

#define IDC_MODES 1002

#define IDC_FULL 1003

#define IDC_WND 1004

#define IDC_REF 1005

#define IDC_DEVICE 1010

#define IDC_MODE 1011

#define IDC_ADAPTERFMT 1012

#define IDC_BACKFMT 1013

#define IDC_STATIC -1

#endif






Re: Windows SDK How to populate a WIN32 C++ Non MFC Dialog Listbox control

Paul Marriott

I think the problem is because you are using a LB_ADDSTRING command to send data to a COMBOBOX.

To send data to the combo box , use CB_ADDSTRING

For the Previous reply, what I ws getting at was the :: in front of GetDlgItem.

I am not sure what the effect of that is.