Author Topic: Weird WinAPI problem  (Read 13774 times)

henniez-swisswater

  • Guest
Weird WinAPI problem
« on: March 06, 2006, 05:00:25 pm »
I have twice the same code. once it works fine the other one doesn't work. the code is supposed to show a window with a static text (created with CreateWindow("STATIC","blabla",...)). the reason for me having twice the same code, is that that I worte one from scratch (not working) and the other one is generate by code::blocks. the code::blocks one works, so I copied the working code, generated by code::blocks, and I overwrote my code. Here coms the strange thing, it still doesn't work. It is exactly the same code. Is it possible than some kind of project setting can cause this problem?

regards 

webwraith

  • Guest
Re: Weird WinAPI problem
« Reply #1 on: March 06, 2006, 05:35:27 pm »
Do you have both pieces of code in the same project? If not, perhaps you should check the build options of the non-working code.

Also, you just say that the code isnt working, how do you mean?

Is it not compiling? compiling, but not linking? What are the error messages (if any)?

henniez-swisswater

  • Guest
Re: Weird WinAPI problem
« Reply #2 on: March 06, 2006, 05:38:11 pm »
o sry for that. it compiles but the static text is nowhere (the non-working one). and yeah they are in two different projects.


webwraith

  • Guest
Re: Weird WinAPI problem
« Reply #3 on: March 06, 2006, 05:58:28 pm »
OK, I'm not quite sure I follow you. what do you mean by static text?

CreateWindow takes two character arrays, one is the name of your previously registered class (i.e.;the lpszClassName member of WNDCLASS structure), this must be the same as your registered window class otherwise Windows will be looking for a class that doesn't exist.
Thoe other character array is the 'caption' of the window, or the text that appears in the titlebar. This can be anything you like, but won't put anything into the client area (the main area of the window).

If you want the equivalent of an error message-type window, use MessageBox() instead. otherwise you're looking at window controls, and there are others out there much more knowledgable about those than I. Your best bet is to invest in a decent book on Win32 api programming

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: Weird WinAPI problem
« Reply #4 on: March 06, 2006, 06:38:29 pm »
CreateWindow takes two character arrays, one is the name of your previously registered class (i.e.;the lpszClassName member of WNDCLASS structure), this must be the same as your registered window class otherwise Windows will be looking for a class that doesn't exist.
Or it may be one of the predefined class names, such as "COMBOBOX" or "STATIC".

Quote
Thoe other character array is the 'caption' of the window, or the text that appears in the titlebar. This can be anything you like, but won't put anything into the client area (the main area of the window).
...unless you're creating a control window as a child window of the main window, or any of a number of similar scenarios.

Quote
otherwise you're looking at window controls, and there are others out there much more knowledgable about those than I.
Indeed...

henniez-swisswater: The Win32 API is a fickle mistress. When I'm forced to use it, I tend to keep the MSDN Library open for reference to any functions I use. When problems like yours show up, it's usually due to some obscure constraint on a Win32 function.

Fortunately, I can usually make use of wxWidgets, which actually makes GUI programming fun.

Good luck...
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

sethjackson

  • Guest
Re: Weird WinAPI problem
« Reply #5 on: March 06, 2006, 06:50:30 pm »
Yeah I agree with TDragon. Win32 API programming is a mess. I used to use it..... Until I found wxWidgets. wxWidgets does make GUI programming fun. :D

Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Weird WinAPI problem
« Reply #6 on: March 06, 2006, 07:54:17 pm »
I think it's only a mess when you are new to it.  :? You learn to do a cleaner job in time. My first win32 programs were messes all over the place, because I was new to it, and I didn't understand it as much. After you've been usng it awhile, you start to understand more how everything works together, and it becomes more clean and organised as a result.
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

sethjackson

  • Guest
Re: Weird WinAPI problem
« Reply #7 on: March 06, 2006, 07:57:49 pm »
 :lol: I used it for two years, and I still think it was a mess.  :P

henniez-swisswater

  • Guest
Re: Weird WinAPI problem
« Reply #8 on: March 06, 2006, 08:02:45 pm »
@TDragon: Indeed I wanted to use the predefined "STATIC" "BUTTON" etc. But I reckon I would be best to get a book too. Does anyone of you know a good one?

sethjackson

  • Guest
