WM_LBUTTONDOWN

Started by
17 comments, last by Death100 17 years, 1 month ago
I know this isn't exactly a game, but I don't know any win32 specific forums, so here goes: I'm trying to make a music/movie player. I need to have some sort of slider to display where you are in the movie or music, so i'm making one, only i'm have a problem with WM_LBUTTONDOWN. My control doesn't detect it. But it does detect WM_NCLBUTTONDOWN (this is supposed to be sent when you aren't clicking on the client area of the control, right?), but only when you click on the control not in the window area. I don't know how to fix it... heres my control code:

class SLIDER {
      public:
      //functions to initialize slider
      SLIDER(bool i);
      
      //functions for callback func
      static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      
      LRESULT paint(WPARAM wParam, LPARAM lParam);
      
      private:
      //variables to control control
      HWND hwnd; //you never know when you might need it
      COLORREF bg; //the background
      COLORREF Scol; //color of the slider
      float pos; //position (percentage) of the slider
      RECT rect;
}; //classes need semi colons at the end =)

SLIDER::SLIDER(bool i) {
               if (i != false) {
                   WNDCLASSEX wc;
                     
                   //fill it in
                   wc.cbSize = sizeof(wc);
                   wc.lpszClassName = "slider";
                   wc.hInstance = GetModuleHandle(0);
                   wc.lpfnWndProc = WndProc;
                   wc.hCursor = LoadCursor(NULL, IDC_ARROW);
                   wc.hIcon = 0;
                   wc.lpszMenuName = 0;
                   wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
                   wc.style = 0;
                   wc.cbClsExtra = 0;
                   wc.cbWndExtra = sizeof(SLIDER);
                   wc.hIconSm = 0;
                   
                   //now register
                   RegisterClassEx(&wc);
               }
}

LRESULT SLIDER::paint(WPARAM wParam, LPARAM lParam) {
        HDC dc;
        PAINTSTRUCT ps;
        int x;
        
        dc = BeginPaint(hwnd, &ps);
        GetClientRect(hwnd, &rect);
        
        //drawing here
        x = rect.left + ((rect.right - rect.left) * (pos / 100));
        
        Rectangle(dc, x - 10, rect.top, x + 10, rect.bottom);
        
        EndPaint(hwnd, &ps);
        
        return 0;
}

/*-----------------------------Window Procedure------------------------------------*/
LRESULT CALLBACK SLIDER::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
                     //memory variable
                     SLIDER* memory = (SLIDER *)GetWindowLong(hwnd, 0);
                     
                     switch (message) {
                            case WM_NCCREATE:
                                 memory = new SLIDER(false);
                                 SetWindowLong(hwnd, 0, (LONG)memory);
                                 
                                 //fill in default vars
                                 memory->hwnd = hwnd;
                                 memory->bg = RGB(10, 20, 30);
                                 memory->Scol = (COLORREF)20;
                                 memory->pos = 50;
                                 
                                 return (memory != NULL);
                                 
                            case WM_PAINT:
                                 return memory->paint(wParam, lParam);
                            
                            case WM_LBUTTONUP:
                                 memory->pos += 1;
                                 InvalidateRect(hwnd, &memory->rect, false);
                                 UpdateWindow(hwnd);
                                 
                                 return 0;
                            
                            case WM_ERASEBKGND:
                                 //prevent flicker
                                 return 1;
                            
                            default:
                                    DefWindowProc(hwnd, message, wParam, lParam);
                     }
}
/*---------------------------------End----------------------------------------------*/

SLIDER slide(true);
heres the bit where i use it: /*---------------------------------- This is PlayM Movie player source Version: 0.1 */ //definitions #include "resource.h" //include standard library stuff #include <windows.h> //guess? #include <stdio.h> //file input/output //controls #include "controls.h" //include wrapper for creation of the window #include "wrapper.h" //callback func LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND slider; switch(message) { //menu case WM_COMMAND: switch (LOWORD(wParam)) { //exit from menu case menu_exit: exit(0); } //on create case WM_CREATE: //make controls slider = CreateWindowEx( WS_EX_CLIENTEDGE, "slider", "slider", WS_VISIBLE | WS_CHILD, 0, 0, 100, 100, hwnd, NULL, GetModuleHandle(0), NULL); if (slider == NULL) { MessageBox(NULL, "hello", "bs", MB_OK); } break; //resize slider case WM_SIZE: SetWindowPos(slider, NULL, 0, HIWORD(lParam) - 20, LOWORD(lParam), 20, NULL); break; //exit case WM_CLOSE: //bye exit(0); //default default: DefWindowProc(hwnd, message, wParam, lParam); } } //main func int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmd, int show) { //make window if (!win.MakeWindow("MPLAY", hInstance, 200, 200)) { return 0; } //show it win.ShowWin(show); //handle messages while(GetMessage(&win.message, NULL, 0, 0) > 0) { TranslateMessage(&win.message); DispatchMessage(&win.message); } } any help would be much appreciated. Edit: please use source tags for large portions of code - .ED [Edited by - Emmanuel Deloget on March 2, 2007 4:01:57 AM]
http://furboxes.com/forum
Advertisement
Before I attempt to answer your question, the Win32 API provides a slider control: the Trackbar control

Anyway, you control isnt handling WM_LBUTTONDOWN because you dont have a WM_LBUTTONDOWN handler in your window procedure. Instead, you have a WM_LBUTTONUP handler.
I know there is a standard control, i just wanted to learn how to make my own. it's not just WM_MOUSEBUTTONDOWN, any WM_LMOUSEBUTTONS don't work.
http://furboxes.com/forum
In the forums you can paste code by writing

source lang="cpp"]
// code here
/source]

Both source lang & /source need begging and ending braces [ ] so they work. Please don’t get offended for me telling you this. I find it easier for others to read the code when it’s like that.
k, I didn't know that.

Has no-one encountered this problem before? Is it something to do with the RECT? I really want this to work...
http://furboxes.com/forum
I would agree with Colin Jeanne that you don't appear to have all the right button handlers in there. Typically you would do something like this for a button click and release:

case WM_LBUTTONDOWN: // Left button down
{
SetCapture(hWnd);
.... do whatever you must here ....
return 0;
}

case WM_LBUTTONUP: // Left button released
{
.... do whatever you must here ....
ReleaseCapture();
return 0;
}

Read up on Windows mouse input on MSDN

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/userinput/mouseinput.asp

hth
F451
I also note that in your current posted code you change the pos member of the SLIDER class but in paint() you never do anything with that member. When you paint you are painting the exact same thing always.

Also, the call to UpdateWindow() is not necessary after InvalidateRect() as invalidating part of the window will automatically send a WM_PAINT message to the message queue.
If you build your own control and want it to receive all kind of input you need to answer the call to WM_GETDLGCODE. Simply return DLGC_WANTALLKEYS and do not pass the message on to DefWindowProc.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

The pos var is a percentage:

i convert it to coords here:

x = rect.left + ((rect.right - rect.left) * (pos / 100));


it's nothing to do with the WM_PAINT, because it does work when i use WM_NCLBUTTONDOWN to catch the mouseclicks. WM_LBUTTONDOWN doesn't even get sent (i put a MessageBox() in it and it didn't show)

I was using this tutorial: http://www.catch22.net/tuts/custctrl.asp
http://furboxes.com/forum
*bump*
http://furboxes.com/forum

This topic is closed to new replies.

Advertisement