home *** CD-ROM | disk | FTP | other *** search
- program ColorBox;
-
- {$G+} {$S-}
-
- {$M 10240,1024}
-
- {$R ColorBox}
-
- uses WinTypes,WinProcs,win31;
-
- CONST
- AppName='ColorBox';
- eraser:hbrush=0;
- Wi:HWnd=0;
- tc:array[0..15] of tcolorref=
- ($000000,$800000,$008000,$808000,
- $000080,$800080,$008080,$808080,
- $C0C0C0,$FF0000,$00FF00,$FFFF00,
- $0000FF,$FF00FF,$00FFFF,$FFFFFF);
- tcn:array[0..15] of string[7]=
- ('Black','Navy','Green','Teal',
- 'Maroon','Purple','Olive','Gray',
- 'Silver','Blue','Lime','Aqua',
- 'Red','Fuscia','Yellow','White');
- color:array[1..2] of byte=(15,0);
-
- first:boolean=false;
- w:hwnd=0;
- itm:byte=65;
-
- VAR
- hinst1:THandle;
- hicon1:hicon;
- mwid,mht:integer;
-
- hkk:hhook;
- hkproc:thookproc;
- proc1,proc2,aproc:tfarproc;
-
- FUNCTION num(xx:longint):string;
- var aa:string[30];
- BEGIN
- str(xx,aa);
- num:=aa;
- END;
-
- function p(s:string):pchar;assembler;
- asm
- push ds
- cld
- lds si,s
- lodsb
- xor ah,ah
- les di,s
- inc di
- add di,ax
- xor al,al
- stosb
- mov ax,si
- mov dx,ds
- pop ds
- end;
-
- function pas(s:string):string;assembler;
- asm
- push ds
- cld
- les di,s
- inc di
- mov dx,di
- mov cx,255
- xor al,al
- repne scasb
- dec di
- sub di,dx
- mov ax,di
- les di,s
- stosb
- lds si,s
- les di,@result
- movsb
- mov cl,al
- xor ch,ch
- inc cx
- rep movsb
- pop ds
- end;
-
- procedure WriteColors;
- var
- s:string;
- begin
- s:=num(color[1]);
- WriteProfileString(AppName,'Background',p(s));
- s:=num(color[2]);
- WriteProfileString(AppName,'Text',p(s));
- end;
-
- procedure makenewcolors;
- begin
- if eraser<>0 then deleteobject(eraser);
- eraser:=createsolidbrush(tc[color[1]]);
- end;
-
- procedure centerw(w:hwnd);
- var r:TRect;
- begin
- getwindowrect(w,r);
- with r do
- begin
- right:=right-left;
- bottom:=bottom-top;
- movewindow(w,(mwid-right) div 2,(mht-bottom) div 2,right,bottom,false);
- end;
- end;
-
- function ans2(hDlg:hWnd;message,
- wParam:Word;lParam:LongInt):Longint;Export;
- var i:integer;
- begin
- case message of
- WM_CTLCOLOR:
- begin
- SetBkMode(HDC(wParam),TRANSPARENT);
- SetBkColor(HDC(wParam),tc[color[1]]);
- SetTextColor(HDC(wParam),tc[color[2]]);
- ans2:=eraser;
- exit;
- end;
- wm_initdialog:centerw(w);
- end;
- ans2:=CallWindowProc(proc2,hdlg,message,wParam,lParam);
- end;
-
- procedure unhook;
- begin
- if w=0 then exit;
- SetWindowLong(w,GWL_WNDPROC,longint(proc2));
- w:=0;
- end;
-
- function CBTProc(Code:Integer;wParam:Word;
- lParam:LongInt):longint;Export;
- var
- lpcbtcreate:PCBT_CREATEWND;
- begin
- case code of
- HCBT_CREATEWND:
- begin
- lpcbtcreate:=PCBT_CREATEWND(lParam);
- if lpcbtcreate^.lpcs^.lpszClass=wc_dialog then
- begin
- w:=HWND(wParam);
- first:=TRUE;
- end else if first then
- begin
- proc2:=tfarproc(SetWindowLong(w,GWL_WNDPROC,longint(proc1)));
- first:=false;
- end;
- end;
- HCBT_DESTROYWND:if HWND(wParam)=w then unhook;
- end;
- CBTProc:=CallNextHookEx(hkk,Code,wParam,lParam);
- end;
-
- procedure paintboxes;
- var
- w1,w2:hwnd;
- s:string;
- begin
- w1:=findwindow(wc_dialog,nil);
- if w1<>0 then
- repeat
- getclassname(w1,@s[1],250);
- if pas(s)='#32770' then invalidaterect(w1,nil,true);
- w2:=w1;
- w1:=getnextwindow(w2,gw_HWndNext);
- until w1=0;
- end;
-
- procedure ClrOut(i:integer;ss:string);
- begin
- ss:=' '+ss+': '+num(color[i])+' ('+tcn[color[i]]+') ';
- setdlgitemtext(wi,99+i,p(ss));
- end;
-
- function WinProc(Wi:HWnd;Message,WParam:Word;
- LParam:Longint):Longint;export;
- var
- i:integer;
- r:TRect;
- w2:hwnd;
-
- begin
- WinProc:=0;
- case Message of
- wm_Command:
- case wParam of
- 8:showwindow(wi,sw_hide);
- 9:
- begin
- messagebox(wi,
- 'This program to color dialog boxes is '+
- 'freeware by Harry Gensler '+
- '(Philosophy Department, Loyola University, '+
- '6525 North Sheridan, Chicago, IL 60626).'#13#10#13#10+
- 'Put in your StartUp group to run automatically; use '+
- '"HIDE" command-line parameter to start hidden.',
- 'About Color Dialog Boxes',mb_ok);
- setfocus(GetDlgItem(wi,itm));
- end;
- 2:
- begin
- EndDialog(wi,0);
- Exit;
- end;
- 65..68:
- begin
- itm:=wParam;
- setfocus(GetDlgItem(wi,itm));
- case wparam of
- 65:
- begin
- if color[1]=0 then color[1]:=15 else dec(color[1]);
- makenewcolors;
- end;
- 66:
- begin
- if color[1]=15 then color[1]:=0 else inc(color[1]);
- makenewcolors;
- end;
- 67:if color[2]=0 then color[2]:=15 else dec(color[2]);
- 68:if color[2]=15 then color[2]:=0 else inc(color[2]);
- end;
- sendmessage(wi,wm_setredraw,0,0);
- ClrOut(1,'BACKGROUND');
- ClrOut(2,'TEXT');
- getclientrect(getdlgitem(Wi,9),r);
- i:=r.right-r.left;
- getclientrect(Wi,r);
- with r do
- begin
- right:=right-i;
- left:=left+i;
- end;
- sendmessage(wi,wm_setredraw,1,0);
- invalidaterect(wi,@r,true);
- updatewindow(wi);
- paintboxes;
- end;
- end;
- wm_setfocus:
- begin
- showwindow(wi,sw_show);
- setfocus(GetDlgItem(wi,itm));
- end;
- wm_ctlcolor:
- begin
- SetBkColor(wParam,tc[color[1]]);
- SetTextColor(wParam,tc[color[2]]);
- winproc:=eraser;
- exit;
- end;
- wm_EraseBkGnd:
- begin
- getclientrect(wi,r);
- fillrect(wParam,r,eraser);
- winproc:=eraser;
- exit;
- end;
- wm_Destroy:
- begin
- if eraser<>0 then deleteobject(eraser);
- writecolors;
- DestroyIcon(hicon1);
- if w<>0 then
- begin
- w2:=w;
- unhook;
- invalidaterect(w2,nil,true);
- end;
- UnhookWindowsHookEx(hkk);
- FreeProcInstance(AProc);
- FreeProcInstance(proc1);
- PostQuitMessage(0);
- exit;
- end;
- wm_QueryEndSession:writecolors;
- else;
- end;
- WinProc:=DefWindowProc(Wi,Message,WParam,LParam);
- end;
-
- procedure WinInit;
- var
- dc:hdc;
- s:string;
- i:byte;
- const
- WClass:TWndClass=(
- Style:0;
- lpfnWndProc:@WinProc;
- cbClsExtra:0;
- cbWndExtra:DlgWindowExtra;
- hInstance:0;
- hIcon:0;
- hCursor:0;
- hbrBackground:0;
- lpszMenuName:nil;
- lpszClassName:AppName);
- begin
- wi:=findwindow(AppName,nil);
- if wi<>0 then
- begin
- showwindow(wi,sw_shownormal);
- setfocus(wi);
- exit;
- end;
- dc:=getdc(getdesktopwindow);
- for i:=0 to 15 do tc[i]:=getnearestcolor(dc,tc[i]);
- releasedc(getdesktopwindow,dc);
- eraser:=getstockobject(gray_brush);
- WClass.hInstance:=HInstance;
- WClass.hIcon:=LoadIcon(hinstance,'W');
- WClass.hCursor:=LoadCursor(0,idc_Arrow);
- WClass.hbrBackground:=eraser;
- if not RegisterClass(WClass) then halt(255);
- hinst1:=WClass.hInstance;
- hicon1:=WClass.hIcon;
- mwid:=getsystemmetrics(sm_cxscreen);
- mht:=getsystemmetrics(sm_cyscreen);
- i:=GetProfileInt(AppName,'Background',15);
- if (i>=0) and (i<=15) then color[1]:=i;
- i:=GetProfileInt(AppName,'Text',0);
- if (i>=0) and (i<=15) then color[2]:=i;
- makenewcolors;
- Wi:=CreateDialog(hinstance,'W',0,nil);
- centerw(wi);
- ClrOut(1,'BACKGROUND');
- ClrOut(2,'TEXT');
- if CmdShow=6 then CmdShow:=sw_hide;
- aproc:=MakeProcInstance(@CBTProc,HInstance);
- hkproc:=THookProc(aproc);
- hkk:=SetWindowsHookEx(WH_CBT,hkproc,hinstance,0);
- proc1:=MakeProcInstance(@ans2,hinstance);
- s:='x';
- if paramcount>0 then s:=paramstr(1);
- if upcase(s[1])='H' then showwindow(wi,sw_hide)
- else ShowWindow(Wi,CmdShow);
- end;
-
- var
- M:TMsg;
- begin
- WinInit;
- while GetMessage(M,0,0,0) do if not isdialogmessage(wi,M) then
- begin
- TranslateMessage(M);
- DispatchMessage(M);
- end;
- UnregisterClass(AppName,hinst1);
- Halt(m.wParam);
- end.