Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Multiple windows - (http://www.programmingforums.org/showthread.php?t=14999)

pcbrainbuster Jan 21st, 2008 3:31 PM

Multiple windows -
 
Sup dudes,

When you first create that mian rectangle window in which everything will be based on, how do you create ANOTHER one of those?

Thanks.

Ancient Dragon Jan 21st, 2008 4:01 PM

Re: Multiple windows -
 
>>how do you create ANOTHER one of those
The same way you created the first one.

pcbrainbuster Jan 21st, 2008 5:28 PM

Re: Multiple windows -
 
But when I give the same class name as the first it looks exactly the same as the first window.

Any ideas?

Thanks.

Ancient Dragon Jan 21st, 2008 5:56 PM

Re: Multiple windows -
 
But I thought you said you wanted to create a second window that looked exactly like the first? Or maybe I misunderstood. You can register as many different classes as you like. If you want the second window to look a little different than the first then register another window class just like you did the first one.

If you want the second window to be a child of the first and bounded by the first window's rectangle then pass a handle of the first window as the parent to the second window.

pcbrainbuster Jan 22nd, 2008 12:21 PM

Re: Multiple windows -
 
Oh damn! No matter what I do I can't seem to create a second window, also I can't seem to find any resources to learn from :(

I'm sorry that I'm asking of this; but could you please create an example in which there are two SEPARATE windows.

Thanks a lot(hopefully :-|)!

Ancient Dragon Jan 22nd, 2008 1:12 PM

Re: Multiple windows -
 
This is using VC++ 2008 Express. It creates two identical windows. Most of the code was generated by the IDE when I created the project. All I did was add a second class name and changed the *.rc file to contain a second string.

:

  1. // testwin.cpp : Defines the entry point for the application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "testwin.h"
  6.  
  7. #define MAX_LOADSTRING 100
  8.  
  9. // Global Variables:
  10. HINSTANCE hInst;                                                                // current instance
  11. TCHAR szTitle[MAX_LOADSTRING];                                        // The title bar text
  12. TCHAR szWindowClass1[MAX_LOADSTRING];                        // the main window class name
  13. TCHAR szWindowClass2[MAX_LOADSTRING];                        // the main window class name
  14.  
  15. // Forward declarations of functions included in this code module:
  16. ATOM                                MyRegisterClass(HINSTANCE hInstance, LPCTSTR szWindowClass);
  17. BOOL                                InitInstance(HINSTANCE, int, LPCTSTR);
  18. LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
  19. INT_PTR CALLBACK        About(HWND, UINT, WPARAM, LPARAM);
  20.  
  21. int APIENTRY _tWinMain(HINSTANCE hInstance,
  22.                     HINSTANCE hPrevInstance,
  23.                     LPTSTR    lpCmdLine,
  24.                     int      nCmdShow)
  25. {
  26.         UNREFERENCED_PARAMETER(hPrevInstance);
  27.         UNREFERENCED_PARAMETER(lpCmdLine);
  28.  
  29.         // TODO: Place code here.
  30.         MSG msg;
  31.         HACCEL hAccelTable;
  32.  
  33.         // Initialize global strings
  34.         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  35.         LoadString(hInstance, IDC_TESTWIN1, szWindowClass1, MAX_LOADSTRING);
  36.         LoadString(hInstance, IDC_TESTWIN2, szWindowClass2, MAX_LOADSTRING);
  37.         MyRegisterClass(hInstance, szWindowClass1);
  38.         MyRegisterClass(hInstance, szWindowClass2);
  39.  
  40.         // Perform application initialization:
  41.         if (!InitInstance (hInstance, nCmdShow, szWindowClass1))
  42.         {
  43.                 return FALSE;
  44.         }
  45.         if (!InitInstance (hInstance, nCmdShow, szWindowClass2))
  46.         {
  47.                 return FALSE;
  48.         }
  49.  
  50.         hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTWIN));
  51.  
  52.         // Main message loop:
  53.         while (GetMessage(&msg, NULL, 0, 0))
  54.         {
  55.                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  56.                 {
  57.                         TranslateMessage(&msg);
  58.                         DispatchMessage(&msg);
  59.                 }
  60.         }
  61.  
  62.         return (int) msg.wParam;
  63. }
  64.  
  65.  
  66.  
  67. //
  68. //  FUNCTION: MyRegisterClass()
  69. //
  70. //  PURPOSE: Registers the window class.
  71. //
  72. //  COMMENTS:
  73. //
  74. //    This function and its usage are only necessary if you want this code
  75. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  76. //    function that was added to Windows 95. It is important to call this function
  77. //    so that the application will get 'well formed' small icons associated
  78. //    with it.
  79. //
  80. ATOM MyRegisterClass(HINSTANCE hInstance, LPCTSTR szWindowClass)
  81. {
  82.         WNDCLASSEX wcex;
  83.  
  84.         wcex.cbSize = sizeof(WNDCLASSEX);
  85.  
  86.         wcex.style                        = CS_HREDRAW | CS_VREDRAW;
  87.         wcex.lpfnWndProc        = WndProc;
  88.         wcex.cbClsExtra                = 0;
  89.         wcex.cbWndExtra                = 0;
  90.         wcex.hInstance                = hInstance;
  91.         wcex.hIcon                        = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTWIN));
  92.         wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
  93.         wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
  94.         wcex.lpszMenuName        = MAKEINTRESOURCE(IDC_TESTWIN);
  95.         wcex.lpszClassName        = szWindowClass;
  96.         wcex.hIconSm                = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
  97.  
  98.         return RegisterClassEx(&wcex);
  99. }
  100.  
  101. //
  102. //  FUNCTION: InitInstance(HINSTANCE, int)
  103. //
  104. //  PURPOSE: Saves instance handle and creates main window
  105. //
  106. //  COMMENTS:
  107. //
  108. //        In this function, we save the instance handle in a global variable and
  109. //        create and display the main program window.
  110. //
  111. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow, LPCTSTR szWindowClass)
  112. {
  113.   HWND hWnd;
  114.  
  115.   hInst = hInstance; // Store instance handle in our global variable
  116.  
  117.   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  118.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  119.  
  120.   if (!hWnd)
  121.   {
  122.       return FALSE;
  123.   }
  124.  
  125.   ShowWindow(hWnd, nCmdShow);
  126.   UpdateWindow(hWnd);
  127.  
  128.   return TRUE;
  129. }
  130.  
  131. //
  132. //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  133. //
  134. //  PURPOSE:  Processes messages for the main window.
  135. //
  136. //  WM_COMMAND        - process the application menu
  137. //  WM_PAINT        - Paint the main window
  138. //  WM_DESTROY        - post a quit message and return
  139. //
  140. //
  141. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  142. {
  143.         int wmId, wmEvent;
  144.         PAINTSTRUCT ps;
  145.         HDC hdc;
  146.  
  147.         switch (message)
  148.         {
  149.         case WM_COMMAND:
  150.                 wmId    = LOWORD(wParam);
  151.                 wmEvent = HIWORD(wParam);
  152.                 // Parse the menu selections:
  153.                 switch (wmId)
  154.                 {
  155.                 case IDM_about:
  156.                         DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
  157.                         break;
  158.                 case IDM_EXIT:
  159.                         DestroyWindow(hWnd);
  160.                         break;
  161.                 default:
  162.                         return DefWindowProc(hWnd, message, wParam, lParam);
  163.                 }
  164.                 break;
  165.         case WM_PAINT:
  166.                 hdc = BeginPaint(hWnd, &ps);
  167.                 // TODO: Add any drawing code here...
  168.                 EndPaint(hWnd, &ps);
  169.                 break;
  170.         case WM_DESTROY:
  171.                 PostQuitMessage(0);
  172.                 break;
  173.         default:
  174.                 return DefWindowProc(hWnd, message, wParam, lParam);
  175.         }
  176.         return 0;
  177. }
  178.  
  179. // Message handler for about box.
  180. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  181. {
  182.         UNREFERENCED_PARAMETER(lParam);
  183.         switch (message)
  184.         {
  185.         case WM_INITDIALOG:
  186.                 return (INT_PTR)TRUE;
  187.  
  188.         case WM_COMMAND:
  189.                 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  190.                 {
  191.                         EndDialog(hDlg, LOWORD(wParam));
  192.                         return (INT_PTR)TRUE;
  193.                 }
  194.                 break;
  195.         }
  196.         return (INT_PTR)FALSE;
  197. }


pcbrainbuster Jan 22nd, 2008 3:36 PM

Re: Multiple windows -
 
I think it would be more efficient for me to learn how to work with dialogs... :(

Could you please point me to a tutorial(I would search Google but its usually better to ask someone with experience to point you to one mainly because they know the best one(s))?

Thanks.

pcbrainbuster Jan 22nd, 2008 3:37 PM

Re: Multiple windows -
 
Oh yeah, I forgot to mension; I want a tutorial that teaches you how to work with Dialog Boxes WITHOUT resource templates...

Thanks again. :)

Sane Jan 22nd, 2008 3:43 PM

Re: Multiple windows -
 
Quote:

Originally Posted by pcbrainbuster (Post 140082)
Oh yeah, I forgot to mension; I want a tutorial that teaches you how to work with Dialog Boxes WITHOUT resource templates...

Thanks again. :)

You mean... simply a second window?

lectricpharaoh Jan 22nd, 2008 7:24 PM

Re: Multiple windows -
 
You might consider C# or VB.NET, as the visual designers with these compilers tend to make this sort of thing a lot easier. After you've gotten more comfortable with programming, then you'll be better equipped to deal with the Win32 API.


All times are GMT -5. The time now is 10:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC