Recent

Author Topic: Compiling with resources.????  (Read 932 times)

mav

  • Jr. Member
  • **
  • Posts: 79
Compiling with resources.????
« on: July 08, 2020, 06:21:22 pm »
Hello:
  What am I doing wrong here?
Compile without errors, the window works, but there is no menu.
 The resource: source08_resource.rc
 
Code: Pascal  [Select][+][-]
  1. 1 ICON eric.ico // icon
  2.  
  3. 10 MENU // main window menu
  4.  BEGIN
  5.  POPUP "&File"
  6.   BEGIN
  7.    MENUITEM "Message 1", 11
  8.    MENUITEM "Message 2", 12
  9.     MENUITEM SEPARATOR
  10.    MENUITEM "&Exit", 19
  11.   END
  12.  
  13.  POPUP "&About"
  14.   BEGIN
  15.    MENUITEM "About", 21
  16.   END
  17.  END
           The Code :
                           
Code: Pascal  [Select][+][-]
  1. {$APPTYPE GUI}
  2. {$MODE DELPHI}
  3.  
  4. program Win32_Source08;
  5.    uses  windows;
  6.  
  7. {$r source08_resource.rc}
  8.  
  9. const
  10.   AppName = 'Win32/ FPC - Source 08';
  11.   MenuName = '10';
  12.  
  13. var
  14.   active   : BOOLEAN;
  15.   Amsg     : MSG;
  16.   hWindow  : hwnd;
  17.   dcWindow : hdc;
  18.   ps       : paintstruct;
  19.   r        : rect;
  20. // Try, Throw, Catch mechanism. Simple proc to display given errors. //
  21. procedure ThrowError(pcErrorMessage : pChar);
  22. begin
  23.   MessageBox(0, pcErrorMessage, 'Error', MB_OK);
  24.   Halt(0);
  25. end;
  26. // WinProc to handle Windows messages. //
  27. function WindowProc(hEventWindow : hwnd; Umsg : DWORD; wParam : WPARAM;
  28.                     lParam : LPARAM) : LRESULT;stdcall;
  29. begin
  30.   Result := 0;
  31.   case (Umsg) of
  32.     WM_ACTIVATE : begin
  33.                   active := true;
  34.                   end;
  35.  
  36.     WM_DESTROY : begin
  37.                   active := false;
  38.                   PostQuitMessage(0);
  39.                   Exit;
  40.                 end;
  41.  
  42.     WM_KEYDOWN : begin
  43.                   active := false;
  44.                 end;
  45.  
  46.     WM_PAINT : begin
  47.                   dcWindow := BeginPaint(hWindow ,@ps);
  48.                   GetClientRect(hWindow, @r);
  49.                   SetBKMode(dcWindow, TRANSPARENT);
  50.                   SetTextColor(dcWindow, RGB(0,0,0));
  51.  
  52.                   DrawText(dcWindow, 'Note the Windows menu up there.', -1, @r,
  53.                   DT_SINGLELINE or DT_CENTER or DT_VCENTER);
  54.                   EndPaint(hWindow, ps);
  55.                 end;
  56.  
  57.     WM_COMMAND : begin
  58.                   case (LOWORD(wParam)) of
  59.  
  60.                   11 :
  61.                      begin        // message 1
  62.                       MessageBox(0, 'This is message 1.', 'Message', MB_OK);
  63.                      end;
  64.                   12 :
  65.                      begin       // message 2
  66.                       MessageBox(0, 'This is message 2.', 'Message', MB_OK);
  67.                      end;
  68.                   19 :
  69.                      begin        // exit application
  70.                       active := false;
  71.                      end;
  72.                   21 :
  73.                      begin      // about
  74.                       MessageBox(0, 'This is a simple example to show you how menus work.', 'About', MB_OK);
  75.                      end;
  76.  
  77.                  end; // case
  78.                 end; // wm_command
  79.  
  80.   else
  81.     Result := DefWindowProc(hEventWindow, Umsg, wParam, lParam);
  82.   end;
  83. end;
  84.  
  85. // Register the Window Class //
  86. procedure RegisterWindow();
  87. var
  88.   WinClass: WndClassEx;
  89.  
  90. begin
  91.   WinClass.cbSize               := Sizeof(WndClassEx);
  92.   WinClass.Style                := cs_hRedraw OR cs_vRedraw;
  93.   WinClass.lpfnWndProc          := WndProc(@WindowProc);
  94.   WinClass.cbClsExtra           := 0;
  95.   WinClass.cbWndExtra           := 0;
  96.   WinClass.hInstance            := system.MainInstance;
  97.   WinClass.hIcon                := LoadIcon(system.MainInstance, makeintresource(1));
  98.   WinClass.hCursor              := LoadCursor(0, idc_Arrow);
  99.   WinClass.hbrBackground        := GetStockObject(LTGRAY_BRUSH);
  100.   WinClass.lpszMenuName         := MenuName;
  101.   WinClass.lpszClassName        := 'WindowClass';
  102.   WinClass.hIconSm              := LoadIcon(system.MainInstance, makeintresource(1));
  103.  
  104.   if RegisterClassEx(WinClass) = 0 then ThrowError('Registering the Windows Class failed!');
  105. end;
  106. // Create the window. Throw error if this fails. //
  107. procedure CreateWindow();
  108. begin
  109.  
  110.  hWindow := CreateWindowEx(WS_EX_TOPMOST, 'WindowClass', AppName,WS_CAPTION OR WS_POPUPWINDOW OR WS_VISIBLE,
  111.                0, 0, 640, 480,0, 0,system.MainInstance,NIL);
  112.  
  113.   if hWindow <> 0 then
  114.   begin
  115.     ShowWindow(hWindow, CmdShow);
  116.     ShowWindow(hWindow, SW_SHOW);
  117.     UpdateWindow(hWindow);
  118.   end
  119.   else ThrowError('Window could not be created');
  120. end;
  121. // Destroy the window. //
  122. procedure KillWindow();
  123. begin
  124.     DestroyWindow(hWindow);
  125. end;
  126. // Main. //
  127. begin
  128.  
  129. active := false;
  130.  
  131.  RegisterWindow();
  132.  CreateWindow();
  133.  
  134. // Main loop. //
  135.    repeat
  136.  
  137.     if PeekMessage(@Amsg,0,0,0,0) = TRUE then
  138.      begin                                    // if message waiting then get it
  139.       GetMessage(@Amsg,0,0,0);
  140.       TranslateMessage(Amsg);
  141.       DispatchMessage(Amsg);
  142.      end;
  143.  
  144. // -------- enter main loop code here -------- //
  145.  
  146.    until active = false;
  147.  KillWindow();
  148. end.                                        
Thanks..

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Compiling with resources.????
« Reply #1 on: July 08, 2020, 06:25:43 pm »
Quote from: MSDN
lpszMenuName

Type: LPCTSTR

Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu.

If I change that it works.

Code: [Select]
  WinClass.lpszMenuName         := MakeIntResource(10);
« Last Edit: July 08, 2020, 06:29:05 pm by marcov »

mav

  • Jr. Member
  • **
  • Posts: 79
Re: Compiling with resources.????
« Reply #2 on: July 08, 2020, 07:02:25 pm »
Yessss!!!It work:Thaknsss

 

TinyPortal © 2005-2018