www.xordern.net

joke functions (programming)

I have searched for my old CDs with old programs to find joke functions that I used someday in an application.

Short notice: Jokes are only funny, if they happen in in a given context.
(e.g. "Please do NOT click here" or "Don't click on the button!") or if it happens all of a sudden (your cd door is opening - and then a message appears "Please put 20 $ in your cd drive to pay for the shareware and accept this by clicking on Ok")

Furthermore jokes should only be for a short period of time and quit without damage....

The following procedures can be used to design joke programes:

(The implementation in different programming languages is usally similar to each other, so mostly only one language is used in the examples.)

ControlMouse I

Let the mouse pointer jump...

Language: .Net / [C#]

Object: System.Windows.Forms.Control.Cursor

This value cannot only be read, it can also be set to a specific value.

Code:

int screen_width = Screen.PrimaryScreen.Bounds.Width;
int screen_height = Screen.PrimaryScreen.Bounds.Height;
Random rnd = new Random();
for (int z=0;z<20;z++)
{
	 Cursor.Position = new Point(rnd.Next(0, screen_width), rnd.Next(0, screen_height));

	 Application.DoEvents();
	 Thread.Sleep (550);
}

Control-IO

(Delphi) download example [200 kB]

BlockInput (block mouse/keyboard input)

The function BlockInput can be used to lock any mouse move or keyboard input.

DLL: user32.dll

Notice: To work under Windows Vista properly the program requires admin priviledges.
The blocker can be disabled by pressing CTRL+ALT+DEL.

Code:

[Delphi:]

procedure TForm1.Block ();
var DLLHandle: THandle;
FuncPtr: TFarProc;
BI: function(Block: BOOL): BOOL; stdcall;

begin
  DLLHandle := LoadLibrary('user32.dll');
  FuncPtr := GetProcAddress(DLLHandle, 'BlockInput');
	
  if FuncPtr <> nil then begin
    @BI :=GetProcAddress(DLLHandle, PChar('BlockInput')) ;
    BI(true); // block mouse and keyboard inputs
        
    Sleep (6501);
        
    BI(false); //recover the function of mouse + keyboard
  end;
  FreeLibrary(DLLHandle);
end;

power down screen (energy-saving)

A no longer existing screen signal can create the apprehension that something is happening what is not seen.

The screen can be powered off by activating energy-saving.

=> download example

Code:

[Delphi:]

for i := 0 to 16 do begin
  SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
  Sleep (400); // 6400 ms
end;
//(re-)activate energy-saving of monitor

SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
//deactivate energy-saving of monitor (power on screen)



ControlCDdoor

(Visual Basic) download example [10,1 kB]

control cd doors

Again and again the unexpected use of the cd drive is very surprising. It can be opened or closed by an application.

cd doorDLL: winmm.dll

Entry point: mciSendStringA

Command:

open: "set cdaudio door open"

close: "set cdaudio door closed"

Code:

[C#:]

[DllImport("winmm.dll", EntryPoint="mciSendStringA", CharSet=CharSet.Ansi)]
protected static extern int mciSendString
  (string mciCommand,
   StringBuilder returnValue,
   int returnLength,
   IntPtr callback);
   
static void open ()
{
    mciSendString ("set cdaudio door open", null, 0, IntPtr.Zero);
}
static void close()
{
    mciSendString ("set cdaudio door closed", null, 0, IntPtr.Zero);
}

[Basic]

Private Declare Function mcisendString Lib "winmm.DLL" Alias _
  "mciSendStringA" (ByVal lpstrCommand As String, ByVal _
  lpstrReturnString As String, ByVal wReturnLength As Integer, _
  ByVal hCallack As Integer) As Long
  
Sub open ()
 Call mcisendString("Set CDAudio Door Open", 0&, 0, 0)
 'Open Wait
End Sub
Sub close ()
 Call mcisendString("Set CDAudio Door Closed", 0&, 0, 0)
 'Closed Wait
End Sub

demo-WindowsForms-Net

(c#) download of the example "demo-WindowsForms-Net" [17,2 kB]

[opacity]

Language: .Net / [C#]

Property: System.Windows.Forms.Form.Opacity

The property Opacity defines the opacity level of a dotnet form (window) in percent.

Code:

[C#:]

//window is 100% visible
this.Opacity = 1;
	
//blend window up and down
for (int h=0;h<2;h++)
{
  for (int j=100;j>20;j--)
  {
	 this.Opacity -= 0.01;
	 Application.DoEvents();
	 Thread.Sleep (40); //every 40 ms 
  }
  
  for (int j=20;j<100;j++)
  {
	 this.Opacity += 0.01;
     Application.DoEvents();
	 Thread.Sleep (60); //every 60 ms 
  }
}
//set window to 100% visible
this.Opacity = 1;

transparent windows [TransparencyKey]

=> download example

TransparencyKey Opacity
With this property a windows form can be made transparent

Language: .Net / [C#]

Property: System.Windows.Forms.Form.TransparencyKey

TransparencyKey contains a color of a .net-form (window) which makes it transparent at the area where this color appears.

If FormBorderStyle is set to none , it is possible to create non square windows.

Code [C#]:

The TransparencyKey is an attribute of a window form:

this.TransparencyKey = Color.Pink;

At all areas where the form is pink it will be transparent and it can get holes.


ControlHandles

(c++) download "ControlHandles" [65,2 kB]

control (other) applications

Do you believe that any window in Windows can be controlled remotely? Or. do you think that a text of a program can be read from another application?

This example shows the handle with handles on the example of Notepad.

Language: C++

DLL: user32.dll

Used procedure: send events to the message loop of a Windows program

Helpful tools: Microsoft Spy++

Control Handles
other handles can be controled and be read out

objects / windows / handles:

In principle, each object (window, button ,...) under Windows represents a handle with which it can be addressed. This handle has a class name, Windows text and can receive or produce messages.

HWND <handlename> = NULL;

However, these objects can be searched, found and controlled afterwards by other programs.

Search for handles.

a) EnumWindows-procedure

//Search a window handle with a specified process id
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
{
HWND found_hwnd;

DWORD id = GetWindowThreadProcessId(hwnd, NULL);
if (id == (DWORD)param)
{
//found
found_hwnd=hwnd;
return false;
}
return true;
}

BOOL OtherProcedure ()
{
  [...]
  ::EnumWindows(&EnumWindowsProc, pi.dwThreadId);
  [...]
}

In this case found_hwnd would be the searched handle.

b) FindWindow

It is also possible to find handles with the function FindWindow.

It searches for a class name or window name (title/caption):

HWND hIam = FindWindow(_T("Notepad"),0); //search for a handle with the class name "Notepad"

HWND hIam = FindWindow(0,_T("Save as")); //search after a handle wit the window name (title) "Save as"

c) FindWindowEx

Handles that are subordinated to the parent handle are called child windows. They can be searched with the function FindWindowEx and a class or window name.

Search for the (first) child window of the handle hParent with the class name "Edit":

HWND hChild = ::FindWindowEx(hParent, NULL, _T("Edit"), NULL);

Search in a handle for the (first) handle with the caption "&Yes":

HWND hChild = ::FindWindowEx(<HANDLE>, NULL, NULL, "&Yes");

d) other methods

In addition handles can be found with commands like GetActiveWindow() or WindowFromPoint(POINT*) e.g. under the mouse cursor.

Send messages.

These handles can receive events (messages). To send a message you can use the command PostMessage or SendMessage.

To do this the command is called with e.g. the handle of a text box, the message type and a buffer with the content:

PostMessage(<TEXT-HANDLE>,WM_CHAR,chararray[position],0);

The post of a click message to a button [handle] looks like this:

PostMessage(<BUTTONHANDLE>,BM_CLICK,0,0);

readout.

However, you cannot only send something to a handle - you can also receive something.

For example, the content (windows name) of a text box-handle can be read out:

HWND programm = FindWindow(NULL,_T("window title")); 
HWND subhandle = ::FindWindowEx(programm, NULL, _T("RichViewEdit"), NULL);

char buffer[1024];

SendMessage(subhandle, WM_GETTEXT, (WPARAM)256,(LPARAM)buffer);

MessageBox (NULL, buffer, "Content of the text box:", NULL);

Notice.

This page should only demonstrate the handle with handles on a very easy level - of course this is no reference and doesn't replace any documentation.

For more information on these topics look at e.g. the MSDN-documentation. Helpful to identify handles and messages is the program "Microsoft Spy ++", which is included in Visual Studio.

With these basics it should be possible to understand the source code of the programm.

  © 2009-2014 Dominik Bok