Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

windowTools.cpp

Go to the documentation of this file.
00001 /* *************************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2003-2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016         http://www.gnu.org/copyleft/gpl.html
00017 ************************************************************************************* */
00018 
00019 #include <windows.h>
00020 #include <windowsx.h>
00021 #include <commctrl.h>
00022 
00023 #include "windowTools.h"
00024 
00025 // donne la taille de la fenetre client
00026 BOOL GetWindowSize(HWND hWnd,int *dx,int *dy)
00027 {
00028         RECT rect;
00029         if (!GetClientRect(hWnd,&rect)) return FALSE;
00030         *dx = rect.right-rect.left;
00031         *dy = rect.bottom-rect.top;
00032         return TRUE;
00033 }
00034 
00035 // configure la taille d'une fenetre
00036 BOOL SetWindowSize(HWND hWndControl,int dx,int dy)
00037 {
00038         return SetWindowPos(hWndControl,NULL,0,0,dx,dy,SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOSENDCHANGING);
00039 }
00040 
00041 // donne la postion de la fenetre
00042 BOOL GetWindowPos(HWND hWnd,int *x,int *y)
00043 {
00044         RECT rect;
00045         GetWindowRect(hWnd,&rect);
00046         POINT pt;
00047         pt.x = rect.left;
00048         pt.y = rect.top;
00049         ScreenToClient(GetParent(hWnd),&pt);
00050         *x=pt.x;
00051         *y=pt.y;
00052         return TRUE;
00053 }
00054 
00055 // ajout ou retir un style d'une fenetre cf.: voir CreateWindowEx
00056 LONG SetWindowStyle(HWND hWnd,int set,LONG dwStyle)
00057 {
00058         LONG result = GetWindowLong(hWnd,GWL_STYLE);
00059         result |=dwStyle;
00060         if (!set) result-=dwStyle;
00061         return  SetWindowLong(hWnd,GWL_STYLE,result);
00062 }
00063 
00064 // place en fullscreen
00065 void SetFullScreen(HWND hWnd,BOOL isFull,BOOL isTopMost)
00066 {
00067         HWND hWndConfig = isTopMost? HWND_TOPMOST : NULL;
00068 
00069         static int dxOld,dyOld,xOld,yOld;
00070         static BOOL oldIsFull=FALSE;
00071         if (isFull)
00072         {
00073                 if (!oldIsFull)
00074                 {
00075                         GetWindowSize(hWnd,&dxOld,&dyOld);
00076                         GetWindowPos(hWnd,&xOld,&yOld);
00077                 }
00078                 oldIsFull=TRUE;
00079                 int dx = GetSystemMetrics(SM_CXSCREEN);
00080                 int dy = GetSystemMetrics(SM_CYSCREEN);
00081                 SetWindowStyle(hWnd,FALSE,WS_BORDER|WS_CAPTION|WS_DLGFRAME|WS_SIZEBOX );
00082                 SetWindowPos(hWnd,NULL,0,0,dx,dy,SWP_SHOWWINDOW); 
00083                 SetWindowPos(hWnd,hWndConfig,0,0,dx,dy,SWP_SHOWWINDOW); // topMost
00084                 SetActiveWindow(hWnd);
00085         }
00086         else 
00087         {
00088                 oldIsFull=FALSE;
00089                 SetWindowStyle(hWnd,TRUE,WS_BORDER|WS_CAPTION|WS_DLGFRAME|WS_SIZEBOX);
00090                 SetWindowPos(hWnd,hWndConfig, xOld, yOld, dxOld, dyOld, 0); // topMost
00091                 SetActiveWindow(hWnd);
00092         }
00093 }
00094 
00095 // place en fenetre un titre ou non
00096 void SetWindowTitle(HWND hWnd,char *title,BOOL enableTitle)
00097 {
00098         SetWindowStyle(hWnd,enableTitle,WS_TILEDWINDOW);
00099         SetWindowText(hWnd,title);                      
00100 }
00101 
00102 // simule un appuis de bouton
00103 void PushButton(HWND hWnd,int idControl)
00104 {
00105         SendMessage(hWnd,WM_COMMAND,idControl,0);
00106 }
00107 
00108 // simule un appuis sur un boutton check box
00109 BOOL ToggleCheck(HWND hWnd,int idControl)
00110 {
00111         HWND hWndCtrl = GetDlgItem(hWnd,idControl);
00112         BOOL state = !Button_GetCheck(hWndCtrl);
00113         Button_SetCheck(hWndCtrl,state);
00114         PushButton(hWnd,idControl);
00115         return state;
00116 }
00117 
00118 // affiche le curseur de la souris (version parallele qui peut declanché un stack overflow si trop d'interation)
00119 /*
00120 void ShowMouse(BOOL active)
00121 {
00122         int num=ShowCursor(active);
00123         if (num<0&&active) ShowMouse(active);
00124         if (num>=0&&!active) ShowMouse(active);
00125 }
00126 */
00130 void ShowMouse(BOOL active)
00131 {
00132         if (active) while (ShowCursor(TRUE)<0);
00133         else while (ShowCursor(FALSE)>=0);
00134 }
00135 
00136 // calcul le delta de taille de fenetre
00137 BOOL GetDeltaWindow(HWND hWndParent,RECT *oldRect,int *deltaX,int *deltaY)
00138 {
00139         RECT newRect;
00140 
00141         GetWindowRect(hWndParent,&newRect);
00142         *deltaX = (newRect.right-newRect.left)-(oldRect->right-oldRect->left);
00143         *deltaY = (newRect.bottom-newRect.top)-(oldRect->bottom-oldRect->top);
00144 
00145         CopyMemory(oldRect,&newRect,sizeof(RECT));
00146         return TRUE;
00147 }
00148 
00149 // configure la taille d'une fenetre par rapport au delta
00150 BOOL SetDeltaWindowSize(HWND hWndControl,int deltaX,int deltaY)
00151 {
00152         RECT rect;
00153 
00154         GetWindowRect(hWndControl,&rect);
00155         int dX = rect.right-rect.left;
00156         int dY= rect.bottom-rect.top;
00157 
00158         SetWindowPos(hWndControl,NULL,0,0,dX+deltaX,dY+deltaY,SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOSENDCHANGING);
00159         return TRUE;
00160 }
00161 
00162 // configure la position d'une fenetre par rapport au delta
00163 BOOL SetDeltaWindowPos(HWND hWndControl,int deltaX,int deltaY)
00164 {
00165         POINT point;
00166         RECT rect;
00167         GetWindowRect(hWndControl,&rect);
00168         point.x=rect.left;
00169         point.y=rect.top;
00170         HWND hWndParent = GetParent(hWndControl);
00171         ScreenToClient(hWndParent,&point);
00172         SetWindowPos(hWndControl,NULL,point.x+deltaX,point.y+deltaY,0,0,SWP_NOSIZE);//|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOSENDCHANGING);     
00173         return TRUE;
00174 }
00175 
00176 // expande/collaps recursivement une arborecence d'un arbre
00177 void TreeView_ExpandAll (HWND hTree,BOOL expand,HTREEITEM rootTree)
00178 {
00179         if (!rootTree) rootTree = TreeView_GetRoot(hTree);
00180 
00181         while (rootTree)
00182         {
00183                 TreeView_Expand(hTree,rootTree,expand? TVE_EXPAND : TVE_COLLAPSE);
00184                 HTREEITEM childTree = TreeView_GetNextItem(hTree,rootTree,TVGN_CHILD);
00185                 if (childTree) TreeView_ExpandAll(hTree,expand,childTree);
00186                 rootTree = TreeView_GetNextSibling(hTree,rootTree);
00187         } 
00188 }
00189 
00190 // expande/collaps jusqu'a un certain niveau d'arborecence d'un arbre
00191 void TreeView_ExpandAll_Level (HWND hTree,BOOL expand,int levelMax,HTREEITEM rootTree)
00192 {
00193         static int level=0;
00194         if (!rootTree) rootTree = TreeView_GetRoot(hTree);
00195 
00196         level++;
00197         if (level<levelMax)
00198         {
00199                 while (rootTree)
00200                 {
00201                         TreeView_Expand(hTree,rootTree,expand? TVE_EXPAND : TVE_COLLAPSE);
00202                         HTREEITEM childTree = TreeView_GetNextItem(hTree,rootTree,TVGN_CHILD);
00203                         if (childTree) TreeView_ExpandAll_Level (hTree,expand,levelMax,childTree);
00204                         rootTree = TreeView_GetNextSibling(hTree,rootTree);
00205                 } 
00206         }
00207         level--;
00208 }
00209 
00210 
00211 /* --------------------------------------------------------------------------------------------------- 
00212 Déplace une fenetre dans le but de la placer dans le coin le plus proche des icon de tache
00213 --------------------------------------------------------------------------------------------------- */
00214 
00215 void MoveToBorderTask (HWND hUpdatingWnd)
00216 {
00217         if (!hUpdatingWnd) return ;
00218 
00219         APPBARDATA Data ; 
00220         memset (&Data, 0, sizeof(Data)) ;
00221         Data.hWnd = hUpdatingWnd ;
00222         SHAppBarMessage( ABM_GETTASKBARPOS , &Data) ;
00223         RECT r ;
00224         GetWindowRect (hUpdatingWnd , &r) ;
00225 
00226         int cx = GetSystemMetrics(SM_CXSCREEN);
00227         int cy = GetSystemMetrics(SM_CYSCREEN);
00228 
00229         if (Data.rc.top < 0 && Data.rc.left < 0 && Data.rc.right > cx) 
00230         { // Barre de taches en haut
00231                 MoveWindow (hUpdatingWnd, 
00232                         cx - (r.right - r.left), 
00233                         Data.rc.bottom - Data.rc.top, 
00234                         r.right - r.left, 
00235                         r.bottom - r.top, 
00236                         FALSE); 
00237         }
00238         else if (Data.rc.bottom > cy && Data.rc.left < 0 && Data.rc.right > cx) 
00239         { // Barre de taches en bas
00240                 MoveWindow (hUpdatingWnd, 
00241                         cx - (r.right - r.left), 
00242                         cy - (Data.rc.bottom-Data.rc.top)-(r.bottom - r.top), 
00243                         r.right - r.left, 
00244                         r.bottom - r.top, 
00245                         FALSE); 
00246         } 
00247         else if (Data.rc.top < 0 && Data.rc.left < 0 && Data.rc.bottom > cy) 
00248         { // Barre de taches à gauche
00249                 MoveWindow (hUpdatingWnd, 
00250                         (Data.rc.right-Data.rc.left), 
00251                         cy - (r.bottom - r.top), 
00252                         r.right - r.left, 
00253                         r.bottom - r.top, 
00254                         FALSE); 
00255         }
00256         else if (Data.rc.top < 0 && Data.rc.bottom > cy && Data.rc.right > cx) 
00257         { // Barre de taches à droite
00258                 MoveWindow (hUpdatingWnd, 
00259                         cx - (Data.rc.right-Data.rc.left)- (r.right - r.left), 
00260                         cy -(r.bottom - r.top), 
00261                         r.right - r.left, 
00262                         r.bottom - r.top, 
00263                         FALSE); 
00264         }
00265         else   
00266                 MoveWindow (hUpdatingWnd, 
00267                 cx - (r.right - r.left), 
00268                 cy - (Data.rc.bottom-Data.rc.top)-(r.bottom - r.top), 
00269                 r.right - r.left, 
00270                 r.bottom - r.top, 
00271                 FALSE); 
00272 }     

Generated on Fri Aug 20 19:19:52 2004 for 3d Controls by doxygen 1.3.6