CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2004
    Posts
    1,352

    Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainform

    Hi Guys and Gals,

    Im an MFC guy who mostly writes Desktop FormView apps. in MFC: Not exprienced in WIN32api's

    I'm trying to create a WIN 32 api FormView sytle app, which enventually I will port into classes to automate creating the Win 32 app.

    I'm not very experienced with WIN32s and I'm finally taking the time to learn to write WIN32 apps, hopefully as quick as I can write FormView Apps in MFC.

    Here are my questions:

    1) is there a way to place a formview resource created in the resource editor in the MainForm: like you do in an MFC FORMVIEW? Too cubersome to enter the controls manually as in the code below

    2) In the code below why cant I create the controls in the WM_CREATE message handler: It seems to me that would be the place to do it. I created the on the InitInstance function provided by the Win32 skeleton provided by Visual Studio.

    Here is the Initinstance of my WIN32 App.

    Code:
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;
    
       hInst = hInstance; // Store instance handle in our global variable
    
        
       
    
        
    
       hWnd = CreateWindow(szWindowClass, //class name
    	  szTitle, 	          //title
    	  WS_OVERLAPPEDWINDOW,//style
          CW_USEDEFAULT,   //x
    	  0,               //y
    	  CW_USEDEFAULT,   //width
    	  0,               //height
    	  NULL,            //parent handle
    	  NULL,            //menu handle
    	  hInstance,       //which program
    	  NULL);           // no init data ? 
    
    	
    
       if (!hWnd)
       {
          return FALSE;
       }
       
       //CREATE CONTROLS HERE & SET BRUSHES
       g_hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
       
       	
    
       static HWND hwnd_st_u, hwnd_ed_u;
       int x, w, y, h;
       y = 10; h = 20;
       x = 10; w = 50;
       hwnd_st_u = CreateWindow("static", "ST_U",
                                  WS_CHILD | WS_VISIBLE | WS_TABSTOP,
                                  x, y, w, h,
                                  hWnd, (HMENU)(501),
                                  (HINSTANCE) GetWindowLong (hWnd, GWL_HINSTANCE), NULL);
       SetWindowText(hwnd_st_u, "User:");
    
       x += 5+ w; w = 60;
       hwnd_ed_u = CreateWindow("edit", "",
                                  WS_CHILD | WS_VISIBLE | WS_TABSTOP
                                  | ES_LEFT | WS_BORDER,
                                  x, y, w, h,
                                  hWnd, (HMENU)(502),
                                  (HINSTANCE) GetWindowLong (hWnd, GWL_HINSTANCE), NULL);
       SetWindowText(hwnd_ed_u, "Bill");
    
       x=10;
       y= 10 + h;
       w = 200;
       HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("Enter some text"),
                                   WS_CHILD | WS_VISIBLE, x, y, w,
                                   h, hWnd, (HMENU)(500), NULL, NULL);
    
    
    
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    Last edited by ADSOFT; January 2nd, 2019 at 04:20 AM.
    Rate this post if it helped you.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    Writing windows programs using just WIN32 is different from doing this using MFC. For a start, you need a message loop which isn't shown in the code in post #1. The main window is created outside of this loop and then all other processing is done in response to event messages sent from within this loop. Yes, you can create controls in the WM_CREATE message for the main window if the message loop etc is working properly.

    Have a look at https://docs.microsoft.com/en-us/win...indows-program and subsequent articles which explain win32 windows programming. Also consider the book 'Programming Windows' by Charles Petzold https://www.amazon.co.uk/Programming...ywords=petzold which is the bible as to how to write WIN32 Windows programs.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    I do have a message loop for my app: I just posted a snip. I didn't want to post the whole app: here is my message loop. ... the app is working .

    Can you please edit my message loop to display my controls.

    Code:
      /*LRESULT*/static BOOL CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    	TCHAR szHello[MAX_LOADSTRING];
    	LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
    
    	switch (message) 
    	{
    		//DOESN'T WORK HERE
    		case WM_CTLCOLORDLG:
    				return (LONG)g_hbrBackground;
    
    		case WM_CREATE:
    		
    		
    
    		break;
    
    		case WM_CHAR:
    			  gMessage("KeyBoard Hit"); 
    		break; 
    		case WM_COMMAND:
    			wmId    = LOWORD(wParam); 
    			wmEvent = HIWORD(wParam); 
    			// Parse the menu selections:
    
    			switch (wmId)
    			{   case IDM_MYDIALOG:
    				{
    					int ret = DialogBox(GetModuleHandle(NULL), 
    						MAKEINTRESOURCE(IDD_MY_DIALOG_HW), hWnd, DlgProc_My_HW);
    					if(ret == IDOK){
    						MessageBox(hWnd, "Dialog exited with IDOK.", "Notice",
    							MB_OK | MB_ICONINFORMATION);
    					}
    					else if(ret == IDCANCEL){
    						MessageBox(hWnd, "Dialog exited with IDCANCEL.", "Notice",
    						MB_OK | MB_ICONINFORMATION);
    					}
    					else if(ret == -1){
    						MessageBox(hWnd, "Dialog failed!", "Error",
    						MB_OK | MB_ICONINFORMATION);
    					}
    				}
    
    				break;
    				case IDM_ABOUT:
    				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, mAbout);
    				   break;
    				case IDM_EXIT:
    				   DestroyWindow(hWnd);
    				   break;
    				default:
    				   return DefWindowProc(hWnd, message, wParam, lParam);
    			}
    			break;
    		case WM_PAINT:
    			hdc = BeginPaint(hWnd, &ps);
    			// TODO: Add any drawing code here...
    			 SetTextColor(hdc, RGB(60,70,60));
    			 SetBkMode(hdc, TRANSPARENT);
    			RECT rt;
    			GetClientRect(hWnd, &rt);
    			DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
    			EndPaint(hWnd, &ps);
    			break;
    
    		
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }
    I got controls to display if I put them on Initinstance, but I couldn't create them in WM_CREATE ??
    Rate this post if it helped you.

  4. #4
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    Ok I got it to create the control in WM_CREATE

    ...here is the snippet
    Code:
    HWND hWndEdit;
    
    	switch (message) 
    	{
    		//DOESN'T WORK HERE
    		case WM_CTLCOLORDLG:
    				return (LONG)g_hbrBackground;
    
    		case WM_CREATE:
    		//g_hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
    			gMessage("WM_CREATE called"); 
    			hWndEdit =  CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("It Works"),
                                   WS_CHILD | WS_VISIBLE, 100, 100, 70,
                                   25, hWnd, (HMENU)(550), NULL, NULL);
    
    
    		
    
    		break;
    And for those who are interested when I register the main window I set the back ground color, it's weird color but I'm not experienced with CreateSolidBrush(...);

    Code:
    wcex.hbrBackground	= CreateSolidBrush(RGB(100,200,100));//(HBRUSH)(COLOR_WINDOW+1);
    Now if I can't get WIN32s to create all the controls in a FormView or Dialog template that would be awesome, I could then switch templates like switching screens.

    I'm aware that I could just place Dialog box in WinMain , but then I would have to add a menu, etc. I have no probs eventually going that route, but I anyone knows how to create all the controls in a Resource Template in WM_CREATE or at runtime, I would appreciate it.
    Rate this post if it helped you.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    Code:
    //DOESN'T WORK HERE
    case WM_CTLCOLORDLG:
    	return (LONG)g_hbrBackground;
    WM_CTLCOLORDLG message is sent direct to the appropriate dialog, and not to the window owner. See https://docs.microsoft.com/en-us/win...wm-ctlcolordlg

    WM_CREATE message is sent after the window has been created but before it is visible. It's been a very long time since I used a WIN32 windows program. If you create the controls in WM_CREATE, are the windows being created OK but not shown (ie the return value is not NULL)? I've looked at some old WIN32 code and after the controls are created using CreateWindowEx(), ShowWindow() with SW_SHOW is then used for each control.

    FWIW, these are extracts from a WIN32 program that creates controls:

    Code:
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
    {
    	if (const auto rest = CoInitialize(NULL); (rest != S_OK) && (rest != S_FALSE))
    		return FALSE;
    
    	LoadString(hInstance, IDS_APP_TITLE, g_Title, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_WINCLUDE, g_WindowClass, MAX_LOADSTRING);
    
    	if (!InitCommon() || !MyRegisterClass(hInstance) || !(g_hwndMain = InitInstance(hInstance, nCmdShow)))
    		return FALSE;
    
    	SendMessage(g_hwndMain, WA_STARTUP, 0, 0);
    
    	const auto hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINCLUDE));
    	MSG msg;
    
    	// Main message loop:
    	while (GetMessage(&msg, nullptr, 0, 0))
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    
    	return (int)msg.wParam;
    }
    Note that WA_STARTUP is a user defined message

    Code:
    HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       g_inst = hInstance;
    
       if (const auto hWnd = CreateWindowEx(0, g_WindowClass, g_Title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); hWnd) {
    	   ShowWindow(hWnd, nCmdShow);
    	   UpdateWindow(hWnd);
    	   return hWnd;
       }
    
       return FALSE;
    }
    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static LPARAM oldlParam = 0;
    
    	switch (message) {
    		case WA_STARTUP:
    			DoStartup(oldlParam);
    			return TRUE;
    
    		case WM_SIZE:
    			DoSize(oldlParam = lParam);
    			break;
    //....
    Code:
    void DoStartup(LPARAM lParam)
    {
    	SetCursor(g_cnorm);
    
    	if ((g_hwndTV = CreateWindowEx(0, WC_TREEVIEW, "", WS_VISIBLE | WS_CHILD | WS_DLGFRAME | WS_TABSTOP | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT | WS_VSCROLL | WS_HSCROLL | TVS_DISABLEDRAGDROP | TVS_SHOWSELALWAYS /*| TVS_CHECKBOXES*/, 0, 0, 0, 0, g_hwndMain, NULL, g_inst, NULL)) == NULL) {
    		ErrorBox("creating TreeView control"sv);
    		return;
    	}
    
    	if ((g_hwndLV = CreateWindowEx(0, WC_LISTVIEW, "", LVS_REPORT | LVS_SHOWSELALWAYS | LVS_SORTASCENDING |WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_HSCROLL | WS_VSCROLL, 0, 0, 0, 0, g_hwndMain, NULL, g_inst, NULL)) == NULL) {
    		ErrorBox("creating ListView control"sv);
    		return;
    	}
    
    	g_hwndLS = CreateWindowEx(0, "STATIC", "", WS_VISIBLE | WS_CHILD | SS_LEFT, 0, 0, 0, 0, g_hwndMain, NULL, g_inst, NULL);
    	g_hwndRS = CreateWindowEx(0, "STATIC", "", WS_VISIBLE | WS_CHILD | SS_LEFT, 0, 0, 0, 0, g_hwndMain, NULL, g_inst, NULL);
    
    	if ((g_hwndLS == NULL) || (g_hwndRS == NULL)) {
    		ErrorBox("creating static controls"sv);
    		return;
    	}
    
    	const auto	hdc = GetDC(g_hwndLS);
    	const auto	confon = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
    	TEXTMETRIC	tm = {0};
    
    	SelectObject(hdc, confon);
    	GetTextMetrics(hdc, &tm);
    	g_chHeight = tm.tmHeight;
    	ReleaseDC(g_hwndLS, hdc);
    
    	DoSize(lParam);
    
    	ShowWindow(g_hwndLS, SW_SHOW);
    	ShowWindow(g_hwndRS, SW_SHOW);
    	ShowWindow(g_hwndLV, SW_SHOW);
    	ShowWindow(g_hwndTV, SW_SHOW);
    //...
    Code:
    void DoSize(LPARAM lParam)
    {
    	const DWORD wndWid = LOWORD(lParam),
    				gap = 3,
    				TVwid = wndWid / 5,					//20% for TV
    				lvWidth = wndWid - TVwid,
    				dep = HIWORD(lParam),
    				height = g_chHeight * gap / 2;
    
    	MoveWindow(g_hwndTV, 0, height, TVwid, dep, TRUE);
    	MoveWindow(g_hwndLV, TVwid, height, lvWidth, dep, TRUE);
    	MoveWindow(g_hwndLS, 0, 0, TVwid, height, TRUE);
    	MoveWindow(g_hwndRS, TVwid, 0, lvWidth, height, TRUE);
    }

    PS Posted before I saw your post #4!
    Last edited by 2kaud; January 2nd, 2019 at 07:41 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    Thanks, there is some interesting code there.

    It answers my first question of creating controls in the main WindProc function: i.e, the main loop.

    Now if someone can please answer the second part:

    Is there a function that I can place in the WinProc function that will create the controls in a Resource Template like:
    Code:
    int ret = DialogBox(GetModuleHandle(NULL), 
    						MAKEINTRESOURCE(IDD_MY_DIALOG_HW), hWnd, DlgProc_My_HW);
    if I could place the DialogBox inside the client area and make it resizable too it should work. I should be possible: Using the dialogs box WndProc in my case DlgProc_My_HW would work.

    ... or maybe somehow callling the Dialog's Procedure, DlgProc_My_HW from the main's WndProc.

    Another solution is to created Multiple Dialog boxes with menus in a Dialog App and switch between them, hiding the inactive one or destroying it after a different one is created or shown?

    I think I would still like to explore the solution of placing a Dialog box in the client area, maybe placing the Main Windows message loop function , WindProc , inside the function DialogBox(...) instead of DlgProc_My_HW: i.e

    Code:
    int ret = DialogBox(GetModuleHandle(NULL), 
    						MAKEINTRESOURCE(IDD_MY_DIALOG_HW), hWnd, WinProc);
    Last edited by ADSOFT; January 2nd, 2019 at 02:07 PM.
    Rate this post if it helped you.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    if I could place the DialogBox inside the client area and make it resizable too it should work
    There are two types of dialog box (or 3 if you count system modal) - modal and non-modal (modeless) dialog boxes. A modal dialog box is one that becomes the programs active window when created and stays as the programs active window until the dialog box is destroyed. No other program window can become active whilst the modal dialog is in use. A modal dialog is created using DialogBox().

    A modeless dialog behaves much like a child window. When created it becomes the active window, but other program windows can still be used if needed. A modeless dialog is created using CreateDialog().

    The way that dialog messages are handled are also different between the 2 types. See https://docs.microsoft.com/en-us/win...t-dialog-boxes

    Which one are you referring to when you want to create inside the client area? You can create a modeless dialog and have it move etc when the parent window is moved and keep it as the top window etc etc by sending appropriate messages to it from the main window message loop. You can also sub-class the dialog box and change some of its behaviour etc etc. You can even create a program with modeless dialogs without having a main window!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    If you're going to do much WIN32 windows programming, as well as Petzold's 'Programming Windows' I suggested in post #2, I would also suggest 'Windows 95: A Developer's Guide' by Jeffrey Richter https://www.amazon.co.uk/Windows-95-...ywords=richter. I know it refers to Windows 95 and is quite an old book, but programming windows using WIN32 hasn't changed that much (just a few extra APIs) and this book goes into detail about using dialog boxes, sub-classing, hooking etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Jun 2004
    Posts
    1,352

    Re: Creating FORMVIEW WIN32 style app: How to place Formview Resource in the Mainfo

    Thanks , I have both those books. I went the MFC route and it worked pretty well because I could be productive with it, now that I have time I want to learn WIN32, some of the content in those books is for 16bit apps and is outdated isn't it?

    Regarding you answers:

    I like the CreateDialog approach and making it a child window to the main window. That sounds like something that might work.

    Also a MDI WIN32 app might do the job.

    I just want something that would take a resource template and create all the controls in the main window.

    A dialogbox with a menu might also work, and then I can swap dialog boxes. I have seen programs with a dialog box in WinMain. Having a switch statement in WinMain with multiple Dialog boxes with menus might work.

    I think I like Modless idea. ... I have to experiment with that.
    Rate this post if it helped you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured