Relative position of text box on window resize

This is a discussion on Relative position of text box on window resize within the Windows Programming forums, part of the Platform Specific Boards category; I am trying to render a text box in the center of my program window. When I run the program, ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    Relative position of text box on window resize

    I am trying to render a text box in the center of my program window. When I run the program, my text box is not centered in the window. But upon expanding the window, the textbox become centered, as expected. Please help me understand why my program doesn't center the text-box, until I expand the window. My goal is to keep the text-box centered, however the window is sized/resized. I really appreciate it.




    Code:
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         static int xwindow, ywindow;  
    	 static HRGN hrgn;
         HDC         hdc, MemDCExercising;
         HWND         hwnd2;
    	 PAINTSTRUCT ps ;
         HBITMAP bmpExercising;
    
    
    
         switch (message)
         {
    
         case WM_CREATE:
               
               
    		   
    		    SetTimer(hwnd, TIMER_1, 5000, NULL);     
    		   
    
    GetWindowRect (hwnd, &CurrentWindowSize);
    parentwindowwidth = CurrentWindowSize.right - CurrentWindowSize.left;
    parentwindowheight = CurrentWindowSize.bottom - CurrentWindowSize.top;
    
    
    CreateWindow(TEXT("edit"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER, .5 * parentwindowwidth, .5 * parentwindowheight, 500, 100, hwnd, NULL, NULL, hwnd2, NULL);
    	  			 	 
    			 	
    		   return 0 ;
    
    
    
    	   
    
         case WM_PAINT:
              
    		 hdc = BeginPaint (hwnd, &ps) ;
              
                       
    
     //SetTextColor(hdc, RGB(0,255,0));
     //SetBkColor(hdc, 0);
       bmpExercising =  (HBITMAP) LoadImage(hInst, TEXT("notepad.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    	    // Load the bitmap from the resource
    	   // bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_EXERCISING));
    	    // Create a memory device compatible with the above DC variable
    	    MemDCExercising = CreateCompatibleDC(hdc);
                 // Select the new bitmap
                 SelectObject(MemDCExercising, bmpExercising);
    
    	    	 
    	    // Copy the bits from the memory DC into the current dc
    	   
    		StretchBlt(hdc, 0, 0, 64, 64, MemDCExercising, 0, 0, 64, 64, SRCCOPY);
    			 
    			 			
    
    	    // Restore the old bitmap
    	    DeleteDC(MemDCExercising);
    	    DeleteObject(bmpExercising);
    
    
              EndPaint (hwnd, &ps) ;
       		  return 0 ;
    	
    
    		  
    
    
         case WM_TIMER:
    		 
               InvalidateRect (hwnd, NULL, TRUE);
    		   UpdateWindow(hwnd);
    
    		 return 0;
    
    
    
         case WM_SIZE:
             xwindow = LOWORD (lParam);
    		 ywindow = HIWORD (lParam);
           
    		 return 0;
    
    
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
    	 
    	 return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    Last edited by zadeh79; 03-23-2011 at 08:15 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Send a WM_SIZE message to your main window...

    Code:
    case  WM_CREATE :
       // do stuff
      
      PostMessage(MainWindow,WM_SIZE,0,MAKELPARAM(MWidth,MHeight));
      return 0;
    Your WM_SIZE handler then uses MoveWindow to position all children within the parent window.

    That should initialize the window positions for you as soon as your message dispatcher starts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find/Replace problem
    By TAZIN in forum Windows Programming
    Replies: 6
    Last Post: 08-16-2010, 08:07 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  3. Making dialog box the only window
    By PJYelton in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2005, 01:02 PM
  4. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 09:34 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21