Thread: Too much Cpu usage

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Too much Cpu usage

    I cant seem to find why it uses 50% of the CPU.
    Apparently im not getting the handle of the Win_1 because its writing over the window and not in it.

    Thanks for any suggestion!

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <string>
    #include <iostream>
    #include <richedit.h>
    #pragma comment(lib,"user32.lib")
    #pragma comment(lib,"Gdi32.lib")
    using namespace std;
    
    #define IDB_TEXT2 101
    #define IDB_TEXT3 102
    #define GetF 103
    
    HWND hwnd, Win_1, Win_2, Send;
    MSG Msg;
    HDC hdc;
    PAINTSTRUCT ps;
    
    TCHAR *buf;
    int length;
    size_t found ;
    string Text, str;
    
    void GetIt()
    {
         hdc = BeginPaint(Win_1, &ps);
         length = GetWindowTextLength(Win_2);
         buf = new TCHAR[length+1];
         GetWindowText(GetDlgItem(hwnd,IDB_TEXT3), buf, length+1);
         str   = buf;
         found = str.find("\r\n");
         Text  = str.substr(0,found);
         TextOut(hdc, 10, 10, Text.c_str(), strlen(Text.c_str()));
         EndPaint(Win_1, &ps);
         delete[] buf;
    }
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
      switch(msg){
                  case WM_CREATE:
                       Win_1 = CreateWindowEx(0,"Edit","",
                             WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                             ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                             20, 20, 550, 460,hwnd,(HMENU) IDB_TEXT2,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                       Win_2 = CreateWindowEx(0,"Edit","hello",
                             WS_VISIBLE | WS_CHILD |  WS_HSCROLL | WS_VSCROLL |
                             ES_AUTOHSCROLL | ES_WANTRETURN | ES_MULTILINE,
                             20, 500, 550, 180,hwnd,(HMENU) IDB_TEXT3,
                             ((LPCREATESTRUCT)lParam)->hInstance,NULL);
                       Send = CreateWindowEx(0,"Button","Send",
                             WS_CHILD | WS_VISIBLE | WS_BORDER,
                             20, 700, 550, 20,hwnd,(HMENU)GetF,0,NULL);
    
                       EnableWindow(Send ,TRUE);
                       break;
                  case WM_PAINT:
                       GetIt();
                       break;
                  case WM_COMMAND:
                       switch(wParam)
                       {
                        case GetF:
                             InvalidateRect(hwnd,0,FALSE);
                             break;
                       }
                  case WM_TIMER:
                       break;
    
                  case WM_CLOSE:
                       DestroyWindow(hwnd);
                       break;
    
                  case WM_DESTROY:
                       PostQuitMessage(0);
                       break;
                  default:
                       return DefWindowProc(hwnd,msg,wParam,lParam);
           }
           return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
        WNDCLASSEX wc;
        wc.hInstance = hInstance;
        wc.cbClsExtra = 0;
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.cbWndExtra = 0;
        wc.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(100,100,100));
        wc.hCursor = LoadCursor(NULL,IDC_ARROW);
        wc.hIcon   = LoadIcon (NULL, IDI_APPLICATION) ;
        wc.lpfnWndProc =  WndProc;
        wc.lpszClassName = "Class";
        wc.lpszMenuName = NULL;
        wc.style = 0;
        wc.hIconSm = NULL;
        if(!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
            return 0;
        }
    
        hwnd = CreateWindowEx(WS_EX_LEFT|WS_EX_LTRREADING|WS_EX_WINDOWEDGE,
                               "Class",
                               " The_Chatter",
                               WS_OVERLAPPEDWINDOW,
                               CW_USEDEFAULT, CW_USEDEFAULT, 600, 800,
                               HWND_DESKTOP, NULL, hInstance, NULL);
    
        if(hwnd == NULL)
        {
            MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK); return 0;
        }
    
        SetTimer(hwnd,1,125,NULL);
        ShowWindow(hwnd, nCmdShow);
        UpdateWindow(hwnd);
    
        while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
              TranslateMessage(&Msg);
              DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }
    Last edited by Ducky; 10-25-2009 at 05:21 AM.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reduce CPU usage
    By patrick22 in forum Windows Programming
    Replies: 9
    Last Post: 07-10-2009, 02:13 PM
  2. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  3. Net cpu usage of pthreads?!
    By mynickmynick in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2008, 07:59 AM
  4. Calculating CPU Usage
    By vitaliy in forum Linux Programming
    Replies: 3
    Last Post: 08-21-2005, 09:38 AM
  5. CPU Usage so high
    By X PaYnE X in forum Windows Programming
    Replies: 9
    Last Post: 12-21-2003, 03:07 AM