Thread: Very basic, but not STATIC controls.

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Very basic, but not STATIC controls.

    According to the MSDN there are only a few controls BUTTON, COMBOBOX, EDIT, LISTBOX, MDICLIENT, RichEdit, RICHEDIT_CLASS, SCROLLBAR and STATIC.

    I need to create a control exactly like STATIC (just a gray box) except it needs to be able to accept inputs from mouse and keyboards like other controls. I want to then sub class it with SetWindowLong(GWL_WNDPROC) so i can use the WM_PAINT event to draw stuff.
    Is there a way to make STATIC accept input?

    BTW: If you ever used the resource editor in visual studio, there is a control called a Label that is alot like STATIC except that it does accept input from the mouse and keyboard. I want to create "Label" but programmatically instead of through the resource editor.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It does accept input, it just doesn't handle it (you could of course handle it in your own subclass procedure, but that's kinda... difficult). By accepting input you mean being able to select text and copy it? If yes, then you can use a borderless read-only edit box.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Nope it doesnt seem to receive WM_LBUTTONDOWN and WM_RBUTTONDOWN events and probably others. I create the window like this:

    Code:
    CreateWindowEx(0,
                            "STATIC",
                            "Test",
                            WS_CHILD | WS_VSCROLL | WS_HSCROLL,
                            10,
                            10,
                            500,
                            400,
                            getHandle(),
                            NULL,
                            GetModuleHandle(NULL),
                            NULL)
    and sub class it like this:

    Code:
    SetWindowLongPtr(handle, GWLP_USERDATA, (LONG_PTR)this);
    SetWindowLong(handle, GWL_WNDPROC, (LONG)winProc);
    and the sub classed window procedure looks like this:

    Code:
    LRESULT CALLBACK CControlClass::winProc(HWND hWnd, unsigned int message, WPARAM wParam, LPARAM lParam)
    {
      CControlClass* control = (CControlClass*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
    
      if (control)
      {
        TEvent event;
        CMessageHandler* msgHandle = NULL;
        memset(&event, 0, sizeof(event));
        switch (message)
        {
          ...
          case WM_LBUTTONDOWN:
          case WM_RBUTTONDOWN:
            if(control->onMouseClickHandle.isSet())
            {
              event.onMouseClick.button = wParam;
              event.onMouseClick.mouseX = LOWORD(lParam);
              event.onMouseClick.mouseY = HIWORD(lParam);
              msgHandle = &control->onMouseClickHandle;
            }
          break;
    
          ...
        }
    
    
        if (msgHandle)
        {
          if (msgHandle->call(event))
            return 0;
        }
    
        return CallWindowProc(control->defaultProc, hWnd, message, wParam, lParam);
      }
      return 1;
    }
    And the event handling looks like this:

    Code:
    #define MessageHandler(a, b) CMessageHandler((int (CControlClass::*)(const TEvent&))(a), b)
    class CMyWindow:
        public CControlClass
    {
        CControlClass canvas;
    public:
        CMyWindow(HWND hWnd): CControlClass(hWnd)
        {
            //Create a canvas
            canvas.create(CreateWindowEx(
                            0,
                            "STATIC",
                            "Test",
                            WS_CHILD | WS_VSCROLL | WS_HSCROLL,
                            10,
                            10,
                            500,
                            400,
                            getHandle(),
                            NULL,
                            GetModuleHandle(NULL),
                            NULL));
    
            canvas.onMouseClickHandle = MessageHandler(&CMyWindow::canvas_onMouseClick, this);
            canvas.show(SW_SHOWNORMAL);
    
    
            this->onCloseHandle = MessageHandler(&CMyWindow::onClose, this);
            this->onDestroyHandle = MessageHandler(&CMyWindow::onDestroy, this);
            this->show(SW_SHOWNORMAL);
    
        }
    
        int canvas_onMouseClick(const TEvent& event)
        {
            printf("canvas Mouse clicked\n");
            return 0;
        }
    
        int onClose(const TEvent& event)
        {
            destroy();
            printf("Destroyed\n");
            return 0;
        }
    
        int onDestroy(const TEvent& event)
        {
            PostQuitMessage(0);
            return 0;
        }
    };
    If i change the control to "EDIT" the mouse click event works (but looks and behaves like an edit box). If i set it to STATIC it will not work.
    Last edited by 39ster; 02-20-2008 at 06:52 AM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Hmm, i've got no idea what to do with that link. I click it and something about subscribing to a newsgroup comes up but theres nothing to do on it.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by 39ster View Post
    Hmm, i've got no idea what to do with that link. I click it and something about subscribing to a newsgroup comes up but theres nothing to do on it.
    ????
    Can't you use OE or any newsreader ?!
    or just use google groups ?!
    All answers are on newsgroups... for nearly 20 years ! (after BBS)

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Hmm i got it to half work. I set the style SS_NOTIFY. It accepts mouse input but the scroll bars seem to be disabled when i have the style WS_VSCROLL | WS_HSCROLL set.

    Code:
    CreateWindowEx(WS_EX_CLIENTEDGE,
                            WC_STATIC,
                            "",
                            WS_CHILD | WS_VSCROLL | WS_HSCROLL | SS_NOTIFY,
                            206,
                            55,
                            570,
                            470,
                            getHandle(),
                            NULL,
                            CApplication::getHandle(),
                            NULL)
    Last edited by 39ster; 02-21-2008 at 06:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Few Questions (Styles, Static Control)
    By Zeusbwr in forum Windows Programming
    Replies: 11
    Last Post: 04-15-2005, 04:13 AM
  3. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  4. how to set the background colour of a static box?
    By Cobras2 in forum Windows Programming
    Replies: 2
    Last Post: 08-01-2002, 04:57 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM