Acerca de:

Este blog contiene los códigos, ejemplos y bases de datos que he usado cuando aprendía acerca de algún tema específico. En lugar de borrarlos (una vez dominado ya el tema), he decidido publicarlos :)

miércoles, 29 de agosto de 2012

Tutorial: Ejemplo de uso de las apis BitBlt, SetDIBits y GetDIBits

Actualización de Electrónica a Martillazos, Sección Visual Basic 6 y C++:
Ejemplo de Win32 y uso de las apis BitBLT, GetDIBits y SetDIBits. Compilado para Visual C++ Express 2008.
Link Directo
Un Mirror
Enlace para descargar el proyecto

Enjoy!

jueves, 16 de agosto de 2012

VB6: CommonDialog: Cómo saber si se desea abrir un archivo como sólo lectura

En Visual Basic 6, el control CommonDialog tiene una propiedad llamada "Flags" la cual determina las propiedades del Cuadro de Diálogo que se mostrará en nuestra aplicación. Sus valores son (de la documentación de Microsoft):

File Open/Save Dialog Box Flags

Constant Value Description
cdlOFNAllowMultiselect &H200
Specifies that the File Name list box allows multiple selections.
The user can select more than one file at run time by pressing the SHIFT key and using the UP ARROW and DOWN ARROW keys to select the desired files. When this is done, the FileName property returns a string containing the names of all selected files. The names in the string are delimited by spaces.
cdlOFNCreatePrompt &H2000 Specifies that the dialog box prompts the user to create a file that doesn't currently exist. This flag automatically sets the cdlOFNPathMustExist and cdlOFNFileMustExist flags.
cdlOFNExplorer &H80000 Use the Explorer-like Open A File dialog box template. Common dialogs that use this flag do not work under Windows NT using the Windows 95 shell.
CdlOFNExtensionDifferent &H400 Indicates that the extension of the returned filename is different from the extension specified by the DefaultExt property. This flag isn't set if the DefaultExt property is Null, if the extensions match, or if the file has no extension. This flag value can be checked upon closing the dialog box.
cdlOFNFileMustExist &H1000 Specifies that the user can enter only names of existing files in the File Name text box. If this flag is set and the user enters an invalid filename, a warning is displayed. This flag automatically sets the cdlOFNPathMustExist flag.
cdlOFNHelpButton &H10 Causes the dialog box to display the Help button.
cdlOFNHideReadOnly &H4 Hides the Read Only check box.
cdlOFNLongNames &H200000 Use long filenames.
cdlOFNNoChangeDir &H8 Forces the dialog box to set the current directory to what it was when the dialog box was opened.
CdlOFNNoDereferenceLinks &H100000 Do not dereference shell links (also known as shortcuts). By default, choosing a shell link causes it to be dereferenced by the shell.
cdlOFNNoLongNames &H40000 Do not use long file names.
CdlOFNNoReadOnlyReturn &H8000 Specifies that the returned file won't have the Read Only attribute set and won't be in a write-protected directory.
cdlOFNNoValidate &H100 Specifies that the common dialog box allows invalid characters in the returned filename.
cdlOFNOverwritePrompt &H2 Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.
cdlOFNPathMustExist &H800 Specifies that the user can enter only valid paths. If this flag is set and the user enters an invalid path, a warning message is displayed.
cdlOFNReadOnly &H1 Causes the Read Only check box to be initially checked when the dialog box is created. This flag also indicates the state of the Read Only check box when the dialog box is closed.
CdlOFNShareAware &H4000 Specifies that sharing violation errors will be ignored.



Si deseo que, por ejemplo, el CommonDialog sólo admita abrir archivos que existen y que no muestre la casilla de sólo lectura haré:

CommonDialog1.flags = &H1004
CommonDialog1.ShowOpen

Si deseo que sólo admita archivos existentes y que muestre la casilla de abrir como sólo lectura desactivada escribiré:

CommonDialog1.flags = &H1000
CommonDialog1.ShowOpen 

Si deseo que sólo admita archivos existentes y que muestre la casilla de abrir como sólo lectura activada escribiré:

CommonDialog1.flags = &H1001
CommonDialog1.ShowOpen

En Windows XP el cuadro de diálogo Abrir será:

En Windows 7:


Para el caso en que la propiedad Flags del CommonDialog es &H1001 en Windows 7, si se desea abrir un archivo y poder modificarlo, se elige la opción "Abrir para escritura", si se elige simplemente "Abrir" el programa considerará que se abre el archivo como sólo lectura.

Hay un patrón aquí: los valores de las constantes de la propiedad Flags simplemente se suman para poder asignar una propiedad determinada al CommonDialog. Los caracteres "&H" indican que los valores asignados están en hexadecimal, no en decimal.

Para saber si el usuario ha abierto el archivo como sólo lectura se debe evaluar el valor de la propiedad Flags del CommonDialogs después que el usuario ha elegido su archivo y cerrado la ventana de diálogo. Si el valor en Flags es impar, el usuario eligió abrir el archivo como sólo lectura, si es par, ha elegido abrirlo y poder escribir en él (modificarlo).

El código completo para este caso es:

On Error GoTo cogerE
CommonDialog1.flags = &H1001 
CommonDialog1.ShowOpen

Dim miFile as String
miFile = CommonDialog1.Filename

Dim readOnly1 As Integer

readOnly1 = CommonDialog1.flags
readOnly1 = readOnly1 Mod 2 
' si es impar el usuario ha activado la casilla de abrir como sólo lectura

If readOnly1 = 1 Then
    ' desactivar las opciones que escriben en el archivo pues es sólo lectura
Else
    ' activar las opciones que escriben en el archivo pues se ha abierto
    ' como lectura y escritura
End If

Exit Sub
cogerE:

El CommonDialog que estoy usando tiene su propiedad CancelError en Verdadero.

Caso Curioso: Si asigno a la propiedad Flags el valor de 5000 (en decimal) y ejecuto el programa en Windows XP SP3:

CommonDialog1.flags = 5000
CommonDialog1.ShowOpen 

Me mostrará este cuadro de diálogo, al más puro estilo Windows 95: