Page 1 of 2

[SOLVED] C Windows API Issues....

Posted: Fri Aug 14, 2009 11:29 pm
by jackpf
Hello everyone...

I know this is a php forum, and I've posted this on a few C forums as well...but you know...since this is such a responsive community... ;)

Anyway, I've decided to have a bash at C. I can handle CLI programs fine - it's basically the same as PHP. However, I've been trying to use the windows API to create a graphical user interface...to no avail.

Here's my code:

Code: Select all

#include <windows.h>
 
const char Window_Class[] = "WindowClass";
 
//window callback handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    //switch message...
    switch(msg)
    {
        //close window
        case WM_CLOSE:
            MessageBox(hwnd, "Bye", ":)", 0);
            DestroyWindow(hwnd);
        break;
        //display window
        case WM_PAINT:
            RECT rcClient;
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
 
            DrawTextEx(hdc, "Hello...world?"+'\0', -1, &rcClient, DT_CENTER, NULL);
            
            EndPaint(hwnd, &ps);
        break;
        //register window process
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
 
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    char *lpCmdLine, int nCmdShow)
{
    //register windows classes
    WNDCLASSEX  window;
    HWND        hwnd;
    MSG         msg;
 
    //register window with....windows ;)
    window.cbSize           = sizeof(WNDCLASSEX);
    window.style            = 0;
    window.lpfnWndProc      = WndProc;
    window.cbClsExtra       = 0;
    window.cbWndExtra       = 0;
    window.hInstance        = hInstance;
    window.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    window.hCursor          = LoadCursor(NULL, IDC_ARROW);
    window.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1);
    window.lpszMenuName     = NULL;
    window.lpszClassName    = Window_Class;
    window.hIconSm          = LoadIcon(NULL, IDI_APPLICATION);
 
    //...
    RegisterClassEx(&window);
 
    //create the window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        Window_Class,
        "Window!",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 250,
        NULL, NULL, hInstance, NULL);
 
    //run...
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    //run the message loop
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return msg.wParam;
}
 
I'm using DrawTextEx() to display the text...but it's just not showing. I'm using Windows XP, and CodeBlocks as my compiler...

Any help would be awesome,

Cheers,
Jack.

Re: C Windows API Issues....

Posted: Sat Aug 15, 2009 2:16 am
by VladSun
I can't answer your question but you could use WinBinder instead ;)

Re: C Windows API Issues....

Posted: Sat Aug 15, 2009 6:17 am
by jackpf
Hmm....but don't these things just create command line programs? The point of this was to make a "window"... :D

I've always been sceptical of these PHP compilers...do you know how they actually work?

Anyway, I'd love to do this in C. It's such an awesome language....

Btw, I found that you're supposed to call EndPaint() after BeginPaint() (I updated my code) but it unfortunately didn't make a difference...

Re: C Windows API Issues....

Posted: Sat Aug 15, 2009 10:43 am
by redmonkey
I'm not familiar with this particular Windows API but that code on it's own won't do anything, I'm guessing there must be more to your program?

Also, CodeBlocks isn't a compiler, it's an IDE. CodeBlocks has an optional/alternative installer which includes Mingw ports of some GNU compilers. The last time I looked at CodeBlocks it didn't seem to care if you chose a C or C++ project everything was punted to g++ (a C++ compiler) for building, which may lead to problems.
jackpf wrote:it's basically the same as PHP.
I think that's one of the biggest issues for people, C developers looking at PHP scripts for the first feel confident that they can deal with it easily as many areas of the syntax of the two languages are similar, this also appears to be the case vice versa for PHP developers looking at C for the first time. The reality is that while much of the syntax is similar the semantics are wildly different.

Re: C Windows API Issues....

Posted: Sat Aug 15, 2009 3:12 pm
by jackpf
Ahh, I found what I was looking for at msdn.microsoft.com. It was TextOut(). So simple...

And all that code does is display a window with some text. It's pretty simple...I'm just trying to get the hang of the windows api. And this windows api...is the windows api. windows.h. I'm just using windows functions to display the window.

I'm not sure I agree...command line C programs are extremely similiar to php. They have the same constructs...similiar syntax....

I think working with an OS API is a bit harder...but once you learn a few of the API functions and classes, you get the hang of it.

Anyway, the final code:

Code: Select all

#include <windows.h>
 
#define Window_Class "WindowClass"
 
const char Title[]      = "Title!";
const char Message[]    = "Hello world :)";
 
//window callback handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    //switch message...
    switch(msg)
    {
        //display window
        case WM_PAINT:
            RECT rcClient;
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);
 
            TextOut(hdc,
                    5, 5,
                    Message, strlen(Message));
 
            EndPaint(hwnd, &ps);
        break;
        //close window
        case WM_CLOSE:
            MessageBox(hwnd, "Bye", ":)", MB_OK);
            DestroyWindow(hwnd);
        break;
        //register window process
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
        break;
    }
 
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    char *lpCmdLine, int nCmdShow)
{
    //register windows classes
    WNDCLASSEX  window;
    HWND        hwnd;
    MSG         msg;
 
    //register window with....windows ;)
    window.cbSize           = sizeof(WNDCLASSEX);
    window.style            = 0;
    window.lpfnWndProc      = WndProc;
    window.cbClsExtra       = 0;
    window.cbWndExtra       = 0;
    window.hInstance        = hInstance;
    window.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
    window.hCursor          = LoadCursor(NULL, IDC_ARROW);
    window.hbrBackground    = (HBRUSH) (COLOR_WINDOW + 1);
    window.lpszMenuName     = NULL;
    window.lpszClassName    = Window_Class;
    window.hIconSm          = LoadIcon(NULL, IDI_APPLICATION);
 
    //...
    RegisterClassEx(&window);
 
    //create the window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        Window_Class,
        Title,
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 250,
        NULL, NULL, hInstance, NULL);
 
    //run...
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    //run the message loop
    while(GetMessage(&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
 
    return msg.wParam;
}
I had a go at expanding on that today, and made my own browser :) I'm actually using it right now...
It's dead simple, but still...good to learn your way around the windows API.

