Home > database >  Are resources global in scope in a C (VS) program and consequently where is the ideal place to loa
Are resources global in scope in a C (VS) program and consequently where is the ideal place to loa

Time:11-15

I'm using a resource file for my dialog boxes, menus, etc. in a C program in Visual Studio. Just wondering if a resource file has a global scope? Also, as a consequence of this, where is the ideal place in the program to call GetDlgItem to get the handle of a combobox and to load its list (as well as other similar tasks)?

Can someone give a simple example of adding a list item to a combo box in the context in which a resource file is being used rather than a when a combo box created explicitly in the code is being used?

CodePudding user response:

The information given in the comments answered the question sufficiently. What initially prompted the question was my desire and attempts to put items in a combo box list. The code below successfully accomplished this. This is mostly a Visual C autogenerated message handling procedure for a drop down menu entry called "Add" under a main menu "Edit." In the dialog generated by selecting Add there is a combo box which is designated here as IDC_COMBO1. (IDC_COMBO1 is the name given to the combobox in the resource file and is defined in resource.h so it is recognized here.) I added three lines to the code to put the entry "1st" into the combo box list. Also, as suggested by Richard Critten, I handled this in the WM_INITDIALOG message case which makes sense beside being simple and concise. The three lines were:

HWND hWndCB1;
hWndCB1 = GetDlgItem(hDlg, IDC_COMBO1);
SendMessage(hWndCB1, CB_ADDSTRING, 0, (LPARAM)TEXT("1st"));

The entry "1st" appeared in the list when I built the project. Thanks to those who commented, and I hope that this helps someone else who is attempting to accomplish this. The code of the message handling procedure is below.

INT_PTR CALLBACK Add(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

    HWND hWndCB1;

    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        {   
            hWndCB1 = GetDlgItem(hDlg, IDC_COMBO1);
            SendMessage(hWndCB1, CB_ADDSTRING, 0, (LPARAM)TEXT("1st"));
            return (INT_PTR)TRUE; 
        }
        break;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}
  • Related