C Generic Applications


C++ Generic Applications

There is almost no difference between C++ and Delphi API applications. The only two differences are:

  • Syntax differences between Delphi and C++ languages

  • Since the main begin-end block doesn't exist in C++ applications, the window class registration, creation of the main window, and the message loop is written inside the WinMain function.

To create a generic C++ application in C++Builder, select the Console Application item to start creating a new console application project and uncheck the Console Application radio button on the New Console Application dialog box. When you uncheck the Console Application radio button, the IDE will generate the appropriate WinMain function instead of the main function:

#include <windows.h> #pragma hdrstop #pragma argsused WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {    return 0; }

To create the API application, you have to register and create the window in the WinMain function and add a WindowProc function that will handle messages for the window. The following listing shows the complete source code for a simple C++ API application.

Listing 23-10: A simple C++ API application

image from book
#include <windows.h> #pragma hdrstop const char* GENERIC_WNDCLASS = "CppBuilderWindow"; HWND hMainWindow; LRESULT CALLBACK WindowProc(HWND hwnd,   UINT uMsg, WPARAM wParam, LPARAM lParam) {    switch(uMsg) {       case WM_DESTROY: {         PostQuitMessage(0);         return 0;       }    }    return DefWindowProc(hwnd, uMsg, wParam, lParam); } #pragma argsused WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,    LPSTR lpCmdLine, int nCmdShow) {    MSG msg;    WNDCLASS wClass;    wClass.cbClsExtra = 0;    wClass.cbWndExtra = 0;    wClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);    wClass.hCursor = LoadCursor(NULL, IDC_ARROW);    wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);    wClass.hInstance = hInstance;    wClass.lpszClassName = GENERIC_WNDCLASS;    wClass.lpfnWndProc = WindowProc;    wClass.lpszMenuName = NULL;    wClass.style = CS_HREDRAW | CS_VREDRAW;    RegisterClass(&wClass);    // create the main window    hMainWindow = CreateWindow(GENERIC_WNDCLASS,       "C++ Win32 API Application", WS_OVERLAPPEDWINDOW,       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,       CW_USEDEFAULT, NULL, NULL, hInstance, NULL);    ShowWindow(hMainWindow, SW_SHOWNORMAL);    UpdateWindow(hMainWindow);    // enter the message loop    while(GetMessage(&msg, NULL, 0, 0)) {       TranslateMessage(&msg);       DispatchMessage(&msg);    }    return msg.wParam; }
image from book



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net