begin process at 2012 02 13 21:04:59
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Date & Heure

 > AJOUT D'UN CONTRÔLE CALENDRIER EN DYNAMIQUE SUR UNE FORM, D'UNE MANIÈRE PEUT CONVENTIONNELLE !

AJOUT D'UN CONTRÔLE CALENDRIER EN DYNAMIQUE SUR UNE FORM, D'UNE MANIÈRE PEUT CONVENTIONNELLE !


 Information sur la source

Note :
Aucune note
Catégorie :Date & Heure Classé sous :calandar, sans OCX, SysMonthCal32 Niveau :Initié Date de création :02/09/2010 Date de mise à jour :06/09/2010 14:27:18 Vu :3 020

Auteur : patrick

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

Cliquez pour voir la capture en taille normale
Tous l'intérêt de ce code est de découvrir une manière peux conventionnel pour inclure un contrôle sur une form….

Le code original est de Michel Pierron (http://www.excelabo.net/trucs/ocx_optionnel) que j’ai simplifié et complété.


Source

  • '
  • ' Source original : (Michel Pierron) http://www.excelabo.net/trucs/ocx_optionnel
  • '
  • ' Pour tester, mettre dans en bas d’une form une TextBox nommé TextBox1, et deux boutons nommés CmdSET et CmdGET.
  • '
  • '
  • Option Explicit
  • Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
  • Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  • Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
  • Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  • Private Declare Sub InitCommonControls Lib "comctl32" ()
  • ' MonthCal Messages
  • Private Const MCM_FIRST = &H1000
  • Private Const MCM_GETCURSEL = (MCM_FIRST + 1)
  • Private Const MCM_SETCURSEL = (MCM_FIRST + 2)
  • Private Const MCM_SETCOLOR = (MCM_FIRST + 10)
  • Private Const MCM_GETCOLOR = (MCM_FIRST + 11)
  • Private Const MCM_SETFIRSTDAYOFWEEK = (MCM_FIRST + 15)
  • Private Const MCM_GETFIRSTDAYOFWEEK = (MCM_FIRST + 16)
  • ' MonthCal Styles
  • Private Const MCS_MULTISELECT = &H2
  • Private Const MCS_WEEKNUMBERS = &H4
  • Private Const MCS_NOTODAYCIRCLE = &H8
  • Private Const MCS_NOTODAY = &H10
  • ' MonthCal Color
  • Private Const MCSC_BACKGROUND = 0 ' the background color (between months)
  • Private Const MCSC_TEXT = 1 ' the dates
  • Private Const MCSC_TITLEBK = 2 ' background of the title
  • Private Const MCSC_TITLETEXT = 3
  • Private Const MCSC_MONTHBK = 4 ' background within the month cal
  • Private Const MCSC_TRAILINGTEXT = 5 ' the text color of header & trailing days
  • Private Type SYSTEMTIME
  • wYear As Integer
  • wMonth As Integer
  • wDayOfWeek As Integer
  • wDay As Integer
  • wHour As Integer
  • wMinute As Integer
  • wSecond As Integer
  • wMilliseconds As Integer
  • End Type
  • Private dtHwnd As Long
  • Private Sub Form_Load()
  • InitCommonControls
  • dtHwnd = CreateWindowEx(0, "SysMonthCal32", vbNullString, &H50000000 + MCS_WEEKNUMBERS, 40, 10, 200, 200, Me.hWnd, 0&, 0&, ByVal 0&)
  • SendMessage dtHwnd, MCM_SETCOLOR, MCSC_BACKGROUND, ByVal &HE0E0E0
  • SendMessage dtHwnd, MCM_SETCOLOR, MCSC_TITLEBK, ByVal RGB(46, 210, 50)
  • End Sub
  • Private Sub CmdGET_Click()
  • Dim CurSysTime As SYSTEMTIME
  • SendMessage dtHwnd, MCM_GETCURSEL, 0&, CurSysTime
  • Me.TextBox1.Text = Format(DateSerial(CurSysTime.wYear, CurSysTime.wMonth, CurSysTime.wDay), "Short Date")
  • End Sub
  • Private Sub CmdSET_Click()
  • Dim CurSysTime As SYSTEMTIME
  • On Error Resume Next
  • CurSysTime.wYear = Year(CDate(Me.TextBox1.Text))
  • CurSysTime.wMonth = Month(CDate(Me.TextBox1.Text))
  • CurSysTime.wDay = Day(CDate(Me.TextBox1.Text))
  • On Error GoTo 0
  • SendMessage dtHwnd, MCM_SETCURSEL, 0&, CurSysTime
  • End Sub
  • Private Sub Form_QueryClose(Cancel As Integer, CloseMode As Integer)
  • DestroyWindow dtHwnd
  • End Sub
'
' Source original : (Michel Pierron) http://www.excelabo.net/trucs/ocx_optionnel
'
' Pour tester, mettre dans en bas d’une form une TextBox nommé TextBox1, et deux boutons nommés CmdSET et CmdGET.
'
'

 
 Option Explicit
 
 Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hwndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
 Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As Long) As Long
 Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
 Private Declare Sub InitCommonControls Lib "comctl32" ()