Re: Weird WinAPI problem
« Reply #9 on: March 06, 2006, 08:05:37 pm »
Yes.

http://www.amazon.com/exec/obidos/ASIN/157231995X/winprognet-20/102-5163610-7788162

Old, but good. I don't have it anymore. Ditched it after I started using wxWidgets.

Also look here.

http://www.winprog.org/tutorial/references.html

A good Win32 API tutorial is here.

http://www.winprog.org/tutorial/index.html

Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Weird WinAPI problem
« Reply #10 on: March 06, 2006, 08:10:00 pm »
:lol: I used it for two years, and I still think it was a mess.  :P
Why not give a side by side example of how wx does the same task easier?  :P The only thing I don't like is filling out structs all the time.
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

sethjackson

  • Guest
Re: Weird WinAPI problem
« Reply #11 on: March 06, 2006, 08:22:38 pm »
:lol: I used it for two years, and I still think it was a mess.  :P
Why not give a side by side example of how wx does the same task easier?  :P The only thing I don't like is filling out structs all the time.

Do I really need to? It is obvious. wxWidgets is cross-platform Win32 API is not.

Also as for documentation I can actually find what I'm looking for in the wx manual......  :lol:

Anyways as for creating a window here we go.

wx

Code: cpp
wxFrame* frame = new wxFrame(0, wxID_ANY, _("Test Window"), wxDefaultPosition, wxDefaultSize);

Win32 API

Code: cpp
// Get the hInstance from somewhere. Generally from the window procedure

HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,
                           "myWindowClass",
                           "Test Window",
                           WS_OVERLAPPEDWINDOW,
                           CW_USEDEFAULT,
                           CW_USEDEFAULT,
                           240,
                           120,
                           0,
                           0,
                           hInstance,
                           0);


henniez-swisswater

  • Guest
Re: Weird WinAPI problem
« Reply #12 on: March 06, 2006, 08:35:13 pm »
oh by the way I solved the inital problem with the two versions where one showed the text and the other didn't. It was some config of c:b. I deleted all project files except the .cbp. afterward, everything was back to normal:P at last. Now I can go on with my thrilling WinAPI experience:P I'll finish this project with WinAPI. But the next one is gonna be some crossplatform API. Probably wx.Actually, are there any other?  I know this goes a bit off-topic, sry.

sethjackson

  • Guest
Re: Weird WinAPI problem
« Reply #13 on: March 06, 2006, 08:39:08 pm »
Yes there are others. GTK+, Qt, Fox, FLTK.

Here is a page comparing these to wx.

http://www.wxwidgets.org/wiki/index.php/WxWidgets_Compared_To_Other_Toolkits


Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Weird WinAPI problem
« Reply #14 on: March 06, 2006, 08:51:55 pm »
What exactly did that prove other than they are very similar calls? One has a few more arguments, so what?  :? (I'm not trying to do a win32 vs WX here, I was just curious). I wanted to see an example of actually getting something done.
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: Weird WinAPI problem
« Reply #15 on: March 06, 2006, 09:01:39 pm »
What exactly did that prove other than they are very similar calls? One has a few more arguments, so what?  :? (I'm not trying to do a win32 vs WX here, I was just curious). I wanted to see an example of actually getting something done.

I think you 're comparing oranges to apples here...

wxWidgets (like any other toolkit) takes away the need to manually manage memory resources. This fact alone should make this discussion stop right here.
There's not much to talk about. Use whatever you feel like it's better for the job. Really.

I 've seen all kinds of holly wars going on in various forums. But praising the Winapi? That's a first ;)!
Sorry, I couldn't resist :lol:
« Last Edit: March 06, 2006, 09:03:11 pm by mandrav »
Be patient!
This bug will be fixed soon...

sethjackson

  • Guest
Re: Weird WinAPI problem
« Reply #16 on: March 06, 2006, 09:05:54 pm »
Yeah use whatever you want to. Lets not start a war or something. :D I like wx because it is cross-platform. Code once run everywhere (mostly).
Like Yiannis said you are the first person to praise the Win32 API that I know of. :lol:

Offline Vampyre_Dark

  • Regular
  • ***
  • Posts: 255
  • Hello!
    • Somewhere Over The Rainbow...
Re: Weird WinAPI problem
« Reply #17 on: March 06, 2006, 10:19:13 pm »
No war whatsoever guys.

Here is some code where I ineract with some buttons on my game's config program to choose resolution. It uses the win32 api to interact with the various controls on the dialog. It's fairly straightforward and clean:
Code
    //get the resolution selection
    LRESULT ResSel = SendMessage(hListRes,LB_GETCURSEL,0,0);
    if (ResSel == LB_ERR) nRes = 0;
    else nRes = ResSel;

    if (IsDlgButtonChecked(hMain,IDC_CHK_FULLSCREEN) == BST_CHECKED)
        bFullScreen = true;
            else bFullScreen = false;

    if (IsDlgButtonChecked(hMain,IDC_CHK_VSYNC) == BST_CHECKED)
        bVSync = true;
            else bVSync = false;

    if (IsDlgButtonChecked(hMain,IDC_CHK_SOUND) == BST_CHECKED)
        bSound = true;
            else bSound = false;


Here is quick code where I toggle back the default values, once again very clean.
Code
void DefaultConfig(void)
{
    CheckDlgButton(hMain,IDC_CHK_FULLSCREEN,BST_CHECKED);
    CheckDlgButton(hMain,IDC_CHK_VSYNC,BST_CHECKED);
    CheckDlgButton(hMain,IDC_CHK_SOUND,BST_CHECKED);

    SendMessage(hListRes,LB_SETCURSEL,0,0);

    return;
}

The base of the program couldn't be simpler...

Code
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpszArg, int nStyle)
{

    //load the config dialog
    DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DLG1), NULL, (DLGPROC)DlgProc);

    return 0;
}


