Thread: Centering on the screen

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    2

    Question Centering on the screen

    I was wondering if someone could help me with a program I am working on. I'm designing a C console application and I am trying to find a way to center text on the screen. so that it is in the middle from the top to the bottom and from left to right. Any ideas?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I won't supply you with the code, but will supply you with some math.


    int column=center_of_screen_column-(textwidth_in_columns/2);
    int row=center_of_screen_row-(textheight_in_rows/2);

  3. #3
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    Which version do you use
    if it borland c
    then
    midx=getmaxx()/2;
    midy=getmaxy()/2;
    outtextxy(midx,midy,"text");
    and u have to include graphics.h also.
    AbHHinaay

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    2
    I'm using .Net

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    You have to find the .net functions to get the size of the console window. Then, of course, follow planet_abhi's solution.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    midx=getmaxx()/2;
    midy=getmaxy()/2;
    outtextxy(midx,midy,"text");
    Won't work. It does not take into account the textheight or textwidth of text. The formulas I supplied are standard formulas for typing and layout and will center the text no matter how tall it is and no matter how wide it is, given that the text does fit into the dimensions of the window.

  7. #7
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    This works by centering the text across a line....you can modify it to move to the center of the screen vertically too...

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define TEXT_WIDTH  80
    
    void centre(char[], int);
    
    int main(void)
    {
        char text[81];
        
        puts("Please enter a phrase:");
        gets(text);
        centre(text, TEXT_WIDTH);
        
        system("pause");
        return 0;
    }
    
    
    void centre(char text[], int width)
    {
        char outtext[1+width];
        int n, len, pre;
    
        len = strlen(text);
    
        pre = (width - len) / 2;
        for (n=0; n < pre; n++) {
            outtext[n] = ' ';
            }
    
        outtext[pre] = '\0';
    
        strcat(outtext, text);
    
        for (n=len+pre; n < width; n++) {
                outtext[n] = ' ';
            }
        outtext[width] = '\0';
        printf("%s",outtext);
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > gets(text);
    You are about to enter the twilight zone
    [music]Do do do do do do[/music]

    > char outtext[1+width];
    C89 (the most widely available version of C) doesn't allow variable length arrays.
    You need C99 (the new C standard) or C++ to be able to do this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    HEY...

    >You are about to enter the twilight zone

    I just copy/pasted that code from one of my old archived C files....didnt even bother looking at it....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Resizing the screen while centering the app
    By csonx_p in forum Windows Programming
    Replies: 5
    Last Post: 07-03-2008, 04:29 AM
  2. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  3. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM