• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

gotoxy or cursorto

Hi,

Is there any ANSI C function like GotoXY or CursorTo of Pascal ?

Andrew
0
andrewyu
Asked:
andrewyu
  • 7
  • 5
  • 4
  • +2
1 Solution
 
julio011597Commented:
ANSI C does not deal with screens, i'm afraid.

There's a gotoxy() from conio.h.

HTH, julio
0
 
julio011597Commented:
BTW, that applies to DOS... under Unix, you have dive a bit into the curses library:

man curses
0
 
andrewyuAuthor Commented:
But, I cannot find this in VC5 !

And I also want to know how to clear the screen ?

Andrew
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RONSLOWCommented:
You want the Win32 console API's .. they are quite extensive

You can clear the screen, fill recatnagles with characters and colours, set text position and colour etc.

Would you like further info??

0
 
julio011597Commented:
To clear the screen clrscr(). Did you look for <conio.h> in your help docs?

Anyway, as said this is Dos stuff. If you are willing to deal with windows under Win, feel free to repoen this question.

Regards.
0
 
RONSLOWCommented:
Please reopen the question so I can post an answer relevant to Win32 .. the console support is quite good under Win32 (95 and NT).

0
 
alexoCommented:
SetConsoleCursorPosition() will position the cursor.
Clearing the screen is not directly supported but there's a workaround described in KB article Q99261.
0
 
julio011597Commented:
I've not got VC, so cannot do it myself, but wander WHY nobody cares to give andrewyu a topic to look at in his help file?... fighting for 5 points??

I will never understand this area, guys. Sorry.

-julio
0
 
alexoCommented:
julio, I gave him two topics.  One, a function name.  Another, a KB article.  Anything wrong with that?

Ponts?  What points?
0
 
julio011597Commented:
My comment was basically directed to RONSLOW... no, there's nothing wrong.
BTW, i thought to be *polite*, whoever my comment was meant to.

-julio
0
 
RONSLOWCommented:
I asked if info was required on Win32 because info for DOS only was given.

I will gladly put up the info if it is required.

I am not fighting for points .. just wondering if the answer supplied was OK .. it not is can be rejected and I'll post an answer that addresses WIn32.


0
 
RONSLOWCommented:
CONGUI smaple code .. GUI.C


GUI.C

/******************************************************************************\
*       This is a part of the Microsoft Source Code Samples.  
*       Copyright (C) 1993-1997 Microsoft Corporation.
*       All rights reserved.  
*       This source code is only intended as a supplement to  
*       Microsoft Development Tools and/or WinHelp documentation.
*       See these sources for detailed information regarding the  
*       Microsoft samples programs.
\******************************************************************************/
 
// GUI.C
// ================================================================
// This module contains all of the functions that interface to the
// 'graphical' part of this program. This currently only relates to
// the code that brings up the dialog box, and calls the WinHelp
// engine.
 
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include "ConGUI.h"
 
int DoHelp (char *szHelpTopic);
int GetDialogArgs (char ***pargv);
BOOL CenterWindow (HWND hwnd);
BOOL APIENTRY CLDlgProc (HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam);
 
 
// Use WINHELP to bring up the applicaiton help file
int DoHelp (char *szHelpTopic)
{
    WinHelp (GetFocus(), "ConGUI.HLP", HELP_KEY, (DWORD)(LPSTR)szHelpTopic);
    return TRUE;
}
 
 
// Bring up the dialog box, and pass back a 'command line' as was
// specified by the user
int GetDialogArgs (char ***pargv)
{
    int ret;
    HANDLE hinst;
    HWND hwnd;
    char szFile[80];
 
    hinst = GetModuleHandle (NULL);
    hwnd = GetFocus();
 
    ret = DialogBoxParam (hinst, "CL", NULL, CLDlgProc, (LPARAM)pargv);
 
    if (-1 == ret) {
        ret = GetLastError();
        printf ("Unable to create dialog: %d\n", ret);
        GetModuleFileName (hinst, szFile, sizeof(szFile));
        printf ("hinst = %d\n", hinst);
        printf ("hwnd = %d\n", hwnd);
        printf ("File = %s\n", szFile);
        return FALSE;
 
    }
    return ret;
}
 
