SearchMediaUserControl.cs
Description du code
SearchMediaUserControl.cs est un fichier du projet BrolExplorer.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/brolexplorer/.
Projet BrolExplorer :
Explorateur de media en CSharp.
Code source ou contenu du fichier
Code c# (SearchMediaUserControl.cs) (687 lignes)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using be.gaudry.explorer.model; using System.IO; using be.gaudry.events; using be.gaudry.view.controls; using be.gaudry.model.enums; using be.gaudry.model.drawing; using be.gaudry.model.config; using be.gaudry.model; using be.gaudry.model.drawing.colors; using be.gaudry.model.file; using System.Drawing.Printing; using be.gaudry.view.utils; namespace be.gaudry.explorer.view.controls { public partial class SearchMediaUserControl : UserControl { #region constructor and declarations private MediaParser fileParser; /// <summary> /// used to get the "mouse over cell" for the context menu /// </summary> private DataGridViewCellEventArgs mouseLocation; public SearchMediaUserControl() { InitializeComponent(); imageList1.Images.Add(IconExtractor.ExtractIcon(Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll" ), 4).ToBitmap()); imageList1.Images.Add(IconExtractor.ExtractIcon(Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.System), "shell32.dll" ), 126).ToBitmap()); startPathICB.pushAndSelect( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 1); progressGB.Dock = DockStyle.Fill; "tri dgv", this.Name, "http://msdn2.microsoft.com/fr-fr/library/ms171608(VS.80).aspx", "Tri des datagridview" ) ); "images dgv", this.Name, "http://msdn2.microsoft.com/fr-fr/library/z1cc356h(vs.80).aspx", "Images et datagridview" ) ); "context menu dgv", this.Name, "http://msdn2.microsoft.com/fr-fr/library/system.windows.forms.datagridviewrow.contextmenustrip.aspx", "Menu contextuel et datagridview" ) ); "association fichier", this.Name, "http://faqcsharp.developpez.com/?page=syst#syst_procstartshell", "Ouvrir un fichier avec l'application par défaut" ) ); } #endregion #region properties [ Category("Action"), Browsable(true), Description("Event called on file selected") ] public event FileSelectedEventHandler FileSelected; [ Category("Action"), Browsable(true), Description("Event called on directory selected") ] public event FileSelectedEventHandler DirectorySelected; #endregion #region search private void onKeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { startSearch(); } } private void searchBtn_Click(object sender, EventArgs e) { startSearch(); } private void startSearch() { if (startParseBtn.Text.Equals("Démarrer")) { String error = null; startPathICB.pushAndSelect(0); String startPath = startPathICB.SelectedInnerText; if (Directory.Exists(startPath)) { fileParser.Extensions.Clear(); /*if (findDVDChkB.Checked) fileParser.addExtension("DVD");*/ fileParser.addExtension("AVI"); { fileParser.addExtension("MPG"); fileParser.addExtension("MPEG"); } fileParser.addExtension("WMV"); fileParser.addExtension("MP3"); fileParser.addExtension("WAV"); { fileParser.addExtension("JPG"); fileParser.addExtension("JPEG"); } fileParser.addExtension("GIF"); fileParser.addExtension("PNG"); fileParser.addExtension("BMP"); fileParser.addExtension("PDF"); fileParser.addExtension("DOC"); fileParser.addExtension("TXT"); { fileParser.addExtension("PPS"); fileParser.addExtension("PPT"); } { fileParser.addExtension(findOtherTB.Text); } { error = "\nAucun type de fichier n'est sélectionné."; } else { startParseBtn.Text = "Arrêter"; startParseBtn.Image = global::be.gaudry.explorer.Properties.Resources.StopHS; if (mediaBrowserBgWorker.IsBusy) { error = "Impossible de lancer la recherche car une recherche est déjà en cours..."; } else { fileParser.StartPath = startPath; fileParser.SearchText = fileNameTB.Text; long min = 0L; { if (long.TryParse(sizeCriterionTB.Text, out min)) { min = min * 1024 * 1024; } /* else { min = 0; } } else { min = 0;*/ } fileParser.MinimumSize = min; /*if (calcProgressChkB.Checked) { calcProgressChkB.Enabled = false; }*/ displayProgressGB(); mediaBrowserBgWorker.RunWorkerAsync(); } } } else { error = String.Format("\n{0} n'est pas un chemin valide.", startPath); startPathICB.Items.RemoveAt(0); } if (error != null) { MessageBox.Show( this, error, "Impossible de démarrer la recherche", MessageBoxButtons.OK, MessageBoxIcon.Stop ); } } else { mediaBrowserBgWorker.CancelAsync(); startParseBtn.Text = "Démarrer"; startParseBtn.Image = global::be.gaudry.explorer.Properties.Resources.PlayHS; //browsePgB.ParameterValue = 0; } } #endregion #region browse settings /// <summary> /// Invoke delegate on path modification /// Reinit the progress max value /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void onPathModified(object sender, EventArgs e) { if (DirectorySelected != null) { DirectorySelected( this, startPathICB.Text ); } reinitProgressMaxValue(sender, e); } public void setPath(String path) { int img = 0; if (path.Equals(Environment.SpecialFolder.MyDocuments)) { img = 1; } startPathICB.pushAndSelect(path, img); } private void browseStartPathBtn_Click(object sender, EventArgs e) { //folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyDocuments; if (DialogResult.OK == folderBrowserDialog.ShowDialog()) { setPath(folderBrowserDialog.SelectedPath); } } private void fileNameChkB_Click(object sender, EventArgs e) { fileNameTB.Text = ""; } private void findAllExtensionsChkB_Click(object sender, EventArgs e) { enableExtensionVideo(enable); enableExtensionAudio(enable); enableExtensionImage(enable); foreach (Control c in fileTypeGB.Controls) { c.Enabled = enable; } findAllExtensionsChkB.Enabled = true; } private void enableExtensionVideo(bool enable) { } private void enableExtensionImage(bool enable) { } private void enableExtensionAudio(bool enable) { } private void enableExtensionBooks(bool enable) { } private void allFilmsChkB_Click(object sender, EventArgs e) { } private void allImagesChkB_Click(object sender, EventArgs e) { } private void allMusicsChkB_Click(object sender, EventArgs e) { } private void allBooksChkB_Click(object sender, EventArgs e) { } private void findOtherChkB_Click(object sender, EventArgs e) { findOtherTB.Text = ""; } private void sizeCriterionChkB_Click(object sender, EventArgs e) { } private void cleanDtgBtn_Click(object sender, EventArgs e) { resultsDGV.Rows.Clear(); cleanDtgBtn.Enabled = false; enableOutputBtn(false); cleanDtgBtn.Text = "Vider les résultats"; } private void enableOutputBtn(bool enabled) { printBtn.Enabled = enabled; saveBtn.Enabled = enabled; } #endregion #region progress methods private void displayProgressGB() { browsePgB.Value = 0; browsePgB.Style = ProgressBarStyle.Marquee; curDirValLbl.Text = ""; progressGB.Visible = true; } /// <summary> /// Reinit the progress max value /// (this value depends of the quantity of files into the directory to scan) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void reinitProgressMaxValue(object sender, EventArgs e) { searchResultsLbl.Visible = false; calcProgressChkB.Enabled = true; } /// <summary> /// Start asynchroneous search /// </summary> /// <remarks> /// In this method, do not modify any graphic component /// </remarks> /// <param name="sender"></param> /// <param name="e"></param> private void mediaBrowserBgWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bgw = sender as BackgroundWorker; // Call the model asynchroneous search //e.Result = fileParser.browse(bgw, e); e.Result = fileParser.parse(bgw, e); } /// <summary> /// Display results throws by the search /// </summary> /// <remarks> /// The graphic components may here react at the results /// </remarks> /// <param name="sender"></param> /// <param name="e"></param> private void mediaBrowserBgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { switch(e.ProgressPercentage) { case (int)BGWORKER.currentInfo: { String s = e.UserState as String; if(s!=null)curDirValLbl.Text = s; break; } case (int)BGWORKER.progressBarStyle_Block: { browsePgB.Style = ProgressBarStyle.Blocks; break; } default: { if (e.ProgressPercentage > 0) { browsePgB.Value = e.ProgressPercentage; if (e.UserState != null) { Object[] results = e.UserState as object[]; if (results != null) { if(!fileExistsInDGV(results[0] as string)) resultsDGV.Rows.Add(results); } } } break; } } } private void mediaBrowserBgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { startParseBtn.Text = "Démarrer"; browsePgB.Value = 0; cleanDtgBtn.Enabled = resultsDGV.RowCount > 0; enableOutputBtn( cleanDtgBtn.Enabled); cleanDtgBtn.Text = String.Format("Vider les {0} résultats", resultsDGV.RowCount); /*try { if (e.Result != null) { previousMaxResult = (float)e.Result; } } catch (System.Reflection.TargetInvocationException) { }*/ if (e.Error != null) { searchResultsLbl.Text = "Erreur : " + e.Error.Message; } else if (e.Cancelled) { searchResultsLbl.Text = "La recherche a été stoppée avant sa fin... Tous les résultats ne sont pas présents."; } else { searchResultsLbl.Text = String.Format("Taille du répertoire : {0}", Units.getLengthString((long)e.Result)); // The model maintains the previous results if no significative changes occurs calcProgressChkB.Enabled = false; } //searchResultsLbl.Text = String.Format("Taille du répertoire : {0}", Units.getLengthString((long)e.Result)); searchResultsLbl.Visible = true; startParseBtn.Image = global::be.gaudry.explorer.Properties.Resources.PlayHS; progressGB.Visible = false; } /// <summary> /// Check if a full filename exists in the DGV to avoid insert it again /// </summary> /// <param name="pathToCheck">(string) full filename to find in the DGV</param> /// <returns>(bool) true if exists, false otherwise</returns> private bool fileExistsInDGV(string pathToCheck) { if (pathToCheck == null) return false; foreach (DataGridViewRow row in resultsDGV.Rows) { if (row.Cells[0].Value.Equals(pathToCheck)) { return true; } } return false; } #endregion #region results format private void systemIconsChkB_Click(object sender, EventArgs e) { } private void resultsDGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.RowIndex < 0) return; // Set the colors for file extensions in the mediaType column. if (resultsDGV.Columns[e.ColumnIndex].Name.Equals("mediaType")) { BrolColor.BrolInnerColor color; String mediaTypeStr = (String)e.Value; switch (mediaTypeStr) { case "DVD": color = BrolColor.getBrolColor(1); break; case ".AVI": color = BrolColor.getBrolColor(2); break; case ".MPG": color = BrolColor.getBrolColor(3); break; case ".MPEG": color = BrolColor.getBrolColor(4); break; case ".WMV": color = BrolColor.getBrolColor(5); break; case ".MP3": color = BrolColor.getBrolColor(6); break; case ".WAV": color = BrolColor.getBrolColor(7); break; case ".JPEG": color = BrolColor.getBrolColor(8); break; case ".JPG": color = BrolColor.getBrolColor(9); break; case ".GIF": color = BrolColor.getBrolColor(10); break; case ".PNG": color = BrolColor.getBrolColor(11); break; case ".BMP": color = BrolColor.getBrolColor(12); break; case ".PDF": color = BrolColor.getBrolColor(13); break; case ".DOC": color = BrolColor.getBrolColor(14); break; case ".TXT": color = BrolColor.getBrolColor(15); break; case ".PPS": color = BrolColor.getBrolColor(16); break; case ".PPT": color = BrolColor.getBrolColor(17); break; default: color = BrolColor.getBrolColor(0); break; } e.CellStyle.BackColor = color.BackColor; e.CellStyle.ForeColor = color.ForeColor; //e.CellStyle.SelectionBackColor = Color.Brown; } // Replace filename values with system icons. { // Ensure that the value is a string. String stringValue = e.Value as string; if (stringValue == null) return; // Set the cell ToolTip to the text value. DataGridViewCell iconCell = resultsDGV[e.ColumnIndex, e.RowIndex]; iconCell.ToolTipText = stringValue; e.Value = iconList1.getImage(stringValue); } // Format file length if (resultsDGV.Columns[e.ColumnIndex].Name.Equals("mediaLength")) { // Ensure that the value is a string. long fileLength = (long)e.Value; { // Set the cell ToolTip to the text value. //DataGridViewCell cell = dataGridView1[e.ColumnIndex, e.RowIndex]; //cell.ToolTipText = String.Format("Taille de {0} octets", fileLength); e.Value = Units.getLengthString(fileLength); } } } #endregion #region results actions /// <summary> /// Used to get row infos /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void resultsDGV_CellMouseEnter(object sender, DataGridViewCellEventArgs e) { mouseLocation = e; } /// <summary> /// Notify the selected file on right click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void resultsDGV_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if ( mouseLocation.RowIndex < 0 || mouseLocation.ColumnIndex < 0 || e.Button != MouseButtons.Right || FileSelected == null ) return; FileSelected( this, (String)resultsDGV.Rows[mouseLocation.RowIndex].Cells["mediaIcon"].Value ); } /// <summary> /// Open file with system default application /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void resultsDGV_MouseDoubleClick(object sender, MouseEventArgs e) { if (mouseLocation.RowIndex >= 0 && mouseLocation.ColumnIndex >= 0) { FileHelper.open( (String)resultsDGV.Rows[mouseLocation.RowIndex].Cells["mediaIcon"].Value, "BrolExplorer" ); } } /// <summary> /// Notify the selected file on double click or on arrow click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void resultsDGV_CurrentCellChanged(object sender, EventArgs e) { if (resultsDGV.CurrentRow==null || resultsDGV.CurrentRow.Index < 0 || FileSelected == null) return; FileSelected( this, (String)resultsDGV.CurrentRow.Cells["mediaIcon"].Value ); } #endregion #region print and save methods protected internal void printBtn_Click(object sender, EventArgs e) { DgvFactory.print(resultsDGV, "Résultats de la recherche dans "+startPathICB.Text, this.ParentForm); } protected internal void saveBtn_Click(object sender, EventArgs e) { DgvFactory.save(resultsDGV, "Résultats de la recherche dans " + startPathICB.Text, this.ParentForm); } #endregion #region public methods public int getResultsCount() { return resultsDGV.RowCount; } #endregion /*private void startPathCB_DrawItem(object sender, DrawItemEventArgs e) { //SolidBrush solidBrush = new SolidBrush(startPathCB.ForeColor); //e.Graphics.DrawImage(dirExpandedImg, e.Bounds.X, e.Bounds.Y); //StringFormat stringFormat = new StringFormat(); //stringFormat.Alignment = StringAlignment.Near; //stringFormat.LineAlignment = StringAlignment.Center; Rectangle oRect = new Rectangle( e.Bounds.X + dirExpandedImg.Width, e.Bounds.Y, e.Bounds.Width - dirExpandedImg.Width, e.Bounds.Height); e.Graphics.DrawString(startPathCB.Items[e.Index].ToString(), startPathCB.Font, solidBrush, oRect, stringFormat); //solidBrush.Dispose(); }*/ } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732391076 23/11/2024 20:44:36 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
Version en cache
23/11/2024 20:44:36 Cette version de la page est en cache (à la date du 23/11/2024 20:44:36) afin d'accélérer le traitement. Vous pouvez activer le mode utilisateur dans le menu en haut pour afficher la dernère version de la page.Document créé le 30/10/2009, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/cs-brolexplorer-source-rf-view/controls//SearchMediaUserControl.cs.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.