Hi, I was wondering if anyone knew any code snippets that do something cool like:

Open the CD tray:

mciSendString("open CDAudio", NULL, 0, NULL);

cout << "Opening CD-ROM door ..." << endl;
mciSendString("set CDAudio door open", NULL, 0, NULL);

cout << "Closing the CD-ROM door in 5 seconds ..." << endl;
Sleep(5000);
mciSendString("set CDAudio door closed", NULL, 0, NULL);

mciSendString("close CDAudio", NULL, 0, NULL);

Turn the monitor off and on:

cout << "Turning off monitor...";
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);

cout << endl << "Turning monitor back on...";
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
William Hemsworth commented: Nice :) +3

Recommended Answers

All 12 Replies

I like williamhelmswort's mouse-hook snippet. It always makes random amusement when I use it on somebody elses computer and watch them struggle to move the mouse XD

commented: Thanks !! :) +3

I can't find it. What is the code?

/*
 * Author@WilliamHelmsworth
 *  Make sure project type is windows application
 */

#define _WIN32_WINNT 0x0500
#include<windows.h>
#include<cmath>

LRESULT CALLBACK mouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
   // Get event information
   PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT) lParam;

   bool eat = false;

   // Screen resolution
   static float screen_cx = static_cast<float>( GetSystemMetrics(SM_CXSCREEN) );
   static float screen_cy = static_cast<float>( GetSystemMetrics(SM_CYSCREEN) );

   // Centre of screen
   static float screen_centre_x = screen_cx / 2.0f;
   static float screen_centre_y = screen_cy / 2.0f;

   // Calculate distance away from centre of screen
   float dx = p->pt.x - screen_centre_x;
   float dy = p->pt.y - screen_centre_y;

   float dist = sqrt(dx * dx + dy * dy);

   // Check if cursor is more than 300px away from centre of screen
   if (dist > 300) {
      float angle = atan2(dy, dx);

      // Trap cursor
      SetCursorPos(
         /* X */ int( screen_centre_x + cos(angle) * 300 ),
         /* Y */ int( screen_centre_y + sin(angle) * 300 )
      );

      // Stop windows handling event
      eat = true;
   }

   return (eat ? 1 : CallNextHookEx(NULL, nCode, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd) {

   // Set mouse hook
   HHOOK mouseHook = SetWindowsHookEx(
                  WH_MOUSE_LL,      /* Type of hook */
                  mouseHookProc,    /* Hook process */
                  hInstance,        /* Instance */
                  static_cast<DWORD>(NULL));

   // Wait for user to exit
   MessageBox(NULL, "Press OK to close.", "", MB_OK);
   return 0;
}

I did make one minor change and that is the static cast from NULL to DWORD to satisfy the compiler.

I ran this using Dev-CPP default compiler (most likely mingw), though I'm not sure of what version of the compiler.

Cool =)

Wow, I thought it just disabled the mouse completely, it makes it limited to a circle. COOL. I'm gonna prank lots of people ^^.

Good one , can we get some more of these code snippets ?

I wrote a slightly evil code, which I decided not to put up as a code snippet because it completely stops you from doing ANYTHING with the mouse or keyboard :) The only way to get out of it is to either log off (may only work on some computers) . or to shut down the computer. Be prepared before trying it out :D

The only thing you can't disable like this is the ability for the user to press CTRL-ALT-DEL

/*
 *  Make sure project type is windows application
 */

#define _WIN32_WINNT 0x0500
#include<windows.h>
#include<cmath>

LRESULT CALLBACK mouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
   return 1;
}

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
   return 1;
}

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd) {

   // Set mouse hook
   HHOOK mouseHook = SetWindowsHookEx(
                  WH_MOUSE_LL,      /* Type of hook */
                  mouseHookProc,    /* Hook process */
                  hInstance,        /* Instance */
                  NULL);

   HHOOK keyboardHook = SetWindowsHookEx(
                  WH_KEYBOARD_LL,
                  keyboardHookProc,
                  hInstance,
                  0);

   // Wait for user to exit
   MessageBox(NULL, "Press OK to close.", "", MB_OK);
   return 0;
}

But just incase you want to be able to get out of it, you can make it so the user can exit the process be pressing the ESCAPE button, just change the keyboard hook like this.

LRESULT CALLBACK keyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
   PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT) (lParam);
   if (p->vkCode == VK_ESCAPE) {
      exit(0);
   }
   return 1;
}

Enjoy! :)

And I suppose you could be VERY evil and make it so the application automatically runs on startup :D

edit: Dont know why but this post has appeared before the main post :P

Your question has no sense.
Learn Win32 api and read MSDN.
There are just ... millions of code samples.

commented: Huhh ?? I dont think you understood his question. +0

The only thing you can't disable like this is the ability for the user to press CTRL-ALT-DEL

Disabling Ctrl+Alt+Del is a FAQ on Adv. Win32 api ng for decades
(news://194.177.96.26/comp.os.ms-windows.programmer.win32)
And Disabling input like this is horrible. The official method is BlockInput()
Read the Petzold + Richter to learn Win32 api...

commented: At least provide a code example of how something can be flawed or wrong instead of taking the easy way out and saying "read said book." +0

Pffftt, its horrible ?? I honestly have no idea where you get this information from, it works doesn't it ??

marco93, I'm reading Programming Windows by Charles Petzold, and I know about MSDN, it doesn't show every cool thing that can be done with C++. If it did, I would be completely amazed. Also, HOW IS THE CODE HORRIBLE?. BlockInput can't do everything. Compile the first code for a mouse disabler. It makes it so you can only move it in a circle. Can you do that with BlockInput?

commented: nicely said :) +3
commented: you posed a good question there ;) +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.