Se7en
Se7en

Reputation: 33

Loading Bitmaps onto WinAPI Window(NO FORM!) From .res Files

i have a Win32 API Application in Delphi 2007 with no form and would like to load a bitmap from a .res file. Been looking around for two day's and just can't seem to find anything on this subject so was time to post. :)

Assuming i need to add code to "WM_PAINT" message just not sure what to add. maybe could use GDI.

/Thanks.

EDIT:

function WndProc(hWin: HWnd; Msg, WParam, LParam: Longint): Longint; stdcall;
var
  hbmp: HBITMAP;
  ps: PAINTSTRUCT;
  DC, hdcMem: HDC;
  bmp: BITMAP;
  oldBitmap: HGDIOBJ;
begin
  case Msg of
    WM_CREATE:
    begin
      hbmp := LoadImage(HInstance, 'C:\test_img.bmp', IMAGE_BITMAP, 0, 0,    LR_LOADFROMFILE); // Never called H2077 Value assigned to 'hbmp' never used
      ShowMessage('Im Here'); // Called
      Result := 0;
      Exit;
    end;
    WM_PAINT:
    begin
      hbmp := LoadImage(HInstance, 'C:\test_img.bmp', IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); // Called
      DC := BeginPaint(hWin, ps);
      hdcMem := CreateCompatibleDC(DC);
      oldBitmap := SelectObject(hdcMem, hbmp);
      GetObject(hbmp, SizeOf(bmp), @bmp);
      BitBlt(DC, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMem, 0, 0, SRCCOPY);
      SelectObject(hdcMem, oldBitmap);
      DeleteDC(hdcMem);
      DeleteObject(hbmp);
      EndPaint(hWin, ps);
      Result := 0;
      Exit;
    end;
    WM_DESTROY:
    begin
      PostQuitMessage(0);
      Result := 0;
      Exit;
    end;
  end;
  Result := DefWindowProc(hWin, Msg, WParam, LParam);
end;

Am i right in thinking that WM_CREATE is the equivalent of Form1.OnCreate and WM_DESTROY is Form1.OnDestroy ect..

Upvotes: 0

Views: 1037

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 597941

Look at the TBitmap class in the Graphics unit. It has LoadFromResourceName() and LoadFromResourceID() methods.

Upvotes: 1

Related Questions