Simple WIN API Application

Pages: 12
Hello Everyone!

I am very new to windows form applications. I believe it's really elegant to incorporate a friendly GUI with a language like C++. But the thing is there are no manuals that teach form applications, so I'm referring to the C++ community for this:

I want to write a simple program that translates the following lines of code for a win32 app to a WIN API app:

1
2
3
4
5
6
7
8
9
10
11
void main()
{
	ifstream in;
	string s;
	in.open(...);
	while (getline(in, s))
	{
		cout << s << endl;
	}
                 in.close();
}


I want my cout to be in a text box... Like the one I'm using right now to write my post.

Note that I'm using a GCC compiler (not MSVC interface) and relying on the following declaration:
1
2
3
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
...
}


Thanks!
Last edited on
Last edited on
closed account (y07Gz8AR)
Well the simplest Windows API program you can make would either be a window or a message box.

For a window use:
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
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char lpClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpCmdLine,
                    int nCmdShow)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           lpClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}


And for a message box use:
1
2
3
4
5
#include <windows.h>
int main()
{
MessageBox(NULL, "Hello, world!", "Hello world", MB_OK);
}


Any help or such just let me know.
Last edited on
sorry about this post, it's a long story and it's my fault. Just suffice to say I was in the wrong
Last edited on
closed account (y07Gz8AR)
ignore him, he's trolling.


No, ignore this Seraphimsan who is naysaying me.

You want to really see who's true? Post that code I gave and have a compiler with minor varying differences read it.

I guarantee that it will work.
I didn't understand?
Why is Seraphimsan so mad?
What does trolling mean?

Moreover, this line gave an error:
 
wincl.lpClassName = szClassName;


Says:
struct 'WNDCLASSEX' has no member named 'lpClassName' and that
'szClassName was not declared in this scope'
Last edited on
closed account (y07Gz8AR)
Moreover, this line gave an error:

wincl.lpClassName = szClassName;


All right. Then try this code instead and tell me what happens when you compile:
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
#include <windows.h>

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;              
    MSG messages;           
    WNDCLASSEX wincl;       

  
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;     
    wincl.style = CS_DBLCLKS;             
    wincl.cbSize = sizeof (WNDCLASSEX);

  
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                
    wincl.cbClsExtra = 0;                      
    wincl.cbWndExtra = 0;                      
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    if (!RegisterClassEx (&wincl))
        return 0;

   
    hwnd = CreateWindowEx (
           0,                
           szClassName,       
           "Windows App",     
           WS_OVERLAPPEDWINDOW, 
           CW_USEDEFAULT,       
           CW_USEDEFAULT,     
           544,                 
           375,               
           HWND_DESKTOP,       
           NULL,             
           hThisInstance,       
           NULL               
           );

    ShowWindow (hwnd, nFunsterStil);
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }

    return messages.wParam;
}



LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                
    {
        case WM_DESTROY:
            PostQuitMessage (0);    
            break;
        default:                    
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

closed account (z05DSL3A)
ToniAz,

Go to your local library and get hold of a copy of Petzold. Yes it is old but still possibly the best place to start.

[Petzold] Programming Windows, 5th edition by Charles Petzold
A tutorial for programmers wishing to write applications for Windows using the C programming language and the native Win32 application programming interface (API). Programs written using this book run under every version of Windows from Windows 95 through Windows XP and beyond.
imademyownlanguage: Yeah thanks, figured it would be a typo in naming lpszClassName...

Grey Wolf: Thanks a lot! I'm on it, but do you possibly know why, when I compile, then run from compiler or just double click on the built .exe, a win32 console still appears?

Thanks!
closed account (z05DSL3A)
ToniAz,

I can not be sure (I don't use GCC on windows) but I would guess that you are missing a switch on your compiler instruction.

I think that the switch is something like: -Wl,-subsystem,windows

Hope that helps.
Read this: http://www.winprog.org/tutorial/

That is where I learned, A LOT of great information for Win32 C/C++ programming. Just make sure you FULLY understand all of C or C++'s main concepts, it will make things a lot more easy.

Here is some information that I will point out for you:

If you want a basic window you need:

-A window class
This isn't a class like in OOP, this is actually structure that we fill out. The information that we give to this structure is information on our window

-A window handle
A handle is similar to a pointer, you can think of it as "pointing to a window". You will use a window handle as the return for the CreateWindow or the CreateWindowEx function. Those function will actually create the window for you.

-A message loop
A message loop is a while loop (Most the time) that calls two functions: TranslateMessage(&Message); & DispatchMessage(&Message); The first function does some "translating" and performs some tasks that are needed for some features. The 2nd function dispatches the message to your WindowProcedure (Usually called WndProc) function. This function will handle the messages and you will tell it to do what you want it to do in this function.

-Messages
Messages are like signals that are sent from what you do, to the Windowprocedure function. Messages can be a mouse click, a key press, spinning the mouse weel, closing, maximizing, or minimizing the application, and a lot of other things. These will always be an integer variable, and you can handle them in a switch statement in your window procedure function.

All of these concepts, and more, are explained in depth in the tutorial that I linked you to above. If you have any questions you can aim me, my aim is iLeetGamer. Or just post a thread at these forums (I suggest that :P)
closed account (yAqiE3v7)
This is me again, imademyownlanguage(other account got suspended).

But yeah that code for creating a simple Window in Windows API programming can vary. Some people use it in a different way, but that's the basic, straightforward way in which I find easiest to do.

And some random dude is right...If you don't fully understand C++'s tasks regarding the use of Windows API and its calls, functions you will struggle understanding it. If you know some C++ then try and learn more, slowly, but remember to do your best at mastering it. It's much better to be skilled than just briefly knowledgeable, trust me. :)

Any more help with Windows API and I'll help as best I can. :)
To be honest, Win32 is a complete cesspool. They have entire classes just trying to teach it.
Win32 is the most elegant way to write Windows programs. I've made a good career out of replacing failed OOP Class monstrosities with small fast Win32 solutions.
Win32 is the way to go for GUIs. It is easy and doesn't take that long to learn.
closed account (yAqiE3v7)
Actually, it's beyond hard to learn Windows API programming.

Last edited on
It is very easy for me o__o
closed account (yAqiE3v7)
Now take an average bloke for example. A person who is merely struggling with C++ basics to intermediate, and try to get them to understand Windows API programming.

They'll punch the screen within 2 minutes. :)
Hi again,

I still have some 3 irrelevent questions:

(1) What does a troll mean?

(2) What's the difference between WIN API and the projects you do in MSVC (dragging and dropping)

(3) Why do I keep getting a win32 console even though I've created a window?
1 - Someone who insults someone on the internet, going around and meaning mean, ect.

3 - Make sure you made a "Win32 Application" or "GUI Application" and NOT a "Win32 Console Applications" or a "Console Application"
Pages: 12