Thread: Relative position of text box on window resize

  1. #1
    Registered User
    Join Date
    Mar 2011

    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.

    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 -;
    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
              EndPaint (hwnd, &ps) ;
       		  return 0 ;
         case WM_TIMER:
               InvalidateRect (hwnd, NULL, TRUE);
    		 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
    Join Date
    Aug 2010
    Ontario Canada
    Send a WM_SIZE message to your main window...

    case  WM_CREATE :
       // do stuff
      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
Website Security Test