Autor Beitrag
Rool
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 211



BeitragVerfasst: Sa 01.02.03 18:04 
Ich habe das Problem, das ich über Api ein Menu mit einem Bitmap davor einfügen möchte. Mit Text klappt alles bestens, aber ich weis nicht wie ich das mit dem Bitmap handhaben soll. Bisher hab ich folgendes:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
bmp:=TBitmap.Create;
bmp.LoadFromFile('BMP.bmp');

InsertMenu(Menu, indexMenu, MF_BYPOSITION or MF_POPUP or MF_BITMAP, submenu, PCHAR(bmp.Handle));

bmp.Free;


Dazu muss ich noch sagen, dass Menu das Menu ist, in das der Eintrag eingefügt wird, submenu ein Untermenu ist und indexMenu die Position, an der der Eintrag engefügt werden soll!

Könnt ihr mir sagen was ich falsch mache?

_________________
MFG Rool
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 01.02.03 18:57 
nonVCL Programm mit Bitmaps im Menü:
ausblenden volle Höhe Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
{******************************************************************************}
{                                                                              }
{                           Demo Bitmap-Menü                                   }
{                                                                              }
{                    Copyright (c) 2001 Michael Puff                           }
{                           www.luckie-online.de                               }
{                          mpuff@luckie-online.de                              }
{                                                                              }
{******************************************************************************}
program bitmapmenu;

{$R resource.res}

uses
  Windows,
  Messages;

const
  ClassName = 'WndClass';
  AppName = 'Bitmap-Menü-Demo';
  WindowWidth = 500;
  WindowHeight = 350;

  IDM_KUCKUCK = 1;
  IDM_LERCHE = 2;
  IDM_SPATZ = 3;

var
  hMenu, hPopupMenu: Cardinal;

{ GetLastError }
function DisplayErrorMsg(hWnd: THandle): DWORD;
var
  szBuffer: array[0..255] of Char;
begin
  FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, GetLastError, 0, szBuffer,
    sizeof(szBuffer), nil);
  MessageBox(hWnd, szBuffer, 'Fehler', MB_ICONSTOP);
  result := GetLastError;
end;

procedure MenuBmp(idBmpStart: DWORD; dc: HDC; cntItems, ItemHeight: Byte; hWnd: Cardinal);
var
  i: Integer;
  hBmp: HBITMAP;
  bmp: Windows.BITMAP;
  hdcBmp: HDC;
begin
  for i := 0 to cntItems-1 do
  begin
    hBmp := LoadBitmap(hInstance, MAKEINTRESOURCE(idBmpStart+i));
    GetObject(hBmp, sizeof(BITMAP), @bmp);
    hdcBmp := CreateCompatibleDC(dc);
    SelectObject(hdcBmp, hBmp);
    StretchBlt(dc, 2, ItemHeight*i+2, 15, 15, hdcBmp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
  end;
  ReleaseDC(hWnd, hdcBmp);
  DeleteObject(hBmp);
end;

function WndProc(hWnd: HWND; uMsg: UINT; wParam: wParam; lParam: LParam):
  lresult; stdcall;
var
  mi : PMEASUREITEMSTRUCT;
  di : PDRAWITEMSTRUCT;
begin
  Result := 0;
  case uMsg of
    WM_CREATE:
    begin
      hMenu := CreateMenu;
      hPopupMenu := CreateMenu;
      AppendMenu(hPopupMenu, MF_OWNERDRAW, IDM_KUCKUCK, '&Kuckuck');
      AppendMenu(hPopupMenu, MF_OWNERDRAW, IDM_LERCHE, '&Lerche');
      AppendMenu(hPopupMenu, MF_OWNERDRAW, IDM_SPATZ, '&Spatz');
      AppendMenu(hMenu, MF_POPUP, hPopupMenu, '&Datei');
      SetMenu(hWnd, hMenu);
    end;
    WM_MEASUREITEM:
    begin
      mi := PMEASUREITEMSTRUCT(lParam);
      case mi.CtlType of
        ODT_MENU:
        begin
          mi.itemWidth := 100;
          mi.itemHeight := 19;
        end
      end;
    end;
    WM_DRAWITEM:
    begin
      di := PDRAWITEMSTRUCT(lParam);
      case di.CtlType of
        ODT_MENU:
        begin
          if (di.itemState and ODS_SELECTED = ODS_SELECTED)  then
          begin
            FillRect(di.hDC, di.rcItem, GetSysColorBrush(COLOR_HIGHLIGHT));
            SetTextColor(di.hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
          end
          else
          begin
            FillRect(di.hDC, di.rcItem, GetSysColorBrush(COLOR_BTNFACE));
            SetTextColor(di.hDC, GetSysColor(COLOR_WINDOWTEXT));
          end;
          SetBkMode(di.hDC, TRANSPARENT);
          di.rcItem.Left := di.rcItem.left + 30;
          DrawText(di.hDC, PChar(di.itemData), lstrlen(PChar(di.itemData)), di.rcItem,
            DT_SINGLELINE or DT_VCENTER);
          MenuBmp(101, di.hDC, 3, 19, hWnd);
        end;
      end;
    end;
    WM_COMMAND:
    begin
      if HiWord(wParam) = 0 then
        case LoWord(wParam) of
          IDM_KUCKUCK: SendMessage(hWnd, WM_CLOSE, 0,0 );
        end;
    end;
    WM_DESTROY: PostQuitMessage(0);
  else
    Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
  end;
end;

var
  wc: TWndClassEx = (
    cbSize          : SizeOf(TWndClassEx);
    Style           : CS_HREDRAW or CS_VREDRAW;
    lpfnWndProc     : @WndProc;
    cbClsExtra      : 0;
    cbWndExtra      : 0;
    hbrBackground   : COLOR_APPWORKSPACE;
    lpszMenuName    : nil;
    lpszClassName   : ClassName;
    hIconSm         : 0;
  );
  msg: TMsg;
begin
  wc.hInstance  := hInstance;
  wc.hIcon      := LoadIcon(0, IDI_APPLICATION);
  wc.hCursor    := LoadCursor(0, IDC_ARROW);

  RegisterClassEx(wc);
  CreateWindowEx(0, ClassName, AppName, WS_CAPTION or WS_VISIBLE or
    WS_SYSMENU, Integer(CW_USEDEFAULT),Integer(CW_USEDEFAULT), WindowWidth,
    WindowHeight, 0, 0, hInstance, nil);

  while true do
  begin
    if not GetMessage(msg, 0, 0, 0) then
      break;
    begin
      TranslateMessage(msg);
      DispatchMessage(msg);
    end;
  end;
  ExitCode := msg.wParam;
end.

Die Bitmpas liegen fortlaufend Reihenfolge in der Ressource: 101, 102, 103. Duie Bitmaps müssen in der Reihenfolge in der Ressource liegen, wie sie nachher im Menü erscheinen sollen.
Ist noch keine optimale Lösung aber so geht es zu mindest erst mal.
Rool Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 211



BeitragVerfasst: So 02.02.03 01:39 
Titel: Hmmmm...
Ok, so gehts, aber die alte Methode von mir tuts inzwischen auch. Musste das Bitmap nur im initialization Abschnitt Erzeugen und erst bei Dll-Entladung zerstören! Verstehe zwar nicht warum das so ist aber jetzt funktioneierts!
Trotzdem Danke für deine Mühe!!![/code]

_________________
MFG Rool