' MonthCal Messages
 Private Const MCM_FIRST = &H1000
 Private Const MCM_GETCURSEL = (MCM_FIRST + 1)
 Private Const MCM_SETCURSEL = (MCM_FIRST + 2)
 Private Const MCM_SETCOLOR = (MCM_FIRST + 10)
 Private Const MCM_GETCOLOR = (MCM_FIRST + 11)
 Private Const MCM_SETFIRSTDAYOFWEEK = (MCM_FIRST + 15)
 Private Const MCM_GETFIRSTDAYOFWEEK = (MCM_FIRST + 16)

' MonthCal Styles
 Private Const MCS_MULTISELECT = &H2
 Private Const MCS_WEEKNUMBERS = &H4
 Private Const MCS_NOTODAYCIRCLE = &H8
 Private Const MCS_NOTODAY = &H10

' MonthCal Color
 Private Const MCSC_BACKGROUND = 0   ' the background color (between months)
 Private Const MCSC_TEXT = 1         ' the dates
 Private Const MCSC_TITLEBK = 2      ' background of the title
 Private Const MCSC_TITLETEXT = 3
 Private Const MCSC_MONTHBK = 4      ' background within the month cal
 Private Const MCSC_TRAILINGTEXT = 5 ' the text color of header & trailing days
  
  Private Type SYSTEMTIME
  wYear As Integer
  wMonth As Integer
  wDayOfWeek As Integer
  wDay As Integer
  wHour As Integer
  wMinute As Integer
  wSecond As Integer
  wMilliseconds As Integer
 End Type

Private dtHwnd As Long

Private Sub Form_Load()
  InitCommonControls
  dtHwnd = CreateWindowEx(0, "SysMonthCal32", vbNullString, &H50000000 + MCS_WEEKNUMBERS, 40, 10, 200, 200, Me.hWnd, 0&, 0&, ByVal 0&)
  SendMessage dtHwnd, MCM_SETCOLOR, MCSC_BACKGROUND, ByVal &HE0E0E0
  SendMessage dtHwnd, MCM_SETCOLOR, MCSC_TITLEBK, ByVal RGB(46, 210, 50)
End Sub

Private Sub CmdGET_Click()
 Dim CurSysTime As SYSTEMTIME
  SendMessage dtHwnd, MCM_GETCURSEL, 0&, CurSysTime
  Me.TextBox1.Text = Format(DateSerial(CurSysTime.wYear, CurSysTime.wMonth, CurSysTime.wDay), "Short Date")
End Sub

Private Sub CmdSET_Click()
 Dim CurSysTime As SYSTEMTIME
  On Error Resume Next
   CurSysTime.wYear = Year(CDate(Me.TextBox1.Text))
   CurSysTime.wMonth = Month(CDate(Me.TextBox1.Text))
   CurSysTime.wDay = Day(CDate(Me.TextBox1.Text))
  On Error GoTo 0
  SendMessage dtHwnd, MCM_SETCURSEL, 0&, CurSysTime
End Sub

Private Sub Form_QueryClose(Cancel As Integer, CloseMode As Integer)
 DestroyWindow dtHwnd
End Sub



 Conclusion

Pour la récupération automatique de la date sélectionnée, on pourrait prévoir une procédure de sous classement de la fenêtre dtHwnd. Personnellement, j'utilise un simple timer et la procédure suivante :