// A quick little routine that will center a window on the screen.
// Handy for dialog boxes
BOOL CenterWindow (HWND hwnd)
{
    RECT    rect;
    int     w, h;
    int     wScreen, hScreen, xNew, yNew;
    HDC     hdc;
 
    GetWindowRect (hwnd, &rect);
    w = rect.right - rect.left;
    h = rect.bottom - rect.top;
 
    hdc = GetDC (hwnd);
    wScreen = GetDeviceCaps (hdc, HORZRES);
    hScreen = GetDeviceCaps (hdc, VERTRES);
    ReleaseDC (hwnd, hdc);
 
    xNew = wScreen/2 - w/2;
    yNew = hScreen/2 - h/2;
 
    return SetWindowPos (hwnd, NULL, xNew, yNew, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
 
 
// Create a data structure that will hold the strings for the combo boxes
// we have in our dialog. This just illustrates 'a' way to do this, not
// necessarily the best.
 
typedef struct tagDlgCtrls {
    int ctrlId;
    int def;
    char str[25];
    char opt[5];
} DlgCtrls;
 
DlgCtrls dlgctrls[] = {
    { 415, FALSE, "DOS EXE", "..." },
    { 415, FALSE, "Windows 3.0 EXE", "..."  },
    { 415, FALSE, "Windows 3.0 DLL", "..."  },
    { 415, FALSE, "Windows 3.1 EXE", "..."  },
    { 415, FALSE, "Windows 3.1 DLL", "..."  },
    { 415, TRUE,  "Windows NT EXE", "..."  },
    { 415, FALSE, "Windows NT DLL", "..."  },
    { 415, FALSE, "Windows NT Console App", "..."  },
 
    { 402, FALSE, "Small", "AS" },
    { 402, FALSE, "Medium", "AM" },
    { 402, FALSE, "Compact", "AC" },
    { 402, TRUE,  "Large", "AL" },
    { 402, FALSE, "Huge", "AH" },
    { 402, FALSE, "Customize", "A?" },
 
    { 404, FALSE, "8086", "G0" },
    { 404, FALSE, "80186", "G1" },
    { 404, TRUE,  "80286", "G2" },
    { 404, FALSE, "80386", "G3" },
    { 404, FALSE, "80486", "G4" },
 
    { 406, TRUE,  "stdcall", "Gz" },
    { 406, FALSE, "Pascal", "Gc" },
    { 406, FALSE, "C", "Gd" },
 
    { 408, FALSE, "Level 0", "W0" },
    { 408, FALSE, "Level 1", "W1" },
    { 408, FALSE, "Level 2", "W2" },
    { 408, TRUE,  "Level 3", "W3" },
    { 408, FALSE, "Level 4", "W4" },
 
    { 411, FALSE, "None", "" },
    { 411, FALSE, "Line Numbers Only", "Zd" },
    { 411, TRUE,  "Full Information", "Zi" },
 
    { 418, FALSE, "Ansi C", "Za" },
    { 418, TRUE, "MS Extensions", "Ze" },
 
    { 413, FALSE, "None", "" },
    { 413, TRUE,  "Protect Mode App", "GA" },
    { 413, FALSE, "Protect Mode DLL", "GD" },
 
    { 0, 0}  // End Of List
};
 
 
BOOL APIENTRY CLDlgProc (HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    int wmId;
    static char ***pargv;
    static char **argv;
 
    int i, item, index, iCtrl, argc;
    char *cmd;
    char *cmdline;
 
    switch (msg) {
        case WM_INITDIALOG:
            // We need to initialize stuff in the dialog box...
 
            pargv = (char ***)lParam;
            argv = *pargv;
            CenterWindow (hdlg);
 
            iCtrl = i = 0;
            while (dlgctrls[i].ctrlId) {
                if (dlgctrls[i].ctrlId != iCtrl) { // Starting a new list
                    iCtrl = dlgctrls[i].ctrlId;
                }
                index = SendDlgItemMessage (hdlg, iCtrl, CB_ADDSTRING, 0, (DWORD)(LPSTR)dlgctrls[i].str);
                SendDlgItemMessage (hdlg, iCtrl, CB_SETITEMDATA, index, i);
                if (dlgctrls[i].def) {
                    SendDlgItemMessage (hdlg, dlgctrls[i].ctrlId, CB_SETCURSEL, index, 0);
                }
                i++;
            }
            return (TRUE);
 
        case WM_DESTROY:
            break;
 
        case WM_COMMAND:
            wmId = LOWORD(wParam);
            switch (wmId) {
 
                case T_HELP:
                    DoHelp ("Contents");
                    break;
 
                case IDOK:
                    cmd = cmdline = (char *)GlobalAlloc (GPTR, 128);
                    argv[0] = cmdline;
                    argc = 0;
 
                    if (cmdline) {
                        iCtrl = i = 0;
                        while (dlgctrls[i].ctrlId) {
                            if (dlgctrls[i].ctrlId != iCtrl) {
                                iCtrl = dlgctrls[i].ctrlId;
                                index = SendDlgItemMessage(hdlg, iCtrl, CB_GETCURSEL, 0, 0);
                                if (index) {
                                    item = SendDlgItemMessage (hdlg, iCtrl, CB_GETITEMDATA, index, 0);
                                    wsprintf ((LPSTR)cmd, "-%s", (LPSTR)dlgctrls[item].opt);
                                    cmd += strlen(cmd);
                                    cmd[0] = 0;
                                    argv[++argc] = ++cmd;
                                }
                            }
                            i++;
                        }
 
                    } // if (cmdline)...
 
                    EndDialog(hdlg, argc);
                    return (TRUE);
 
                case IDCANCEL:
                    EndDialog(hdlg, 0);
                    return (TRUE);
            }
            break;
    }
    return (FALSE);
 
    lParam; // unreferenced formal parameter
}

0
 
RONSLOWCommented:
Oops wrong one ... here is more relevant code from CONSOLE.c in Console app.  This has some useful routines and examples of how to do what you require

/*
* FUNCTION: myPuts(HANDLE hConsole, PCHAR s)
* PURPOSE: write a string to the given console buffer, appending a cr/lf
* INPUT: the console to write to, and the string to write
* RETURNS: TRUE if success, FALSE if an error occured
*/  
BOOL myPuts(HANDLE hConsole, PCHAR s) {   BOOL bSuccess;   DWORD cCharsWritten;
  const PCHAR crlf = "\n";   BOOL retflag = TRUE;  
  /* write the string to the console */
  bSuccess = WriteConsole(hConsole, s, strlen(s), &cCharsWritten, NULL);
  retflag = bSuccess;
  /* put a carriage return & line feed after the string */
  bSuccess = WriteConsole(hConsole, crlf, strlen(crlf), &cCharsWritten, NULL);
  if (!bSuccess) retflag = FALSE;
  return(retflag);
}  

/*
* FUNCTION: getConX(HANDLE hCon)
* PURPROSE: to get the current width of the console output buffer
* INPUT: the handle to get the information for
* RETURNS: the width of the current console output buffer, in chars
*/
SHORT getConX(HANDLE hCon) {   CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hCon, &csbi);
  return(csbi.dwSize.X);
}  

/*
* FUNCTION: getConY(HANDLE hCon)
* PURPROSE: to get the current height of the console output buffer
* INPUT: the handle to get the information for
* RETURNS: the height of the current console output buffer, in chars
*/
SHORT getConY(HANDLE hCon) {   CONSOLE_SCREEN_BUFFER_INFO csbi;
  GetConsoleScreenBufferInfo(hCon, &csbi);
  return(csbi.dwSize.Y);
}

/*
* FUNCTION: putStatusLine(HANDLE hOut, PCHAR buf)
* PURPOSE: display a string on the top line of the console output buffer
* INPUT: the output handle, the string to display
* RETURNS: none
*/  
void putStatusLine(HANDLE hOut, PCHAR buf) {   BOOL bSuccess;
  /* position the status line at (0, 0) */   const COORD dwWriteCoord = {0, 0};
  DWORD cCharsWritten;
  int len; /* the length of the input string parameter */   CHAR szTemp[256];
  SHORT sWidth; /* console width */    sWidth = getConX(hOut);
  strcpy(szTemp, buf);   len = strlen(szTemp);
  memset(szTemp + len, ' ', sWidth - len);  /* blank out rest of line */
  /* write the string to the console at the correct position */
  WriteConsoleOutputCharacter(hOut, szTemp, sWidth,
      dwWriteCoord, &cCharsWritten);
  /* color the status line so it stands out */
  FillConsoleOutputAttribute(hOut, FOREGROUND_RED |
      BACKGROUND_WHITE, sWidth, dwWriteCoord, &cCharsWritten);
  return;
}  

/*
* FUNCTION: cls(HANDLE hConsole)
* PURPOSE: clear the screen by filling it with blanks, then home cursor
* INPUT: the console buffer to clear
* RETURNS: none
*/  
void cls(HANDLE hConsole) {
  COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
  DWORD dwConSize; /* number of character cells in the current buffer */  
  /* get the number of character cells in the current buffer */
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  /* fill the entire screen with blanks */
  FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten);
  /* get the current text attribute */
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  /* now set the buffer's attributes accordingly */
  FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten);
  /* put the cursor at (0, 0) */
  SetConsoleCursorPosition(hConsole, coordScreen);
  return;
}  
/*
* FUNCTION: setConTitle
* PURPOSE: simply set the current console title. Called by each demo
*          function to specify the name of the source file that
*          contains the demo function.
* INPUT: null terminated string
*/  
void setConTitle(PCHAR szTitle) {   BOOL bSuccess;  
  /* set the console title to the input string parameter */
  SetConsoleTitle(szTitle);
  return;
}

0
 
seedyCommented:
All for a person who never says "thank you" - or grades you :-)
Well, it is a thank less world out there.
0
 
alexoCommented:
Looking at his info:
>> Last 10 Grades Given
>> C C C C C C C C C C

A fine case of an asshole if I ever saw one.
0
 
julio011597Commented:
It seems i'm gonna get the C grade... fine!
BTW, i've recently started my personal "banned users" list - that is, users not to support anymore: three names up to now. Maybe there's a new candidate?
Anyway, let's see what happens before shooting... :)
0
 
alexoCommented:
>> t seems i'm gonna get the C grade... fine!
And yes!  We have a winner!  :-)

Don't worry, he knows how to grade.  Would you like a 'D' instead?

>> C D D C C C C C C C
0
 
andrewyuAuthor Commented:
Thank you very much and I have already got my answer !!!!!!
0
 
julio011597Commented:
Geez... you must be 12, aren't you?

Good luck, julio
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now