LRESULT CALLBACK DlgProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

    HRESULT hRes;

    switch(Msg)
    {

        case WM_INITDIALOG:
        {
            //grab all the controls, and toggle
            //their default states
            hMain = hWnd;

            hListRes = GetDlgItem(hMain,IDC_RESLIST);
            hChkFullScreen = GetDlgItem(hMain,IDC_CHK_FULLSCREEN);
            hChkVSync = GetDlgItem(hMain,IDC_CHK_VSYNC);
            hChkSound = GetDlgItem(hMain,IDC_CHK_SOUND);

            BuildList();

            //if the config file cannot be loaded,
            //set the default options
            DefaultConfig();

            LoadConfig();

        }break;


        case WM_COMMAND:
        {
            switch( LOWORD(wParam) )
            {


                case IDC_BTN_SAVE:
                {
                    SaveConfig();
                    EndDialog(hMain,0);
                    break;
                }


                case IDC_BTN_CANCEL:
                {
                    EndDialog(hMain,0);
                    break;
                }


                case IDC_BTN_DEFAULT:
                {
                    DefaultConfig();
                    break;
                }


            }break;
        }

        //window being destroyed
        case WM_CLOSE:
        case WM_QUIT:
        case WM_DESTROY:
        {
            EndDialog(hMain,0);
            break;
        }

        default:
            return false; // Didn't handle message
    }

    return true;
}

I've written lots of little things over the past year since I got more comfortable with win32 api, and it's all simple and clean. I have something that is way more complicated archived around here somewhere with tabbed pages inside a dialog and all this other junk and it's still clean and straightforward.  :|
C::B Wishlist
~BOYCOTT THE EVIL YELLOW BOXES~

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: Weird WinAPI problem
« Reply #18 on: March 06, 2006, 10:33:36 pm »
Don't make me post my Win32 and wxWidgets versions of the level editor for my 2d side-scroller game. There is a marked difference between the two with respect to the amount of code, the simplicity of the code, and the functionality of the final product. The wxWidgets version wins hands down. Some places these really come into play are in properly arranging the toolbar and child editor windows (not more than 10 lines of code total, and everything adjusts itself when the parent window changes), and in scrolling the child editor windows (not more than 5 lines of code per child window). And I don't have to code all the hacks to make the scrolling work properly, because the creators of wxWidgets already did for me.
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline kkez

  • Almost regular
  • **
  • Posts: 153
    • WinapiZone
Re: Weird WinAPI problem
« Reply #19 on: March 07, 2006, 01:01:16 pm »
I think it's only a mess when you are new to it.  :? You learn to do a cleaner job in time. My first win32 programs were messes all over the place, because I was new to it, and I didn't understand it as much. After you've been usng it awhile, you start to understand more how everything works together, and it becomes more clean and organised as a result.
*
I totally agree with you :)