Private Sub TimerCalendar_Timer()
  Static Dt As String
  Dim CurSysTime As SYSTEMTIME
  Dim NewDT As String
   TimerCalendar = False
     SendMessage dtHwnd, MCM_GETCURSEL, 0&, CurSysTime
     NewDT = Format(DateSerial(CurSysTime.wYear, CurSysTime.wMonth, CurSysTime.wDay), "Short Date")
     If Dt = "" Then Dt = NewDT
     If Dt <> NewDT Then
       Me.TextBox1.Text = NewDT
       Dt = NewDT
     End If
   TimerCalendar = True
End Sub


 Historique

02 septembre 2010 10:23:43 :
.
02 septembre 2010 10:25:25 :
.
06 septembre 2010 14:27:14 :
ajout de l'instruction InitCommonControls avant l'appel de la class SysMonthCal32 (Merci à Renfield)
06 septembre 2010 14:27:18 :
ajout de l'instruction InitCommonControls avant l'appel de la class SysMonthCal32 (Merci à Renfield)

 Sources du même auteur

Source avec Zip Source avec une capture WEBNTCVS : CONSULTATION D'UN RÉFÉRENCIEL NT-CVS DEPUIS UN N...
Source avec Zip Source avec une capture MINI CLIENT NOTES ECRIT EN VB5 !
Source avec Zip Source avec une capture CHECKBOX DANS UNE DBGRID
GESTION DES ERRORLEVEL EN VB
Source avec Zip TRAITEMENT SUR IMAGE : ROTATION 90°, EFFETS 3D, CONVERSION E...

 Sources de la même categorie

Source avec Zip Source avec une capture LES FONCTIONS DATE PAR L'EXEMPLE. par pasquet78
Source avec Zip Source avec une capture CALENDRIER, MODE D'EMPLOI par pasquet78
Source avec Zip Source avec une capture Source .NET (Dotnet) HORLOGE DIODE AVEC 3 ALARMES ET REMISE À L'HEURE par EhJoe
Source avec Zip Source avec une capture POINTEUSE HORAIRES PAR SEMAINE par VBNoob13
Source avec Zip Source avec une capture HORLOGE À AIGUILLES SIMPLEMENT DANS UN USERFORM EXCEL par bigbe

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture AFFICHER UN TEXTE RTF SIMPLE SANS UTILISER RICHTX32.OCX par jmc70
Source avec Zip Source avec une capture [VB6] BOUTON+FRAME OFFICE 2007 par Duke49
Source avec Zip Source avec une capture [VB6] BOUTON WINDOWS VISTA par Duke49
Source avec Zip Source .NET (Dotnet) OPENPDF.NET par bouv

Commentaires et avis

Commentaire de Renfield le 02/09/2010 12:42:47 administrateur CS

Si on regarde le code de ton Timer, la zone de texte ne se remplit pas toute seule, avant que l'on ne change la valeur du calendar...

ton TimerCalendar est inutile, deux intervalles ne survenant jamais en même temps.

en bref, je mettrai :

Private Sub TimerCalendar_Timer()
Dim CurSysTime As SYSTEMTIME
Dim NewDT As String
    SendMessage dtHwnd, MCM_GETCURSEL, 0&, CurSysTime
    NewDT = FormatDateTime(DateSerial(CurSysTime.wYear, CurSysTime.wMonth, CurSysTime.wDay), vbShortDate)
    If TextBox1 <> NewDT Then
        Me.TextBox1.Text = NewDT
    End If
End Sub



Commentaire de patrick le 02/09/2010 14:35:29

salut,

- TimerCalendar = False / True en début et fin de procédure sont des sécurités que je met systématiquement sur mais Timer.

- Mon code permet un fonctionnement dans les 2 sens : tu peux soit cliquer dans le calendrier, soit modifier la date dans la TextBox et faire SET pour fixé la date sur le calendier...

...Par contre j'ai détecté un problème : LE CODE NE FONCTIONNE QUE DANS L'ENVIRONNEMENT VB !

une fois compilé, CreateWindowEx retourne 0 et rien ne s'affiche (VB5 Pro) !

Je cherche.... mais je ne comprend pas pourquoi... (compile en natif ou p-code)

A+ Patrick

Commentaire de Renfield le 02/09/2010 15:04:20 administrateur CS

devrait fonctionner...
manque surement un InitCommonControls(Ex ?)

Commentaire de patrick le 06/09/2010 14:21:48

bien vu : il manquait un simple InitCommonControls()

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 14,227 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales