| | |
Cool code snippets
![]() |
Hi, I was wondering if anyone knew any code snippets that do something cool like:
Open the CD tray:
Turn the monitor off and on:
Open the CD tray:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
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);
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
--Martin Golding
c++ Syntax (Toggle Plain Text)
/* * 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.
Good one , can we get some more of these code snippets ?
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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
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.
Enjoy!
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!
![]() |
Similar Threads
- Anyone want to learn Ruby (Ruby)
- DaniWeb link directory (Search Engine Optimization)
- Docstring fomatting (Python)
- Why are the Descriptions of Code Snippets in a Fixed Font? (DaniWeb Community Feedback)
- How to use good oo-structure? (C#)
- using php to check for filenames (PHP)
- 'directed acyclic word graph'?? (Java)
- What do you do with your knowledge? (Visual Basic 4 / 5 / 6)
Other Threads in the IT Professionals' Lounge Forum
- Previous Thread: Need Advice/Opinions! Re-entering the IT World!
- Next Thread: CAB and INF
| Thread Tools | Search this Thread |
1gbit advertising advice amazon answers archive british broadband business businessprocesses career carrier censorship cern china cio collectiveintelligence connectivity consumer consumers corporateearnings datatransfer debtcollectors dictionary digg digital ebay ecommerce email employment environment facebook food government grid high-definition hottub infodelivery infotech intel internet interview ipod isp japan kindle lhc library malware marketing mit moonfruit news onlineshopping piracy piratebay pope porn program questions r&d religion remoteworking research retail security sex shopping simple skype smallbusiness smb sms socialmedia socialnetworking software softwareengineer spam speed spending startrek statistics stocks study stumbleupon survey tabletpc technology touch-screen touchscreen twitter uk videoinprint voips web webdeveloper windows words






