View Full Version: moving a STATIC as scrollbar

C++ Learning Community > win32 api C++ programming > moving a STATIC as scrollbar


Title: moving a STATIC as scrollbar


hehzz - May 9, 2005 04:59 PM (GMT)
I'm trying to make a scrollbar with a STATIC (cuz AFAIK scrollbar can't be ownerdrawn, but i need it graphical). So i'm getting WM_MOUSEMOVE messages and moving the STATIC up or down, but as far as i have made it it doesn't work :wacko:

the code:
CODE

#include <windows.h>
#include "resource.h"
#include <iostream>


using namespace std;

LRESULT CALLBACK MainDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam);

bool moving = false;
int oy = 0;

int WINAPI WinMain (HINSTANCE hThisInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpszArgument,
                   int nFunsterStil)
{
   DialogBox(hThisInstance, MAKEINTRESOURCE(IDD_DLGMAIN),
           NULL, reinterpret_cast<DLGPROC>(MainDlgProc));
   return 0;
}

LRESULT CALLBACK MainDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
       if (Msg == WM_INITDIALOG)
       {
               HFONT hFont = CreateFont(24, 20, 0, 0,
                                FW_THIN, FALSE, FALSE, FALSE,
                                ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                                CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                                FF_DONTCARE, "Georgia");      
               SendDlgItemMessage(hWndDlg, IDE_NUMBER, WM_SETFONT, (WPARAM)hFont, (LPARAM)MAKELPARAM(true, 0));

               return true;
       }
       if (Msg == WM_LBUTTONDOWN)
       {
                   WINDOWPLACEMENT winplace, scrplace;
                   winplace.length = sizeof(winplace);
                   GetWindowPlacement(GetDlgItem(hWndDlg, IDE_NUMBER), &winplace);                    
                   RECT wplace = winplace.rcNormalPosition;

                   int x = LOWORD(lParam);
                   int y = HIWORD(lParam);

                   if (x >= wplace.left &&
                       y >= wplace.top &&
                       x <= wplace.right &&
                       y <= wplace.bottom)
                   {
                       oy = y - wplace.top;
                       moving = true;
                   }
       }
       if (Msg == WM_LBUTTONUP)
       {
               moving = false;
       }
       if (Msg == WM_MOUSEMOVE)
       {
               if (moving)
               {
                   if (wParam == MK_LBUTTON)
                   {
                       WINDOWPLACEMENT winplace, scrplace;
                       winplace.length = sizeof(winplace);
                       GetWindowPlacement(GetDlgItem(hWndDlg, IDE_NUMBER), &winplace);                    
                       RECT wplace = winplace.rcNormalPosition;

                       int x = LOWORD(lParam);
                       int y = HIWORD(lParam);

                       if (x >= wplace.left &&
                           y >= wplace.top &&
                           x <= wplace.right &&
                           y <= wplace.bottom)
                       {

                           cout  << "top: " << wplace.top << endl
                                 << "y: " << y << endl
                                 << "oy: " << oy << endl;
                            MoveWindow(GetDlgItem(hWndDlg, IDE_NUMBER),
                                       wplace.left,
                                       (wplace.top - (oy - (y - wplace.top))),
                                       wplace.right - wplace.left,
                                       wplace.bottom - wplace.top, true);
                            oy = y - wplace.top;
                       }
                   }
               }
               return true;
       }
       return false;
}


It seems that it receives too much WM_MOUSEMOVE messages (even if no mouse is moved)..
Ridiculous is that if i change line:

CODE

                                       (wplace.top - (oy - (y - wplace.top))),

to
CODE

                                       (wplace.top + (oy - (y - wplace.top))),


to make it work reverse, it works as it have to work!
what's the problem??? :o

hehzz - May 9, 2005 06:40 PM (GMT)
yay i solved the problem...
CODE

#include <windows.h>
#include "resource.h"
#include <iostream>

using namespace std;

LRESULT CALLBACK MainDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam);

bool moving = false;
int oy = 0;

int WINAPI WinMain (HINSTANCE hThisInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpszArgument,
                   int nFunsterStil)
{
   DialogBox(hThisInstance, MAKEINTRESOURCE(IDD_DLGMAIN),
           NULL, reinterpret_cast<DLGPROC>(MainDlgProc));
   return 0;
}

LRESULT CALLBACK MainDlgProc(HWND hWndDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
       if (Msg == WM_INITDIALOG)
       {
               HFONT hFont = CreateFont(24, 20, 0, 0,
                                FW_THIN, FALSE, FALSE, FALSE,
                                ANSI_CHARSET, OUT_DEFAULT_PRECIS,
                                CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                                FF_DONTCARE, "Georgia");      
               SendDlgItemMessage(hWndDlg, IDE_NUMBER, WM_SETFONT, (WPARAM)hFont, (LPARAM)MAKELPARAM(true, 0));

               return true;
       }
       if (Msg == WM_LBUTTONDOWN)
       {
                   WINDOWPLACEMENT winplace, scrplace;
                   winplace.length = sizeof(winplace);
                   GetWindowPlacement(GetDlgItem(hWndDlg, IDE_NUMBER), &winplace);                    
                   RECT wplace = winplace.rcNormalPosition;
                   
                   int x = LOWORD(lParam);
                   int y = HIWORD(lParam);
                   
                   if (x >= wplace.left &&
                       y >= wplace.top &&
                       x <= wplace.right &&
                       y <= wplace.bottom)
                   {
                       oy = y;
                       moving = true;
                   }
       }
       if (Msg == WM_LBUTTONUP)
       {
               moving = false;
       }
       if (Msg == WM_MOUSEMOVE)
       {
               if (moving)
               {
                   if (wParam == MK_LBUTTON)
                   {
                       WINDOWPLACEMENT winplace, scrplace;
                       winplace.length = sizeof(winplace);
                       GetWindowPlacement(GetDlgItem(hWndDlg, IDE_NUMBER), &winplace);                    
                       RECT wplace = winplace.rcNormalPosition;
                       
                       int x = LOWORD(lParam);
                       int y = HIWORD(lParam);
                       
                       if (x >= wplace.left &&
                           y >= wplace.top &&
                           x <= wplace.right &&
                           y <= wplace.bottom)
                       {
                           if (oy != (y - wplace.top))
                           {
                                MoveWindow(GetDlgItem(hWndDlg, IDE_NUMBER),
                                           wplace.left,
                                           (wplace.top - ((oy - wplace.top) - (y - wplace.top))),
                                           wplace.right - wplace.left,
                                           wplace.bottom - wplace.top, true);
                                oy = y;
                            }
                       }
                   }
               }
               return true;
       }
       return false;
}





* Hosted for free by InvisionFree