Windows App - C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phlip

    #16
    Re: Windows App - C++

    August1 wrote:
    [color=blue]
    > Irrelevent only to those who find it irrelevent, or dedicate much of
    > their time in C/C++ with console applications. Many people today
    > prefer to use C++ for Windows apps, so they have a greater
    > understanding of the language and use it for more than console
    > applications. Therefore they wouldn't find it difficult to answer
    > console-related or gui-related questions should any be presented.[/color]

    Oh, dear. Yet another newbie challenging the topicality protocols.

    Why don't we all just discuss keyboard layouts while we're at it?

    August1, only an elite minority are capable of answering technical
    questions, on fora of _their_ choice, and you are danged lucky to have such
    a wide selection of them. However, posting to the narrowest technical
    newsgroup possible is

    ==> in your best interest. <==

    If, for example, you post a Linux question to a Microsoft newsgroup, you
    very well might get a good answer, and a hearty discussion. If you instead
    post to a Linux newsgroup, the regulars there will _compete_ with each other
    to provide the best answer. They will review each others' answers, provide
    emmendments and additions, and generally aid the Linux cause.

    Apologies on behalf of my e-siblings who can only phrase this in terms of
    territory - "you are off-topic, so get lost". That's stimulating to write,
    but hardly enfranchising to read. You yourself can also try to do better.

    --
    Phlip



    Comment

    • Mike Wahler

      #17
      Re: Windows App - C++

      "Phlip" <phlip_cpp@yaho o.com> wrote in message
      news:GZddd.9236 $5b1.7143@newss vr17.news.prodi gy.com...

      [excellent presentation of advantages of topicality]

      Very well put. Kudos.

      -Mike


      Comment

      • Default User

        #18
        Re: Windows App - C++

        August1 wrote:

        [color=blue]
        > Irrelevent only to those who find it irrelevent, or dedicate much of
        > their time in C/C++ with console applications.[/color]

        You've been told that this is off-topic. Instead of acknowledging that
        and apologizing, you argue. To what end? Do you think you'll
        everybody's mind. Instead, what happens is this:


        *plonk*




        Brian

        Comment

        • August1

          #19
          Re: Windows App - C++

          No point has been missed, no insults have been offered to anyone, no
          exclamations have been added for emphasis to convey a conviction, no
          profanity has been offered, no "misconduct " has been presented. i do
          however find some of the responses interesting.

          regards

          Comment

          • Phlip

            #20
            Re: Windows App - C++

            August1 wrote:
            [color=blue]
            > No point has been missed, no insults have been offered to anyone, no
            > exclamations have been added for emphasis to convey a conviction, no
            > profanity has been offered, no "misconduct " has been presented. i do
            > however find some of the responses interesting.[/color]

            Attempt to be gracious noted! Now answer questions about C++ here.

            --
            Phlip



            Comment

            • Alf P. Steinbach

              #21
              Re: Windows App - C++

              * August1:[color=blue]
              >
              > I'm beginning to approach Windows programming in C++. I've written
              > a short
              > application which is to do nothing more than demonstrate the Window
              > style being used. The child windows are not appearing within the
              > parent window. Could someone point out the cause?[/color]

              Because you forgot to compile and run the program?

              If you have any problems with compiling, ask in a compiler-specific
              newsgroup.

              If you have any problems getting the program to run, ask in a Windows-specific
              newsgroup.


              [color=blue]
              > #include <windows.h>[/color]

              For C++ always #define STRICT and NOMINMAX before this #include.

              [color=blue]
              > #include <string.h>[/color]

              Preferentially use


              #include <cstring>


              [color=blue]
              > long WINAPI MainWndProc(HWN D,UINT,WPARAM,L PARAM);
              >
              > HWND hWnd;[/color]

              Don't use global variables.

              [color=blue]
              > HWND hwndEdit;
              > HWND hwndButton;
              > HWND hwndClearButton ;
              > char szMessage[50] = "";[/color]

              Here you're into buffer overflow territory; instead consider
              std::string.


              [color=blue]
              > int WINAPI WinMain(HINSTAN CE hInstance,
              > HINSTANCE hPrevInstance,
              > LPSTR lpCmdLine,
              > int nCmdShow)
              > {[/color]

              Check out your compiler's support for standard 'main'. g++ supports
              that directly for Windows GUI application. Use standard 'main'.

              [color=blue]
              > WNDCLASS wc;
              > wc.lpszClassNam e = "Style1";
              > wc.lpfnWndProc = MainWndProc;
              > wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
              > wc.hInstance = hInstance;
              > wc.hIcon = LoadIcon(NULL,I DI_APPLICATION) ;
              > wc.hCursor = LoadCursor(NULL ,IDC_ARROW);
              > wc.hbrBackgroun d = (HBRUSH)( COLOR_WINDOW+1 );
              > wc.lpszMenuName = "";
              > wc.cbClsExtra = 0;
              > wc.cbWndExtra = 0;
              > RegisterClass(& wc);[/color]

              A simpler way might be to use an initializer, "WNDCLASS wc = { ... };".

              Anyway, to ensure all non-assigned fields are zero, declare it like


              WNDCLASS wc = {0};



              [color=blue]
              > hWnd = CreateWindow("S tyle1","Border Demonstration",
              > WS_OVERLAPPEDWI NDOW,CW_USEDEFA ULT,CW_USEDEFAU LT,
              > 185,265,NULL,NU LL,hInstance,NU LL);
              >
              > hwndEdit = CreateWindow("E DIT",NULL,
              > WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
              > 10,10,155,20,hW nd,NULL,hInstan ce,NULL);
              >
              > hwndButton = CreateWindow("B UTTON","Message ",
              > WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTO N,
              > 10,40,35,35,hWn d,NULL,hInstanc e,NULL);
              >
              > hwndClearButton = CreateWindow("B UTTON","Clear",
              > WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTO N,
              > 50,40,35,35,hWn d,NULL,hInstanc e,NULL);[/color]

              Error checking might be a good idea... For example using a function

              template< typename T >
              inline void throwIf0( T const& x )
              {
              if( !x ) { throw std::runtime_er ror( "Ooops." ); }
              }

              Then

              hWnd = CreateWindow("S tyle1","Border Demonstration",
              WS_OVERLAPPEDWI NDOW,CW_USEDEFA ULT,CW_USEDEFAU LT,
              185,265,NULL,NU LL,hInstance,NU LL);
              throwIf0( hWnd );

              of course with a 'catch' somewhere.

              [color=blue]
              > ShowWindow(hWnd ,nCmdShow);
              >
              > MSG msg;
              > while(GetMessag e(&msg,NULL,0,0 ))
              > {
              > TranslateMessag e(&msg);
              > DispatchMessage (&msg);
              > }
              > return msg.wParam;[/color]

              Here should be cast to the return type, i.e.


              return static_cast<int >( msg.wParam );

              [color=blue]
              > }//end WinMain()[/color]



              [color=blue]
              > long WINAPI MainWndProc(HWN D hWnd,UINT msg,WPARAM wParam,LPARAM
              > lParam)
              > {
              > HWND hwndCtl = (HWND)lParam;[/color]

              Don't use C style casts.

              In this case, use a reinterpret_cas t or the appropriate system-specific
              macro/function (off-topic hint: <windowsx.h>) .

              Anyway the logic is flawed: you don't yet have enough information to
              say that this argument represents what you think it does. But it works.
              With a bit of maintainance it will cease to work.

              [color=blue]
              > switch(msg)
              > {
              > case WM_COMMAND:
              > switch(wParam)
              > {
              > case BN_CLICKED:
              > if(hwndCtl == hwndButton)
              > {
              > strcpy(szMessag e,"This program is an overlapped style Window.");[/color]

              Buffer overflow?

              Indentation!

              [color=blue]
              > SetWindowText(h wndEdit,szMessa ge);
              > }
              > else if(hwndCtl == hwndClearButton )
              > {
              > strcpy(szMessag e,"");
              > SetWindowText(h wndEdit,szMessa ge);
              > }
              > }
              > break;
              > case WM_DESTROY:
              > PostQuitMessage (0);
              > return 0;
              > default:
              > return DefWindowProc(h Wnd,msg,wParam, lParam);
              > }
              >
              > return 0;[/color]

              Unreachable.
              [color=blue]
              > }//end MainWndProc()[/color]

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Phlip

                #22
                Re: Windows App - C++

                Alf P. Steinbach wrote:
                [color=blue][color=green]
                > > #include <windows.h>[/color]
                >
                > For C++ always #define STRICT and NOMINMAX before this #include.[/color]

                And WIN32_LEAN_AND_ MEAN!
                [color=blue][color=green]
                > > #include <string.h>[/color]
                >
                > Preferentially use
                >
                > #include <cstring>[/color]

                James Kanze points out don't use the C++-style C Library Headers unless you
                really really mean it. Right now, in our legacy-code besotted world,
                <cstring> is a portability dead spot, and <string.h> is everywhere you need
                to be.

                --
                Phlip



                Comment

                • August1

                  #23
                  Re: Windows App - C++

                  > You've been told that this is off-topic. Instead of acknowledging that[color=blue]
                  > and apologizing, you argue. To what end? Do you think you'll
                  > everybody's mind. Instead, what happens is this:
                  >
                  >
                  > *plonk*[/color]

                  I've not laughed so heartily in about 1 month. Thanks to all.
                  regards

                  Comment

                  • Ioannis Vranos

                    #24
                    Re: Windows App - C++

                    August1 wrote:[color=blue]
                    > Hi,
                    >
                    > I'm beginning to approach Windows programming in C++. I've written
                    > a short
                    > application which is to do nothing more than demonstrate the Window
                    > style being used. The child windows are not appearing within the
                    > parent window. Could someone point out the cause?[/color]


                    I am using .NET so I do not know these Win32 stuff. In any case,
                    comp.lang.c++ is about talking ISO C++, that is the pure C++ language as
                    defined by the ISO C++ standard.

                    For questions about the trillions of system specific applications of
                    C++, one should post the relevant newsgroups.

                    For example there are MS newsgroups for MS platform programming and in
                    particular MS provides a free newsgroup server


                    msnews.microsof t.com


                    with all the newsgroups regarding their platforms.


                    Just configure your newsgroup reader to access that server and find the
                    relevant newsgroups.


                    Also there are some of those MS newsgroups, and other non-MS newsgroups
                    about Win32 available in Usenet (Usenet is where you found this
                    newsgroup), just search for newsgroups with the word Win32 in your
                    newsgroup client.



                    --
                    Ioannis Vranos


                    Comment

                    • E. Robert Tisdale

                      #25
                      Re: Windows App - C++

                      August1 wrote:

                      [Something that calls itself Default User wrote:]
                      [color=blue][color=green]
                      >>You've been told that this is off-topic.
                      >>Instead of acknowledging that and apologizing, you argue. To what end?
                      >>Do you think you'll [change] everybody's mind.
                      >>Instead, what happens is this:
                      >>
                      >>*plonk*[/color]
                      >
                      > I've not laughed so heartily in about 1 month. Thanks to all.
                      > regards[/color]

                      Please ignore our trolls.
                      They seem to think that it's fun to ambush new subscribers.

                      The comp.lang.c++ newsgroup is a good place to get bad advice
                      about Windows specific programming.
                      I hope the you found the redirection to more appropriate forums helpful.

                      Please come back to us
                      when you have specific questions about standard C++.

                      Comment

                      • August1

                        #26
                        Re: Windows App - C++

                        > Because you forgot to compile and run the program?[color=blue]
                        >
                        > If you have any problems with compiling, ask in a compiler-specific
                        > newsgroup.
                        >
                        > If you have any problems getting the program to run, ask in a Windows-specific
                        > newsgroup.
                        >
                        >
                        >[color=green]
                        > > #include <windows.h>[/color]
                        >
                        > For C++ always #define STRICT and NOMINMAX before this #include.
                        >
                        >[color=green]
                        > > #include <string.h>[/color]
                        >
                        > Preferentially use
                        >
                        >
                        > #include <cstring>
                        >
                        >
                        >[color=green]
                        > > long WINAPI MainWndProc(HWN D,UINT,WPARAM,L PARAM);
                        > >
                        > > HWND hWnd;[/color]
                        >
                        > Don't use global variables.
                        >
                        >[color=green]
                        > > HWND hwndEdit;
                        > > HWND hwndButton;
                        > > HWND hwndClearButton ;
                        > > char szMessage[50] = "";[/color]
                        >
                        > Here you're into buffer overflow territory; instead consider
                        > std::string.
                        >
                        >
                        >[color=green]
                        > > int WINAPI WinMain(HINSTAN CE hInstance,
                        > > HINSTANCE hPrevInstance,
                        > > LPSTR lpCmdLine,
                        > > int nCmdShow)
                        > > {[/color]
                        >
                        > Check out your compiler's support for standard 'main'. g++ supports
                        > that directly for Windows GUI application. Use standard 'main'.
                        >
                        >[color=green]
                        > > WNDCLASS wc;
                        > > wc.lpszClassNam e = "Style1";
                        > > wc.lpfnWndProc = MainWndProc;
                        > > wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
                        > > wc.hInstance = hInstance;
                        > > wc.hIcon = LoadIcon(NULL,I DI_APPLICATION) ;
                        > > wc.hCursor = LoadCursor(NULL ,IDC_ARROW);
                        > > wc.hbrBackgroun d = (HBRUSH)( COLOR_WINDOW+1 );
                        > > wc.lpszMenuName = "";
                        > > wc.cbClsExtra = 0;
                        > > wc.cbWndExtra = 0;
                        > > RegisterClass(& wc);[/color]
                        >
                        > A simpler way might be to use an initializer, "WNDCLASS wc = { ... };".
                        >
                        > Anyway, to ensure all non-assigned fields are zero, declare it like
                        >
                        >
                        > WNDCLASS wc = {0};
                        >
                        >
                        >
                        >[color=green]
                        > > hWnd = CreateWindow("S tyle1","Border Demonstration",
                        > > WS_OVERLAPPEDWI NDOW,CW_USEDEFA ULT,CW_USEDEFAU LT,
                        > > 185,265,NULL,NU LL,hInstance,NU LL);
                        > >
                        > > hwndEdit = CreateWindow("E DIT",NULL,
                        > > WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
                        > > 10,10,155,20,hW nd,NULL,hInstan ce,NULL);
                        > >
                        > > hwndButton = CreateWindow("B UTTON","Message ",
                        > > WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTO N,
                        > > 10,40,35,35,hWn d,NULL,hInstanc e,NULL);
                        > >
                        > > hwndClearButton = CreateWindow("B UTTON","Clear",
                        > > WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTO N,
                        > > 50,40,35,35,hWn d,NULL,hInstanc e,NULL);[/color]
                        >
                        > Error checking might be a good idea... For example using a function
                        >
                        > template< typename T >
                        > inline void throwIf0( T const& x )
                        > {
                        > if( !x ) { throw std::runtime_er ror( "Ooops." ); }
                        > }
                        >
                        > Then
                        >
                        > hWnd = CreateWindow("S tyle1","Border Demonstration",
                        > WS_OVERLAPPEDWI NDOW,CW_USEDEFA ULT,CW_USEDEFAU LT,
                        > 185,265,NULL,NU LL,hInstance,NU LL);
                        > throwIf0( hWnd );
                        >
                        > of course with a 'catch' somewhere.
                        >
                        >[color=green]
                        > > ShowWindow(hWnd ,nCmdShow);
                        > >
                        > > MSG msg;
                        > > while(GetMessag e(&msg,NULL,0,0 ))
                        > > {
                        > > TranslateMessag e(&msg);
                        > > DispatchMessage (&msg);
                        > > }
                        > > return msg.wParam;[/color]
                        >
                        > Here should be cast to the return type, i.e.
                        >
                        >
                        > return static_cast<int >( msg.wParam );
                        >
                        >[color=green]
                        > > }//end WinMain()[/color]
                        >
                        >
                        >
                        >[color=green]
                        > > long WINAPI MainWndProc(HWN D hWnd,UINT msg,WPARAM wParam,LPARAM
                        > > lParam)
                        > > {
                        > > HWND hwndCtl = (HWND)lParam;[/color]
                        >
                        > Don't use C style casts.
                        >
                        > In this case, use a reinterpret_cas t or the appropriate system-specific
                        > macro/function (off-topic hint: <windowsx.h>) .
                        >
                        > Anyway the logic is flawed: you don't yet have enough information to
                        > say that this argument represents what you think it does. But it works.
                        > With a bit of maintainance it will cease to work.
                        >
                        >[color=green]
                        > > switch(msg)
                        > > {
                        > > case WM_COMMAND:
                        > > switch(wParam)
                        > > {
                        > > case BN_CLICKED:
                        > > if(hwndCtl == hwndButton)
                        > > {
                        > > strcpy(szMessag e,"This program is an overlapped style Window.");[/color]
                        >
                        > Buffer overflow?
                        >
                        > Indentation!
                        >
                        >[color=green]
                        > > SetWindowText(h wndEdit,szMessa ge);
                        > > }
                        > > else if(hwndCtl == hwndClearButton )
                        > > {
                        > > strcpy(szMessag e,"");
                        > > SetWindowText(h wndEdit,szMessa ge);
                        > > }
                        > > }
                        > > break;
                        > > case WM_DESTROY:
                        > > PostQuitMessage (0);
                        > > return 0;
                        > > default:
                        > > return DefWindowProc(h Wnd,msg,wParam, lParam);
                        > > }
                        > >
                        > > return 0;[/color]
                        >
                        > Unreachable.
                        >[color=green]
                        > > }//end MainWndProc()[/color][/color]

                        hello alf,

                        thank you for your substantive and even-tempered contribution. the
                        problem appears to have been resolved prior to any replies. i am also
                        reviewing the specific suggestions you've presented (code-related) for
                        subsequent use. very relevant.

                        regards

                        Comment

                        • August1

                          #27
                          Re: Windows App - C++

                          > Attempt to be gracious noted!
                          you have certainly misunderstood and presumed many things if you think
                          that i'm attempting to be gracious.

                          Comment

                          • John Harrison

                            #28
                            Re: Windows App - C++


                            "August1" <anthonyb_nc@ho tmail.com> wrote in message
                            news:fce2a102.0 410200048.63c39 cf4@posting.goo gle.com...[color=blue][color=green]
                            > > Attempt to be gracious noted![/color]
                            > you have certainly misunderstood and presumed many things if you think
                            > that i'm attempting to be gracious.[/color]

                            I'm curious what will you do the next time you get a Windows programming
                            problem? Will you post it here or will you try the other newsgroup I
                            suggested? Or will you take it somewhere completely different?

                            I must agree, I didn't think your last post demonstrated any graciousness
                            either. Phlip was being most presumptuous. My presumption is that you are
                            one of those people whose vanity or arrogance prevents you from ever
                            admitting that you've made a mistake.

                            john


                            Comment

                            • Randy Yates

                              #29
                              Re: Windows App - C++

                              "John Harrison" <john_andronicu s@hotmail.com> writes:
                              [color=blue]
                              > "August1" <anthonyb_nc@ho tmail.com> wrote in message
                              > news:fce2a102.0 410200048.63c39 cf4@posting.goo gle.com...[color=green][color=darkred]
                              >> > Attempt to be gracious noted![/color]
                              >> you have certainly misunderstood and presumed many things if you think
                              >> that i'm attempting to be gracious.[/color]
                              >
                              > I'm curious what will you do the next time you get a Windows programming
                              > problem? Will you post it here or will you try the other newsgroup I
                              > suggested? Or will you take it somewhere completely different?
                              >
                              > I must agree, I didn't think your last post demonstrated any graciousness
                              > either. Phlip was being most presumptuous. My presumption is that you are
                              > one of those people whose vanity or arrogance prevents you from ever
                              > admitting that you've made a mistake.
                              >
                              > john[/color]

                              To be brutally honest, John, I think he (or she) is just stupid. No use
                              reasoning with a moron.
                              --
                              % Randy Yates % "My Shangri-la has gone away, fading like
                              %% Fuquay-Varina, NC % the Beatles on 'Hey Jude'"
                              %%% 919-577-9882 %
                              %%%% <yates@ieee.org > % 'Shangri-La', *A New World Record*, ELO

                              Comment

                              • Raymond Martineau

                                #30
                                Re: Windows App - C++

                                On Wed, 20 Oct 2004 00:13:59 GMT, alfps@start.no (Alf P. Steinbach) wrote:
                                [color=blue][color=green]
                                >> long WINAPI MainWndProc(HWN D,UINT,WPARAM,L PARAM);
                                >>
                                >> HWND hWnd;[/color]
                                >
                                >Don't use global variables.
                                >[/color]

                                You might want to suggest an alternative, such as recommending either a
                                vector for storing HWNDs, a singleton class, or a function that returns a
                                constant reference to the variable. Alternativly, you could recommend that
                                the variable be renamed (as it conflicts or is confused with standard
                                naming conventions with these callback functions) and placed within it's
                                minimal scope.

                                There might be cases where variables actually need to be global. For
                                example, copy protection module might want to give direct global read
                                access to a set of variables containing a registration key to make it
                                slightly harder for crackers to disable a centralized copy protection
                                function. However, this does have a disadvantage of both redundant code and
                                bugs that appear in the remote functions, but the decision is up to the
                                designer.
                                [color=blue][color=green]
                                >> WNDCLASS wc;
                                >> wc.lpszClassNam e = "Style1";
                                >> wc.lpfnWndProc = MainWndProc;
                                >> wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
                                >> wc.hInstance = hInstance;
                                >> wc.hIcon = LoadIcon(NULL,I DI_APPLICATION) ;
                                >> wc.hCursor = LoadCursor(NULL ,IDC_ARROW);
                                >> wc.hbrBackgroun d = (HBRUSH)( COLOR_WINDOW+1 );
                                >> wc.lpszMenuName = "";
                                >> wc.cbClsExtra = 0;
                                >> wc.cbWndExtra = 0;
                                >> RegisterClass(& wc);[/color]
                                >
                                >A simpler way might be to use an initializer, "WNDCLASS wc = { ... };".[/color]

                                This requires actually looking at the header file to see which order the
                                values are assigned. I've seen structures like this before - it's
                                generally more readable when assigning each member by name, and less prone
                                to transposition errors. (e.g. switching around fields lpszMenuName and
                                lpszClassName).
                                [color=blue]
                                >
                                >Anyway, to ensure all non-assigned fields are zero, declare it like
                                >
                                >
                                > WNDCLASS wc = {0};[/color]

                                While you could use that method of using a "clean" structure, it's better
                                to assign the fields individually to see if "you missed something". While
                                this not normally be critical, a bug involving core windows messages or
                                window classes can potentially mess up the system.

                                It may be a bit better to instead call a function that fills in suitable
                                default values for the Window Class (with parameters to address the most
                                common changes), and to make changes as necessairy.

                                Comment

                                Working...