Saturday, March 19, 2011

Digital clock source code


This is a source code for time. It will display in 24-hour formate hr/min/sec. You can use this source code for timing in your game in a separate thread.

#include <stdio.h>
#include <time.h>         //this is header file for time
#include <windows.h>
void ClearConsoleToColors(int ForgC, int BackC);
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
    coord.X = x; coord.Y = y; // X and Y coordinates
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void delay(unsigned int mseconds)
{
    clock_t goal = mseconds + clock();
    while (goal > clock());
}

int main()
{

    long i = 0;                      /* Loop counter              */
    clock_t now = 0;                 /* Holds initial clock time  */
    int interval = 1;                /* Seconds interval for o/p  */
    int elapsed = 0;
    int min=0,MIN=0,hrs=0,sec=0;
    int d=0,f=0;
    now = clock();
    ClearConsoleToColors(2,1);               /* Get current clock time    */

Move console windows using C++ and windows function.

You can move your console windows at your desired location on-screen and also resize the size of console windows by using this source code in code::block.

#include <windows.h>
#include<stdio.h>
HWND WINAPI GetConsoleWindowNT(void)
{
    // declare function pointer type

    typedef HWND WINAPI (*GetConsoleWindowT)(void);

    // declare one such function pointer

    GetConsoleWindowT GetConsoleWindow;

    // get a handle on kernel32.dll

    HMODULE hK32Lib = GetModuleHandle(TEXT("KERNEL32.DLL"));

    // assign procedure address to function pointer

    GetConsoleWindow = (GetConsoleWindowT)GetProcAddress(hK32Lib,TEXT("GetConsoleWindow"));

    // check if the function pointer is valid

    // since the function is undocumented

    if ( GetConsoleWindow == NULL ) {
         return NULL;
    }

    // call the undocumented function

    return GetConsoleWindow();
}



int main()
{
    HWND hWnd=GetConsoleWindowNT();
    MoveWindow(hWnd,1230,750,200,100,TRUE);

}

Change console windows background color in code::block



Use this source code to change console windows background color in code::block.
Pass integer value range 0 to 256 in ClearConsoleToColors(  x, y) function.

#include <windows.h>          //header file for windows
#include <stdio.h>

void ClearConsoleToColors(int ForgC, int BackC);
int main()
{
    ClearConsoleToColors(0,1);
    Sleep(1000);
    return 0;
}
void ClearConsoleToColors(int ForgC, int BackC)
{
     WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
                   //Get the handle to the current output buffer...
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
                         //This is used to reset the carat/cursor to the top left.
     COORD coord = {0, 0};
                      //A return value... indicating how many chars were written
                        //   not used but we need to capture this since it will be
                          //   written anyway (passing NULL causes an access violation).
     DWORD count;

                                   //This is a structure containing all of the console info
                          // it is used here to find the size of the console.
     CONSOLE_SCREEN_BUFFER_INFO csbi;
                     //Here we will set the current color
     SetConsoleTextAttribute(hStdOut, wColor);
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
                              //This fills the buffer with a given character (in this case 32=space).
          FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

          FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
                              //This will set our cursor position for the next print statement.
          SetConsoleCursorPosition(hStdOut, coord);
     }
     return;
}


Text background color of console windows using C in code::block


Use this source code to change the text background color of console windows in code::block

#include <windows.h>
#include <stdio.h>
void SetColorAndBackground(int ForgC, int BackC);
int main()
{
    SetColorAndBackground(5,1);   //color value range 0 up-to 256
    printf("what is text background color");
    Sleep(1000);
    return 0;
}
void SetColorAndBackground(int ForgC, int BackC)
{
     WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
     return;
}

changing text color in codeblock


If you want to change the color of the text in console windows without using graphic then you can use this code in code::blocks.

#include <windows.h>         //header file for windows
#include <stdio.h>                          //C standard library
void SetColor(int ForgC);
int main()
{
    SetColor(34);
    printf("test clour");
    return 0;
}
void SetColor(int ForgC)
{
     WORD wColor;
                          //We will need this handle to get the current background attribute
     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
     CONSOLE_SCREEN_BUFFER_INFO csbi;

                           //We use csbi for the wAttributes word.
     if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
     {
                     //Mask out all but the background attribute, and add in the forgournd color
          wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
          SetConsoleTextAttribute(hStdOut, wColor);
     }
     return;
}


you can change your desire  text color by passing integer value range 0 up-to 256 in SetColor( ) function.

Thursday, March 17, 2011

gotoxy in code::block

gotoxy(int x,int y) is a very important function which helps to move the cursor at our desire location of screen .gotoxy(int x,int y)  is built-in function of turbo c  but in gcc compiler, it won't work. If you are using code::blocks IDE then you may get an error with gotoxy. We have to add code for gotoxy in gcc or codeblocks. This code used windows.h header file in another word we can say its not the c code it is the widows' code. It won't work in the Linux machine because there is no windows.h library.




source code(or download)  for gotoxy is as follows:
 #include<windows.h> //  header file for gotoxy
 #include <stdio.h>     //header file for standard input output

  COORD coord={0,0}; // this is global variable
                                    //center of axis is set to the top left corner of the screen
 void gotoxy(int x,int y)
 {
   coord.X=x;
 coord.Y=y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
 }
  int main()
{

  //calling these function
  gotoxy(20,20);printf("This is demo of gotoxy function");
            
   return 0;
  }

Sunday, March 13, 2011

Mini project "library management" in C programming


      This is my college 1st-semester mini-project “library management “ in C programming language. This is a console application without graphics and compiled in code::block with GCC compiler. I hope it will help you to develop your own project similar project related to file handling for example “Banking “,” customer records”,” student records" etc.
I have divided my project into many functions and I will describe each of them which may help you to understand better.