If anyone wants to take a look, http://jackpf.co.uk/blog/Programming. There should be a download link (just about to post it).

But yeah, thanks for your help anyway,
Regards,
Jack.

Re: [SOLVED] C Windows API Issues....

Posted: Sat Aug 15, 2009 3:19 pm
by Eran
it's not just a different API and functions. Though syntactically similar there are some very basic core differences such as memory management / garbage collection, pointers, OOP support, exception handling and so forth.

Just cause the syntax looks familiar to you doesn't mean you can expect to use PHP programming practices in C and build significant, stable applications.

Re: [SOLVED] C Windows API Issues....

Posted: Sat Aug 15, 2009 3:26 pm
by jackpf
True.

Yeah, it took me a while to grasp pointers....with the deference operator and such.

In PHP, you'd just do:

Code: Select all

$var1 = 'something';
 
$var2 = &$var1;
 
//$var2 now accesses the same memory $var1 does
 
$var2 = 'something else'; // $var1 = $var2 = 'something_else'
Whereas in C:

Code: Select all

int a = 2, *b; //b is a pointer
b = &a;
//b has a's memory
*b = 5; //a = b = 5

Re: [SOLVED] C Windows API Issues....

Posted: Sat Aug 15, 2009 3:32 pm
by Eran
important to note that C pointers are nothing like PHP references. The first points to a memory address and the second is simply an alias to the same parameter.

You can look in the PHP manual for examples of how they differ. http://us3.php.net/manual/en/language.references.php

Re: [SOLVED] C Windows API Issues....

Posted: Sat Aug 15, 2009 5:09 pm
by redmonkey
It doesn't matter if you're writing a console or GUI application in C, the syntax and semantics are the same.

Re: [SOLVED] C Windows API Issues....

Posted: Fri Aug 21, 2009 1:26 am
by alex.barylski
Jack: If you want to build real Windows applications you really really need to switch to MFC and use the application wizard in VS to build a base application and extend it from there. Windows API is a complex beast and understanding that is critical to Windows application development but MFC will save you SOOOOOOO much effort it's not even funny.

Also, you'd have far better luck in getting replies over at CodeProject.com which is a community with articles and C/C++ developers -- likewise their PHP community sucks unlike here. :P

Re: [SOLVED] C Windows API Issues....

Posted: Fri Aug 21, 2009 6:43 am
by jackpf
Yeah...I just wanted to see if I could make my own window using the windows API without having a program write it for me.

And I can :D

But yeah, I agree that VS does make stuff a lot easier it's unreal.

And I posted this on http://forums.devshed.com as well...got some pretty good responses there. As I said, since both languages are so similar, I just thought someone might have some sort of knowledge or experience about my problem here :D

Re: [SOLVED] C Windows API Issues....

Posted: Fri Aug 21, 2009 6:51 am
by VladSun
jackpf wrote:... since both languages are so similar ...
They are not, indeed.

Re: [SOLVED] C Windows API Issues....

Posted: Fri Aug 21, 2009 6:56 am
by jackpf
I find them fairly similar.

Like, I think I "know" PHP.

I've never been to a C class or anything, but I'm still able to write programs in it.

I'd put that down to my prior knowledge of PHP.

If someone asked me to write a program in Delphi or something, I'd have no idea how to. Because it's nothing like PHP, or anything else I know.

Re: [SOLVED] C Windows API Issues....

Posted: Fri Aug 21, 2009 6:59 am
by VladSun
Well, you could write a snippet in Perl and it will work, but still it doesn't mean that Perl and PHP are "so similar".

Re: [SOLVED] C Windows API Issues....

Posted: Sat Aug 22, 2009 4:58 pm
by alex.barylski
Perl, PHP, JavaScript, C++ and many others are all derivatives of C...but they really only share a common syntax.

To effectively program in C/C++ you need to understand pointers and memory management whereas in PHP this is a none-issue. Likewise in PHP you need to be familiar with the Internet and it's protocols (specifically HTTP) whereas most desktop applications get along fine without any Internet connectivity.

JavaScript requires an understanding of a DOM, Windows programming requires good understanding of Windows API, etc.

Perl has a cryptic syntax but still has the ubiquitous semi-colon to indicate the end of a statement and the curly braces for indicating block scope.

Learning the language, IMHO is not the difficult part (once you understand programming concepts; loops, conditionals, etc) as it's really a matter of learning the syntax. It's all the associated technologies that go along with the language as a technology itswelf.