Cool code snippets

Reply

Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Cool code snippets

 
1
  #1
Aug 17th, 2008
Hi, I was wondering if anyone knew any code snippets that do something cool like:

Open the CD tray:

  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:

  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);
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Cool code snippets

 
1
  #2
Aug 17th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Cool code snippets

 
0
  #3
Aug 17th, 2008
I can't find it. What is the code?
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: Cool code snippets

 
0
  #4
Aug 17th, 2008
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Cool code snippets

 
0
  #5
Aug 17th, 2008
Cool =)
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Cool code snippets

 
0
  #6
Aug 17th, 2008
Wow, I thought it just disabled the mouse completely, it makes it limited to a circle. COOL. I'm gonna prank lots of people ^^.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,827
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 118
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Cool code snippets

 
0
  #7
Aug 18th, 2008
Good one , can we get some more of these code snippets ?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,421
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Cool code snippets

 
0
  #8
Aug 18th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,421
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

Re: Cool code snippets

 
0
  #9
Aug 18th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 118
Reputation: marco93 is infamous around these parts marco93 is infamous around these parts marco93 is infamous around these parts 
Solved Threads: 12
marco93 marco93 is offline Offline
Junior Poster

Re: Cool code snippets

 
-2
  #10
Aug 18th, 2008
Your question has no sense.
Learn Win32 api and read MSDN.
There are just ... millions of code samples.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the IT Professionals' Lounge Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC