GobeRadJem32
GobeRadJem32

Reputation: 102

winapi how to create command link in MinGW

i'm programming in c++ winapi. My compiler is MinGW 7.3.0. Command button control:enter image description here

This picture is snipped from Google. I can make command button with this code:

HWND hCmdLnk = 
CreateWindow("classname",
             "Example",
            BS_COMMANDLINK,
              hwnd, (HMENU)
              1, NULL, 
              NULL);

And error is:

'BS_COMMANDLINK' was not 
 declarated in this scope!

I included next libraries:

windows.h
commctrl.h

Why compiler says this error, if BS_COMMANDLINK, writed in microsoft documentation, defined in windows.h/commctrl.h?

Thanks for answers.

Upvotes: 0

Views: 816

Answers (1)

Rita Han
Rita Han

Reputation: 9710

'BS_COMMANDLINK' was not declarated in this scope!

Since BS_COMMANDLINK require NTDDI_VERSION >= 0x06000000 in commctrl.h, a NTDDI version higher than 0x0600 is required.

You can define it in your source file like this:

#define _WIN32_WINNT 0x0601

A working code example like this:

#define _WIN32_WINNT 0x0601
    
#include <windows.h> 
#include <commctrl.h>
    
//...

int WINAPI wWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
{ 
    InitCommonControls();
   
    HWND hCmdLnk = CreateWindowW(
       L"BUTTON",  // Predefined class; Unicode assumed
       L"",        // Text will be defined later
       WS_VISIBLE | BS_COMMANDLINK,  // Styles
       200,        // x position 
       10,         // y position 
       200,        // Button width
       100,        // Button height
       NULL,     // Parent window
       NULL,       // No menu
       hInstance,
       NULL);      // Pointer not needed

    SendMessage(hCmdLnk, WM_SETTEXT, 0, (LPARAM)L"Command link 2");
    SendMessage(hCmdLnk, BCM_SETNOTE, 0, (LPARAM)L"Line 2");
    //... 

  } 

Compile it using MinGw like this:

i686-w64-mingw32-g++ hello.cpp -mwindows

Refer to "Using the Windows Headers" for more detailed informaton.


And in order to make the command link button display correctly the Comctl32 library need to be linked and an XML manifest specifying that version 6 of the Windows common controls library must be loaded by Windows, and embed it into your application as a resource with type "RT_MANIFEST".

The manifest.xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity 
      type="Win32" 
      name="Microsoft.Windows.Common-Controls" 
      version="6.0.0.0" 
      processorArchitecture="*" 
      publicKeyToken="6595b64144ccf1df" 
      language="*"/>
    </dependentAssembly>
  </dependency>
</assembly> 

The resource file (resource.rc):

#define CREATEPROCESS_MANIFEST_RESOURCE_ID  1
#define RT_MANIFEST  24

CREATEPROCESS_MANIFEST_RESOURCE_ID RT_MANIFEST "manifest.xml"

Compile the code and resource files and link them all into an executable application. Select UNICODE via -municode option.

i686-w64-mingw32-g++ -municode -c commandLinkTest.cpp -o commandLinkTest.o
windres -i resource.rc -o resource.o
i686-w64-mingw32-g++ -o commandLinkTest.exe commandLinkTest.o resource.o -s -lcomctl32 -Wl,--subsystem,windows -municode

The file list:

enter image description here

The command link button window will like this:

enter image description here

Upvotes: 1

Related Questions