ArtinSoft's Blogs

Software Migration Experts
Welcome to ArtinSoft's Blogs Sign in | Join | Help
in Search

Mauricio Rojas Blog

April 2009 - Posts

  • VB6 Migrating MouseIcon Property

    In VB6 you can have code like:

    Private Sub Command1_Click()
        Dim x As StdPicture
        Set x = LoadPicture("C:\setup.ico")
        Me.MouseIcon = LoadPicture("C:\setup.ico")
        Me.MousePointer = ccCustom
    End Sub

     

    How can you migrate that to .NET??????

    Well maybe with a helper like the following can help:

    using System; 
    using System.Windows.Forms; 
    using VB6 = Microsoft.VisualBasic.Compatibility.VB6.Support;
    
    namespace Project2
    {
        internal partial class Form1
            : System.Windows.Forms.Form
            {
            
                private void  Command1_Click( Object eventSender,  EventArgs eventArgs)
                {
                    System.Drawing.Image x;
                    x = System.Drawing.Image.FromFile("C:\\setup.ico");
                    this.Cursor = CursorHelper.CreateCursor(x);
    
            }
                [STAThread]
                 static void  Main()
                {
                        Application.Run(new Form1());
                }
            }
    
    
    
    
      public class CursorHelper 
      {
          private struct IconInfo
          {
            public bool fIcon;
            public int xHotspot;
            public int yHotspot;
            public IntPtr hbmMask;
            public IntPtr hbmColor;
          }
      
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        static extern IntPtr CreateIconIndirect(ref IconInfo icon);
    
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
        static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
    
        private static Cursor CreateCursor(System.Drawing.Bitmap bmp, int xHotSpot, int yHotSpot)
        {
            IconInfo tmp = new IconInfo();
            GetIconInfo(bmp.GetHicon(), ref tmp);
            tmp.xHotspot = xHotSpot;
            tmp.yHotspot = yHotSpot;
            tmp.fIcon = false;
            return new Cursor(CreateIconIndirect(ref tmp));
        }
    
    
        public static Cursor CreateCursor(object picture)
        {
          if (picture is System.Drawing.Bitmap)
              return CreateCursor(picture as System.Drawing.Bitmap, 3, 3);
          else 
          {
              System.Drawing.Image image = null;
              IntPtr iunknown = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(picture);
              if (iunknown == IntPtr.Zero)
              {
                  throw new Exception("Unsupported format");
              }
              else 
              {
                  Guid guidIPicture = new Guid("7BF80980-BF32-101A-8BBB-00AA00300CAB");
                  Guid guidIPictureDisp = new Guid("7BF80981-BF32-101A-8BBB-00AA00300CAB");
                  IntPtr testIntPtr = IntPtr.Zero;
                  if (System.Runtime.InteropServices.Marshal.QueryInterface(iunknown,ref guidIPicture,out testIntPtr)==0)
                  {
                      image = Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureToImage(picture);
                  }
                  else if (System.Runtime.InteropServices.Marshal.QueryInterface(iunknown,ref guidIPictureDisp,out testIntPtr)==0)
                  {
                      image = Microsoft.VisualBasic.Compatibility.VB6.Support.IPictureDispToImage(picture);
                  }
                  if (image == null)
                  {
                      throw new Exception("Unsupported format");
                  }
                  using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image))
                  {
                      return CreateCursor(bitmap, 3, 3);
                  }
              };
               
              
          }
    
        }
    
      }
    
    }
  • Get the Week Number in C#

    Here is some examples of how to determine the WeekNumber of a given Date

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
    
            object index = DateTime.Now;
            int res = 0;
            //0    First day of year
            res = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
            Convert.ToDateTime(index), System.Globalization.CalendarWeekRule.FirstDay, System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
    
            //1    (Default) First four day week from Sunday
            res = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
            Convert.ToDateTime(index), System.Globalization.CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Sunday);
    
            //2    First four day week from StartOfWeek
            res = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                    Convert.ToDateTime(index), System.Globalization.CalendarWeekRule.FirstFourDayWeek, System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
    
            //3    First full week from Sunday
            res = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                    Convert.ToDateTime(index), System.Globalization.CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday);
            
            //4    First full week from StartOfWeek
            res = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(
                    Convert.ToDateTime(index), System.Globalization.CalendarWeekRule.FirstFullWeek, System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
    
    
            }
        }
    }

This Blog

Syndication

Powered by Community Server (Non-Commercial Edition), by Telligent Systems