943,922 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 17th, 2008
1

Cool code snippets

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

Open the CD tray:

C++ Syntax (Toggle Plain Text)
  1. mciSendString("open CDAudio", NULL, 0, NULL);
  2.  
  3. cout << "Opening CD-ROM door ..." << endl;
  4. mciSendString("set CDAudio door open", NULL, 0, NULL);
  5.  
  6. cout << "Closing the CD-ROM door in 5 seconds ..." << endl;
  7. Sleep(5000);
  8. mciSendString("set CDAudio door closed", NULL, 0, NULL);
  9.  
  10. mciSendString("close CDAudio", NULL, 0, NULL);

Turn the monitor off and on:

C++ Syntax (Toggle Plain Text)
  1. cout << "Turning off monitor...";
  2. SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2);
  3.  
  4. cout << endl << "Turning monitor back on...";
  5. SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);
Similar Threads
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Aug 17th, 2008
1

Re: Cool code snippets

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
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 17th, 2008
0

Re: Cool code snippets

I can't find it. What is the code?
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Aug 17th, 2008
0

Re: Cool code snippets

c++ Syntax (Toggle Plain Text)
  1. /*
  2.  * Author@WilliamHelmsworth
  3.  * Make sure project type is windows application
  4.  */
  5.  
  6. #define _WIN32_WINNT 0x0500
  7. #include<windows.h>
  8. #include<cmath>
  9.  
  10. LRESULT CALLBACK mouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
  11. // Get event information
  12. PMSLLHOOKSTRUCT p = (PMSLLHOOKSTRUCT) lParam;
  13.  
  14. bool eat = false;
  15.  
  16. // Screen resolution
  17. static float screen_cx = static_cast<float>( GetSystemMetrics(SM_CXSCREEN) );
  18. static float screen_cy = static_cast<float>( GetSystemMetrics(SM_CYSCREEN) );
  19.  
  20. // Centre of screen
  21. static float screen_centre_x = screen_cx / 2.0f;
  22. static float screen_centre_y = screen_cy / 2.0f;
  23.  
  24. // Calculate distance away from centre of screen
  25. float dx = p->pt.x - screen_centre_x;
  26. float dy = p->pt.y - screen_centre_y;
  27.  
  28. float dist = sqrt(dx * dx + dy * dy);
  29.  
  30. // Check if cursor is more than 300px away from centre of screen
  31. if (dist > 300) {
  32. float angle = atan2(dy, dx);
  33.  
  34. // Trap cursor
  35. SetCursorPos(
  36. /* X */ int( screen_centre_x + cos(angle) * 300 ),
  37. /* Y */ int( screen_centre_y + sin(angle) * 300 )
  38. );
  39.  
  40. // Stop windows handling event
  41. eat = true;
  42. }
  43.  
  44. return (eat ? 1 : CallNextHookEx(NULL, nCode, wParam, lParam));
  45. }
  46.  
  47. int WINAPI WinMain(HINSTANCE hInstance,
  48. HINSTANCE hPrevInstance,
  49. LPSTR lpCmdLine,
  50. int nShowCmd) {
  51.  
  52. // Set mouse hook
  53. HHOOK mouseHook = SetWindowsHookEx(
  54. WH_MOUSE_LL, /* Type of hook */
  55. mouseHookProc, /* Hook process */
  56. hInstance, /* Instance */
  57. static_cast<DWORD>(NULL));
  58.  
  59. // Wait for user to exit
  60. MessageBox(NULL, "Press OK to close.", "", MB_OK);
  61. return 0;
  62. }

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.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Aug 17th, 2008
0

Re: Cool code snippets

Cool =)
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Aug 17th, 2008
0

Re: Cool code snippets

Wow, I thought it just disabled the mouse completely, it makes it limited to a circle. COOL. I'm gonna prank lots of people ^^.
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Aug 18th, 2008
0

Re: Cool code snippets

Good one , can we get some more of these code snippets ?
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Aug 18th, 2008
0

Re: Cool code snippets

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

edit: Dont know why but this post has appeared before the main post
Last edited by William Hemsworth; Aug 18th, 2008 at 7:57 am.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 18th, 2008
0

Re: Cool 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

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!
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 18th, 2008
-2

Re: Cool code snippets

Your question has no sense.
Learn Win32 api and read MSDN.
There are just ... millions of code samples.
Reputation Points: -76
Solved Threads: 14
Junior Poster
marco93 is offline Offline
132 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in IT Professionals' Lounge Forum Timeline: Need Advice/Opinions! Re-entering the IT World!
Next Thread in IT Professionals' Lounge Forum Timeline: CAB and INF





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC