Thursday, September 10, 2009

Show/Hide Windows Desktop/Taskbar

Need to show/hide Windows Desktop/Taskbar?
procedure ShowTaskbar(T: Boolean);
var
  hTaskbar: HWND;
begin
  hTaskbar := FindWindow('Shell_TrayWnd', nil);
  if hTaskbar <> 0 then
    if T then
      ShowWindow(wndHandle, SW_SHOW or SW_RESTORE) else
      ShowWindow(wndHandle, SW_HIDE);
end;

Usage
 Show
  ShowTaskbar(True);
 Hide
  ShowTaskbar(False);
procedure ShowDesktop(T: Boolean);
var
  hDesktop: HWND;
begin
  hDesktop := FindWindow('progman', nil);
  if hDesktop <> 0 then
    if T then
      ShowWindow(hDesktop, SW_SHOW or SW_RESTORE) else
      ShowWindow(hDesktop, SW_HIDE);
end;

Usage
 Show
  ShowDesktop(True);
 Hide
  ShowDesktop(False);

2 comments:

  1. hi Dorin,

    thank you for sharing your code!

    Maybe it interests you that I had to use explicitly 'SW_SHOW' not 'SW_SHOW or SW_RESTORE' to restore both functions here on vista 64bit sp1.

    There are two minor bugs in the listings.

    1st (function ShowTaskbar):

    ShowWindow(hDesktop, SW_SW_SHOW or SW_RESTORE) else

    // should be

    ShowWindow(hDesktop, SW_SHOW or SW_RESTORE) else

    2nd (function ShowDesktop):

    ShowWindow(wndHandle, SW_SHOW or SW_RESTORE) else
    ShowWindow(wndHandle, SW_HIDE);

    // should be

    ShowWindow(hTaskbar, SW_SHOW or SW_RESTORE) else
    ShowWindow((hTaskbar, SW_HIDE);

    best regards, JPK

    ReplyDelete
  2. Thank you JPK I modified the post.
    I don't test every function before posting so if any bug found or the method posted is not working please don't hesitate to comment.

    ReplyDelete

Blogroll(General programming and Delphi feeds)