Thread: Why is my program freezing?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    40

    Why is my program freezing?

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int APIENTRY WinMain(HINSTANCE hInstance, 
                         HINSTANCE hPrevInstance, 
                         LPSTR     lpCmdLine, 
                         int       nCmdShow)
    {
        WNDCLASS WndClass;
        WndClass.style = 0;
        WndClass.cbClsExtra = 0;
        WndClass.cbWndExtra = 0;
        WndClass.lpfnWndProc = WndProc;
        WndClass.hInstance = hInstance;
        WndClass.hbrBackground = (HBRUSH) (COLOR_MENU+1);
        WndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
        WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        WndClass.lpszMenuName = 0;
        WndClass.lpszClassName = "WinProg";
        
        RegisterClass(&WndClass);
        
        HWND hWindow;
        hWindow = CreateWindow("WinProg","Test",
                               WS_OVERLAPPEDWINDOW,
                               0,0,600,500,NULL,NULL,
                               hInstance, NULL);
        ShowWindow (hWindow, nCmdShow);
        
        UpdateWindow (hWindow);
        MSG Message;
        while (GetMessage(&Message, NULL, 0, 0));
        {
            DispatchMessage(&Message);
        }
        
        return (Message.wParam);
    }
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage,
                              WPARAM wParam, LPARAM lParam)
    {
        switch(uiMessage)
        {
            case WM_PAINT:
                HDC hdc;
                PAINTSTRUCT ps;
                hdc = BeginPaint (hWnd, &ps);
                HFONT hFont;
                
                hFont = (HFONT) GetStockObject (ANSI_FIXED_FONT);
                SelectObject(hdc, hFont);
                SetTextColor (hdc, RGB(0,0,180));
                SetBkColor (hdc, RGB(190,180,200));
                SetTextAlign (hdc, TA_LEFT);
                char *string1;
                string1 = new char[200];
                lstrcpy (string1,"test test test test test test test test test test test test test test test test test test test test");
                TextOut (hdc, 10, 40, string1, lstrlen(string1));
                TextOut (hdc, 10, 65, string1, lstrlen(string1));
                        
                EndPaint (hWnd, &ps);
                break;    
            case WM_DESTROY:
                PostQuitMessage(0);
                return 0;
            default:
                return DefWindowProc (hWnd, uiMessage,
                                      wParam, lParam);           
        }
    }
    Code:
    #include <iostream.h>
    int var;
    int test();
    int main() { 
     cout << "Please input your language:\n 1. C (C,C++,C#)\n 2. VB\n 3. Other\n";
     cin >> var;
     return test(); }
    int test() {  
     if(var == 1) {
      cout << "Y0u 4r3 t3h 1337\n";
      system("PAUSE");
      return main(); }
     else if(var == 2) {
      cout << "N00B3R!\n";
      system("PAUSE");
      return main(); }
     else if(var == 3) {
      cout << "You were not thought of.\n";
      system("PAUSE");
      return main(); }
     else {      
      return 0; }}

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
        while (GetMessage(&Message, NULL, 0, 0));
        {
            DispatchMessage(&Message);
        }
    Should Be
    Code:
        while (GetMessage(&Message, NULL, 0, 0) >0);
        {
            DispatchMessage(&Message);
        }
    Woop?

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    40
    Nope, would you like to try again?
    Code:
    #include <iostream.h>
    int var;
    int test();
    int main() { 
     cout << "Please input your language:\n 1. C (C,C++,C#)\n 2. VB\n 3. Other\n";
     cin >> var;
     return test(); }
    int test() {  
     if(var == 1) {
      cout << "Y0u 4r3 t3h 1337\n";
      system("PAUSE");
      return main(); }
     else if(var == 2) {
      cout << "N00B3R!\n";
      system("PAUSE");
      return main(); }
     else if(var == 3) {
      cout << "You were not thought of.\n";
      system("PAUSE");
      return main(); }
     else {      
      return 0; }}

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Clue: What's wrong with this line?

    >>while (GetMessage(&Message, NULL, 0, 0));

    gg

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    40
    O_O
    HOLY I am sillyI am sillyI am sillyI am silly
    How did I miss that.....
    The number one programming mistake is forgetting the semi-colon.... and I add an extra....
    Code:
    #include <iostream.h>
    int var;
    int test();
    int main() { 
     cout << "Please input your language:\n 1. C (C,C++,C#)\n 2. VB\n 3. Other\n";
     cin >> var;
     return test(); }
    int test() {  
     if(var == 1) {
      cout << "Y0u 4r3 t3h 1337\n";
      system("PAUSE");
      return main(); }
     else if(var == 2) {
      cout << "N00B3R!\n";
      system("PAUSE");
      return main(); }
     else if(var == 3) {
      cout << "You were not thought of.\n";
      system("PAUSE");
      return main(); }
     else {      
      return 0; }}

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Doh i can't believe i missed that too codeplug
    Woop?

  7. #7
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Oh and btw you might want to delete your allocated memory too
    Woop?

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I might be wrong, but shouldn't you call DeleteObject() on your font after using it?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    40
    I actually already did that. I just didn't do it until after I posted this. And I changed the font to ANSI_VAR_FONT because it just looks better.
    Code:
    #include <iostream.h>
    int var;
    int test();
    int main() { 
     cout << "Please input your language:\n 1. C (C,C++,C#)\n 2. VB\n 3. Other\n";
     cin >> var;
     return test(); }
    int test() {  
     if(var == 1) {
      cout << "Y0u 4r3 t3h 1337\n";
      system("PAUSE");
      return main(); }
     else if(var == 2) {
      cout << "N00B3R!\n";
      system("PAUSE");
      return main(); }
     else if(var == 3) {
      cout << "You were not thought of.\n";
      system("PAUSE");
      return main(); }
     else {      
      return 0; }}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program freezing
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-10-2007, 05:38 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM