Friday, September 5, 2008

Creating add-ons/toolbars for Google Chrome

I was looking for information how to write an add-on for Google Chrome, and while looking I couldn't find any information on this subject (the was this calculator JavaScript, but that's not an add-on).

After a while I've decided to try and dig in Chrome's code, however, even before doing that I found the answer in the Google Code repository of Chrome:

Q. How can I develop extensions for Chromium like in Firefox?
A. Chromium doesn't have an extension system yet. This is something we're interested in adding in a future version. Note that Chromium does support NPAPI-style "plugins", such as Adobe Flash and Apple QuickTime.

See the original FAQ here

Wednesday, September 3, 2008

Fixed aspect ratio in C# window

I needed to create a window with a fixed aspect ratio (for video), and I wanted that the resize frame will change with the user cursor drag.

I came around with this post. Which is almost perfect, but when you drag the left or upper bounds you get the window moving... Here is the fixed code:


#region Keep video window aspect ratio
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

public class MyRect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}

public const int WM_SIZING = 0x0214;
int _lastWidth = 0;
MyRect _lastRect = null;
double _aspectRatio = 0.5;

protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SIZING)
{
RECT rc;
rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));

if (_lastRect == null)
{
_lastRect = new MyRect();
_lastRect.Top = rc.Top;
_lastRect.Bottom = rc.Bottom;
_lastRect.Left = rc.Left ;
_lastRect.Right = rc.Right;
}

if (rc.Right - rc.Left != _lastWidth)
{
double newHeight = ((double)(rc.Right - rc.Left))
/ _aspectRatio;
rc.Bottom = (int)newHeight + _lastRect.Top;
Marshal.StructureToPtr(rc, m.LParam, true);
}
else
{
double newWidth = ((double)(rc.Bottom - rc.Top))
* _aspectRatio;

rc.Right = (int)newWidth + _lastRect.Left;
Marshal.StructureToPtr(rc, m.LParam, true);
}

_lastWidth = rc.Right - rc.Left;
_lastRect.Top = rc.Top;
_lastRect.Bottom = rc.Bottom;
_lastRect.Left = rc.Left;
_lastRect.Right = rc.Right;
}

base.WndProc(ref m);
}
#endregion