7 Replies - 3019 Views - Last Post: 10 May 2012 - 05:11 AM Rate Topic: -----

#1 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

How Would You Simulate PrintScreen In Code

Posted 03 May 2012 - 10:16 AM

Hi,
How would you simulate the printscreen buton being pressed in C++ Code?

Thank you in advance,
L99
Is This A Good Question/Topic? 0
  • +

Replies To: How Would You Simulate PrintScreen In Code

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8963
  • View blog
  • Posts: 33,624
  • Joined: 12-June 08

Re: How Would You Simulate PrintScreen In Code

Posted 03 May 2012 - 10:50 AM

Let's start with what are you expectations and actions you envision for a printscreen in c++ (presumably in a console program)?
Was This Post Helpful? 0
  • +
  • -

#3 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: How Would You Simulate PrintScreen In Code

Posted 03 May 2012 - 11:14 AM

View Postmodi123_1, on 03 May 2012 - 10:50 AM, said:

Let's start with what are you expectations and actions you envision for a printscreen in c++ (presumably in a console program)?


Well its a Win32 Gui, but It actually exacutes in the side-by-side cmd prompt. Also, does it help by me saying its in codeblocks?

Thanks,
L99
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 766
  • View blog
  • Posts: 2,229
  • Joined: 20-March 10

Re: How Would You Simulate PrintScreen In Code

Posted 04 May 2012 - 04:21 PM

Hi lukeme99,

No it doesn't matter which IDE you are using.

Do you mean you wish to use a console to run the program but copy and print a
regular Window ?

I am not sure what you mean but have a look at my tutorial on bitmap printing

BitMap Printing

then all you have to do is copy the window onto the clipboard then from the
clipboard print it out.


Best Wishes

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: How Would You Simulate PrintScreen In Code

Posted 05 May 2012 - 07:36 AM

View Postsnoopy11, on 04 May 2012 - 04:21 PM, said:

Hi lukeme99,

No it doesn't matter which IDE you are using.

Do you mean you wish to use a console to run the program but copy and print a
regular Window ?

I am not sure what you mean but have a look at my tutorial on bitmap printing

BitMap Printing

then all you have to do is copy the window onto the clipboard then from the
clipboard print it out.


Best Wishes

Snoopy.


The tutorial will be helpful, because I don't mean print, as in ink on paper, I mean a snapshot of the screen that I can paste into paint or word etc,
Thank you so much,
L99
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 766
  • View blog
  • Posts: 2,229
  • Joined: 20-March 10

Re: How Would You Simulate PrintScreen In Code

Posted 05 May 2012 - 10:28 AM

Ah cutting and pasting
sorry you weren't that clear in your post...

Try this small example then...

#include <windows.h>
char EditText[256];
int EditTextLength;
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Clipboard Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndButton;
    static HWND hwndEdit;
    HGLOBAL hGlobal;
    PSTR pGlobal;

    switch (message)                  /* handle the messages */
    {
    case WM_CREATE:
        hwndButton = CreateWindow ( TEXT ("button"),"Cut Text",WS_CHILD|WS_VISIBLE|1,55,100,100,25,hwnd,(HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
        hwndEdit = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| ES_LEFT|WS_BORDER| WS_VISIBLE,55, 50, 100, 25, hwnd,
                                 (HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
        return 0;

    case WM_COMMAND:

        switch (wParam)
        {
        case 1:
            EditTextLength = GetWindowTextLength(hwndEdit) +1;
            GetWindowText(hwndEdit, EditText, EditTextLength);
            hGlobal = GlobalAlloc(GHND|GMEM_SHARE,
                                  EditTextLength*sizeof (char));
            pGlobal = (char*)GlobalLock (hGlobal);
            strcpy (pGlobal, EditText);
            GlobalUnlock(hGlobal);

            OpenClipboard(hwnd);
            EmptyClipboard();
            SetClipboardData(CF_TEXT, hGlobal);
            CloseClipboard();
            break;
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}





I know its for text and not a picture but if you do some research
on how SetClipboardData works on msdn you will find its easy to modify to your situation.

Best Wishes Snoopy.
Was This Post Helpful? 0
  • +
  • -

#7 lukeme99  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 15-March 11

Re: How Would You Simulate PrintScreen In Code

Posted 09 May 2012 - 11:45 AM

View Postsnoopy11, on 05 May 2012 - 10:28 AM, said:

Ah cutting and pasting
sorry you weren't that clear in your post...

Try this small example then...

#include <windows.h>
char EditText[256];
int EditTextLength;
/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Clipboard Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hwndButton;
    static HWND hwndEdit;
    HGLOBAL hGlobal;
    PSTR pGlobal;

    switch (message)                  /* handle the messages */
    {
    case WM_CREATE:
        hwndButton = CreateWindow ( TEXT ("button"),"Cut Text",WS_CHILD|WS_VISIBLE|1,55,100,100,25,hwnd,(HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance,NULL);
        hwndEdit = CreateWindow (TEXT ("edit"),NULL,WS_CHILD| ES_LEFT|WS_BORDER| WS_VISIBLE,55, 50, 100, 25, hwnd,
                                 (HMENU) 1,((LPCREATESTRUCT) lParam) -> hInstance ,NULL);
        return 0;

    case WM_COMMAND:

        switch (wParam)
        {
        case 1:
            EditTextLength = GetWindowTextLength(hwndEdit) +1;
            GetWindowText(hwndEdit, EditText, EditTextLength);
            hGlobal = GlobalAlloc(GHND|GMEM_SHARE,
                                  EditTextLength*sizeof (char));
            pGlobal = (char*)GlobalLock (hGlobal);
            strcpy (pGlobal, EditText);
            GlobalUnlock(hGlobal);

            OpenClipboard(hwnd);
            EmptyClipboard();
            SetClipboardData(CF_TEXT, hGlobal);
            CloseClipboard();
            break;
        }
        return 0;

    case WM_DESTROY:
        PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
        break;
    default:                      /* for messages that we don't deal with */
        return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}





I know its for text and not a picture but if you do some research
on how SetClipboardData works on msdn you will find its easy to modify to your situation.

Best Wishes Snoopy.


Thanks allot!
L99
Was This Post Helpful? 0
  • +
  • -

#8 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 766
  • View blog
  • Posts: 2,229
  • Joined: 20-March 10

Re: How Would You Simulate PrintScreen In Code

Posted 10 May 2012 - 05:11 AM

Well don't thank me just yet,


There is a lot of work to be done in
Getting a Bitmap of the screen
onto the clipboard.

As you will no doubt find out.

Come back with code next time
give it your very best effort ok ?

Best Wishes.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1