::: 델파이 Tip&Trick :::

델파이 Tip&Trick 성격에 맞지 않는 광고,비방,질문의 글은 즉시 삭제하며
내용을 복사하여 사용할 경우 반드시 이곳(http://www.howto.pe.kr)을 출처로 명시하여 주세요


Category

  김영대(2004-08-11 21:13:51, Hit : 5066, Vote : 1225
 http://www.howto.pe.kr
 Task bar 에 나타나는 프로그램 아이콘 바꾸기

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    function StringToIcon (const st : string) : HIcon;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  ICONIMAGE = record
    Width, Height, Colors : DWORD;
    lpBits : PChar;
    dwNumBytes : DWORD;
    lpbi : PBitmapInfoHeader;
    lpXOR : PChar;
    lpAND : PChar;                
  end;

function CopyColorTable (var lpTarget : BITMAPINFO; const lpSource : BITMAPINFO) : boolean;
var
  dc : HDC;
  hPal : HPALETTE;
  pe : array [0..255] of PALETTEENTRY;
  i : Integer;
begin
  result := False;
  case (lpTarget.bmiHeader.biBitCount) of
    8 :
     if lpSource.bmiHeader.biBitCount = 8 then
     begin
       Move (lpSource.bmiColors, lpTarget.bmiColors, 256 * sizeof (RGBQUAD));
       result := True
     end
     else
     begin
       dc := GetDC (0);
       if dc <> 0 then
       try
         hPal := CreateHalftonePalette (dc);
         if hPal <> 0 then
         try
           if GetPaletteEntries (hPal, 0, 256, pe) <> 0 then
           begin
             for i := 0 to 255 do
             begin
               lpTarget.bmiColors [i].rgbRed := pe [i].peRed;
               lpTarget.bmiColors [i].rgbGreen := pe [i].peGreen;
               lpTarget.bmiColors [i].rgbBlue := pe [i].peBlue;
               lpTarget.bmiColors [i].rgbReserved := pe [i].peFlags
             end;
             result := True
           end
         finally
           DeleteObject (hPal)
         end
       finally
         ReleaseDC (0, dc)
       end
     end;

    4 :
     if lpSource.bmiHeader.biBitCount = 4 then
     begin
       Move (lpSource.bmiColors, lpTarget.bmiColors, 16 * sizeof (RGBQUAD));
       result := True
     end
     else
     begin
       hPal := GetStockObject (DEFAULT_PALETTE);
       if (hPal <> 0) and (GetPaletteEntries (hPal, 0, 16, pe) <> 0) then
       begin
         for i := 0 to 15 do
         begin
           lpTarget.bmiColors [i].rgbRed := pe [i].peRed;
           lpTarget.bmiColors [i].rgbGreen := pe [i].peGreen;
           lpTarget.bmiColors [i].rgbBlue := pe [i].peBlue;
           lpTarget.bmiColors [i].rgbReserved := pe [i].peFlags
         end;
         result := True
       end
     end;
    1:
     begin
       i := 0;
       lpTarget.bmiColors[i].rgbRed := 0;
       lpTarget.bmiColors[i].rgbGreen := 0;
       lpTarget.bmiColors[i].rgbBlue := 0;
       lpTarget.bmiColors[i].rgbReserved := 0;
       i := 1;
       lpTarget.bmiColors[i].rgbRed := 255;
       lpTarget.bmiColors[i].rgbGreen := 255;
       lpTarget.bmiColors[i].rgbBlue := 255;
       lpTarget.bmiColors[i].rgbReserved := 0;
       result := True
      end;
    else
     result := True
  end
end;

function WidthBytes (bits : DWORD) : DWORD;
begin
  result := ((bits + 31) shr 5) shl 2
end;

function BytesPerLine (const bmih : BITMAPINFOHEADER) : DWORD;
begin
  result := WidthBytes (bmih.biWidth * bmih.biPlanes * bmih.biBitCount)
end;

function DIBNumColors (const lpbi : BitmapInfoHeader) : word;
var
  dwClrUsed : DWORD;
begin
  dwClrUsed := lpbi.biClrUsed;
  if dwClrUsed <> 0 then
    result := Word (dwClrUsed)
  else
    case lpbi.biBitCount of
      1 : result := 2;
      4 : result := 16;
      8 : result := 256
      else
        result := 0
    end
end;

function PaletteSize (const lpbi : BitmapInfoHeader) : word;
begin
  result := DIBNumColors (lpbi) * sizeof (RGBQUAD)
end;

function FindDIBBits (const lpbi : BitmapInfo) : PChar;
begin
  result := @lpbi;
  result := result + lpbi.bmiHeader.biSize + PaletteSize (lpbi.bmiHeader)
end;

function ConvertDIBFormat (var lpSrcDIB : BITMAPINFO; nWidth, nHeight, nbpp : DWORD; bStretch : boolean) : PBitmapInfo;
var
  lpbmi : PBITMAPINFO;
  lpSourceBits, lpTargetBits : Pointer;
  DC, hSourceDC, hTargetDC : HDC;
  hSourceBitmap, hTargetBitmap, hOldTargetBitmap, hOldSourceBitmap : HBITMAP;
  dwSourceBitsSize, dwTargetBitsSize, dwTargetHeaderSize : DWORD;
begin
  result := Nil;
  dwTargetHeaderSize := sizeof ( BITMAPINFO ) + ( 256 * sizeof( RGBQUAD ) );
  GetMem (lpbmi, dwTargetHeaderSize);
  try
   lpbmi^.bmiHeader.biSize := sizeof (BITMAPINFOHEADER);
   lpbmi^.bmiHeader.biWidth := nWidth;
   lpbmi^.bmiHeader.biHeight := nHeight;
   lpbmi^.bmiHeader.biPlanes := 1;
   lpbmi^.bmiHeader.biBitCount := nbpp;
   lpbmi^.bmiHeader.biCompression := BI_RGB;
   lpbmi^.bmiHeader.biSizeImage := 0;
   lpbmi^.bmiHeader.biXPelsPerMeter := 0;
   lpbmi^.bmiHeader.biYPelsPerMeter := 0;
   lpbmi^.bmiHeader.biClrUsed := 0;
   lpbmi^.bmiHeader.biClrImportant := 0;     // Заполняем в таблице цветов
   if CopyColorTable (lpbmi^, lpSrcDIB) then
   begin
     DC := GetDC (0);
     hTargetBitmap := CreateDIBSection (DC, lpbmi^, DIB_RGB_COLORS, lpTargetBits, 0, 0 );
     hSourceBitmap := CreateDIBSection (DC, lpSrcDIB, DIB_RGB_COLORS, lpSourceBits, 0, 0 );

     try
       if (dc <> 0) and (hTargetBitmap <> 0) and (hSourceBitmap <> 0) then
       begin
         hSourceDC := CreateCompatibleDC (DC);
         hTargetDC := CreateCompatibleDC (DC);
         try
           if (hSourceDC <> 0) and (hTargetDC <> 0) then
           begin
             // Flip the bits on the source DIBSection to match the source DIB
             dwSourceBitsSize := DWORD (lpSrcDIB.bmiHeader.biHeight) * BytesPerLine(lpSrcDIB.bmiHeader);
             dwTargetBitsSize := DWORD (lpbmi^.bmiHeader.biHeight) * BytesPerLine(lpbmi^.bmiHeader);
             Move (FindDIBBits (lpSrcDIB)^, lpSourceBits^, dwSourceBitsSize );

             // Select DIBSections into DCs
             hOldSourceBitmap := SelectObject( hSourceDC, hSourceBitmap );
             hOldTargetBitmap := SelectObject( hTargetDC, hTargetBitmap );

             try
               if (hOldSourceBitmap <> 0) and (hOldTargetBitmap <> 0) then
               begin
                 if lpSrcDIB.bmiHeader.biBitCount <= 8 then
                     SetDIBColorTable (hSourceDC, 0, 1 shl lpSrcDIB.bmiHeader.biBitCount, lpSrcDIB.bmiColors);

                 if lpbmi^.bmiHeader.biBitCount <= 8  then
                     SetDIBColorTable (hTargetDC, 0, 1 shl lpbmi^.bmiHeader.biBitCount, lpbmi^.bmiColors );

                  // If we are asking for a straight copy, do it
                 if (lpSrcDIB.bmiHeader.biWidth = lpbmi^.bmiHeader.biWidth) and (lpSrcDIB.bmiHeader.biHeight = lpbmi^.bmiHeader.biHeight) then
                   BitBlt (hTargetDC, 0, 0, lpbmi^.bmiHeader.biWidth, lpbmi^.bmiHeader.biHeight, hSourceDC, 0, 0, SRCCOPY)
                 else
                   if bStretch then
                   begin
                     SetStretchBltMode (hTargetDC, COLORONCOLOR);
                     StretchBlt (hTargetDC, 0, 0, lpbmi^.bmiHeader.biWidth, lpbmi^.bmiHeader.biHeight,
                                 hSourceDC, 0, 0, lpSrcDIB.bmiHeader.biWidth, lpSrcDIB.bmiHeader.biHeight,
                                 SRCCOPY )
                   end
                   else
                     BitBlt (hTargetDC, 0, 0, lpbmi^.bmiHeader.biWidth, lpbmi^.bmiHeader.biHeight, hSourceDC, 0, 0, SRCCOPY );

                 GDIFlush;
                 GetMem (result, Integer (dwTargetHeaderSize + dwTargetBitsSize));

                 Move (lpbmi^, result^, dwTargetHeaderSize);
                 Move (lpTargetBits^, FindDIBBits (result^)^, dwTargetBitsSize)
               end
             finally
               if hOldSourceBitmap <> 0 then SelectObject (hSourceDC, hOldSourceBitmap);
               if hOldTargetBitmap <> 0 then SelectObject (hTargetDC, hOldTargetBitmap);
             end
           end
         finally
           if hSourceDC <> 0 then DeleteDC (hSourceDC);
           if hTargetDC <> 0 then DeleteDC (hTargetDC)
         end
       end;
     finally
       if hTargetBitmap <> 0 then DeleteObject (hTargetBitmap);
       if hSourceBitmap <> 0 then DeleteObject (hSourceBitmap);
       if dc <> 0 then ReleaseDC (0, dc)
     end
   end
finally
   FreeMem (lpbmi)
end
end;

function DIBToIconImage (var lpii : ICONIMAGE; var lpDIB : BitmapInfo; bStretch : boolean) : boolean;
var
  lpNewDIB : PBitmapInfo;
begin
  result := False;
  lpNewDIB := ConvertDIBFormat (lpDIB, lpii.Width, lpii.Height, lpii.Colors,
  bStretch );
  if Assigned (lpNewDIB) then
  try
   lpii.dwNumBytes := sizeof (BITMAPINFOHEADER)
                     + PaletteSize (lpNewDIB^.bmiHeader)
                     + lpii.Height * BytesPerLine (lpNewDIB^.bmiHeader)
                     + lpii.Height * WIDTHBYTES (lpii.Width);
   if lpii.lpBits <> Nil then
     FreeMem (lpii.lpBits);

   GetMem (lpii.lpBits,  lpii.dwNumBytes);
   Move (lpNewDib^, lpii.lpBits^, sizeof (BITMAPINFOHEADER) + PaletteSize(lpNewDIB^.bmiHeader));

   lpii.lpbi := PBITMAPINFOHEADER (lpii.lpBits);
   lpii.lpbi^.biHeight := lpii.lpbi^.biHeight * 2;

   lpii.lpXOR := FindDIBBits (PBitmapInfo (lpii.lpbi)^);
   Move (FindDIBBits (lpNewDIB^)^, lpii.lpXOR^, lpii.Height * BytesPerLine(lpNewDIB^.bmiHeader));

   lpii.lpAND := lpii.lpXOR + lpii.Height * BytesPerLine(lpNewDIB^.bmiHeader);
   Fillchar (lpii.lpAnd^, lpii.Height * WIDTHBYTES (lpii.Width), $00);

   result := True
  finally
   FreeMem (lpNewDIB)
  end
end;

function TForm1.StringToIcon (const st : string) : HIcon;
var
  memDC : HDC;
  bmp : HBITMAP;
  oldObj : HGDIOBJ;
  rect : TRect;
  size : TSize;
  infoHeaderSize : DWORD;
  imageSize : DWORD;
  infoHeader : PBitmapInfo;
  icon : IconImage;
  oldFont : HFONT;
begin
  result := 0;
  memDC := CreateCompatibleDC (0);
  if memDC <> 0 then
  try
   bmp := CreateCompatibleBitmap (Canvas.Handle, 16, 16);
   if bmp <> 0 then
   try
     oldObj := SelectObject (memDC, bmp);
     if oldObj <> 0 then
     try
       rect.Left := 0;
       rect.top := 0;
       rect.Right := 16;
       rect.Bottom := 16;
       SetTextColor (memDC, RGB (255, 0, 0));
       SetBkColor (memDC, RGB (128, 128, 128));
       oldFont := SelectObject (memDC, font.Handle);
       GetTextExtentPoint32 (memDC, PChar (st), Length (st), size);
       ExtTextOut (memDC, (rect.Right - size.cx) div 2, (rect.Bottom - size.cy) div 2, ETO_OPAQUE, @rect, PChar (st), Length (st), Nil);
       SelectObject (memDC, oldFont);
       GDIFlush;

       GetDibSizes (bmp, infoHeaderSize, imageSize);
       GetMem (infoHeader, infoHeaderSize + ImageSize);
       try
         GetDib (bmp, SystemPalette16, infoHeader^, PChar (DWORD (infoHeader) + infoHeaderSize)^);

         icon.Colors := 4;
         icon.Width := 32;
         icon.Height := 32;
         icon.lpBits := Nil;
         if DibToIconImage (icon, infoHeader^, True) then
         try
           result := CreateIconFromResource (PByte (icon.lpBits), icon.dwNumBytes, True, $00030000);
         Finally
           FreeMem (icon.lpBits)
         end
       finally
         FreeMem (infoHeader)
       end

     finally
       SelectObject (memDC, oldOBJ)
     end
   finally
     DeleteObject (bmp)
   end
  finally
   DeleteDC (memDC)
  end
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.Icon.Handle := StringToIcon(IntToStr(Timer1.Tag));
  Timer1.Enabled := True;
  Button1.Enabled := False;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i : Integer;
begin
  Timer1.Tag := Timer1.Tag + 1;
  if Timer1.Tag = 100 then
    Timer1.Tag := 1;

  Application.Icon.Handle := StringToIcon (IntToStr (Timer1.Tag));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Enabled := False;
end;

end.





411   [COM/OLE] 내 프로그램에서 선택한 text를 다른 프로그램에 Drag&Drop으로 주기  김영대 2004/12/10 5804 1225
410   [COM/OLE] TWebBrowser에서 JavaScript 호출하기  김영대 2004/10/09 5565 1225
  [일반/컴포넌트] Task bar 에 나타나는 프로그램 아이콘 바꾸기  김영대 2004/08/11 5066 1225
408   [데이터베이스] DB에서 자음(ㄱ,ㄴ,ㄷ....)으로 SELECT 하기  김영대 2003/03/12 5168 1225
407   [일반/컴포넌트] 한글이 포함된 문자열 자르기 - 반쪽 한글  김영대 2003/03/04 6355 1225
406   [윈도우즈 API] 실행중인 모든 프로그램 Minimized 시키기  김영대 2003/03/06 4606 1224
405   [윈도우즈 API] 원도우 이동시 다시 그리기 설정/해제  김영대 2003/03/07 4500 1223
404   [데이터베이스] 테이블,인덱스 복사하기  김영대 2003/03/07 4078 1223
403   [시스템] 윈도우즈 서비스 목록 구하기  김영대 2004/07/22 4521 1222
402   [네트웍/인터넷] Winsock WriteFile and Overlapped IO  김영대 2003/11/18 5384 1222
401   [시스템] 윈도우즈 전체의 키보드 입력 금지  김영대 2003/03/07 4558 1222
400   [멀티미디어] JPEG, WAVE 를 resource 파일에 넣고 읽어오기  김영대 2003/03/06 5137 1222
399   [윈도우즈 API] 정의되지 않은 Alt+키 입력시 Beep음 제거  김영대 2003/03/07 4336 1221
398   [윈도우즈 API] 작업관리자의 task switching 금지시키기  김영대 2003/03/07 4637 1221
397   [일반/컴포넌트] TForm을 BMP파일로 저장하기  김영대 2003/03/05 4809 1221
396   [일반/컴포넌트] 한 장당 두 페이지 인쇄하기  김영대 2003/03/05 4759 1221
395   [윈도우즈 API] 떠다니는 윈도우  김영대 2003/03/05 4540 1220
394   [일반/컴포넌트] LZExpand를 사용한 파일 복사하기  김영대 2003/03/04 4045 1220
393   [시스템] DOS-matrix 프린터에 인쇄하기  김영대 2003/03/07 4519 1219
392   [시스템] Locking/ Unlocking floppy driver or CD  김영대 2003/03/07 4267 1218
391   [시스템] 현재 내 PC의 비디오 정보  김영대 2003/03/04 4565 1218
390   [일반/컴포넌트] Image의 90도 회전  김영대 2003/03/04 5429 1218
389   [시스템] 윈도우즈 캡션의 높이 읽기/설정  김영대 2003/03/07 4436 1217
388   [알고리즘] 임의의 밑수(radix)를 사용한 진법변환  김영대 2004/12/08 4889 1216
387   [일반/컴포넌트] 문자열 프로시저명으로 실제 프로시저 호출하기2  김영대 2004/10/08 4767 1216
386   [윈도우즈 API] Active Desktop 활성화 여부  김영대 2004/08/25 4638 1216
385   [일반/컴포넌트] IE Toolbar 에 버튼 올리기  김영대 2003/03/26 4750 1215
384   [윈도우즈 API] 휴지통에 파일을 버리는 법  신훈재 2003/03/10 4578 1215
383   [윈도우즈 API] Window Handle 로 실행파일명 구하기  김영대 2003/03/07 4700 1215
382   [윈도우즈 API] Taskbar 감추기/보이기  김영대 2003/03/05 4655 1215
381   [일반/컴포넌트] 주어진 문자열에 잘못된 숫자값이 들어있는지 검사  김영대 2003/03/04 4182 1215
380   [일반/컴포넌트] ListView 의 item 을 강제로 편집상태로 만들기  김영대 2003/03/07 4604 1213
379   [일반/컴포넌트] ListBox의 내용을 파일로 저장하고 불러오기  김영대 2003/03/04 4333 1213
378   [시스템] 윈도우즈 부팅 모드(정상, 안전)  김영대 2004/08/05 4250 1212
377   [일반/컴포넌트] Windows의 사용자이름, 작업그룹 구하기  김영대 2003/04/01 4854 1212
376   [일반/컴포넌트] Form이 생성되어 있는지 검사하기  김영대 2003/03/05 4113 1212
375   [윈도우즈 API] 윈도우즈 "시작" 메뉴 표시하기/감추기  김영대 2003/03/07 4532 1211
374   [시스템] 기본 프린터가 있어야 실행하지 ?  김영대 2003/03/05 4568 1211
373   [일반/컴포넌트] TEdit의 OnChange에서 Text 바꾸기  김영대 2003/03/04 4171 1211
372   [일반/컴포넌트] StringGrid의 Column의 크기(width) 자동 조절  김영대 2003/03/04 6628 1211

[이전 10개] [1]..[11][12][13][14] 15 [16][17][18][19][20]..[25] [다음 10개]
 

Copyright 1999-2021 Zeroboard / skin by zero