C# for MFC programmers
|
|
This list is by no means intended to be comprehensive. I am learning C#, and as I've had to look something up, I record it here. I figure if I had to look it up, you will, too. If you have entries to contribute based on your own experience, or if you discover an error caused by my own naiveté in C#, feel free to send them. I won't attribute each individual contributions, but I will add you to a "contributions by..." section at the end. If you want your email or your Web site included in the attributions list, please say so explicitly; otherwise I will just put your name there. Or, if you want to remain anonymous, tell me and I will hog all the credit myself.
I've discovered a number of useful Web sites for beginning C# programmers. Included in no particular order,
A |
B |
C |
D |
E |
F |
G |
H |
I |
J |
K |
L |
M |
N |
O |
P |
Q |
R |
S |
T |
U |
V |
W |
X |
Y |
Z |
C/MFC concept | C# | ||||
A |
|||||
abs |
Math.Abs |
||||
int n = ...; int a = abs(n); |
int n = ...; int a = Math.Abs(n); |
||||
int n = -2147483648; // largest negative # UINT u = abs(n); // n = 2147483648 |
int n = -2147483648; uint u = Math.Abs(n); // exception! but the following works! uint u = unchecked(n < 0 ? -n : n); |
||||
.AddString(...) (CListBox) |
.Items.Add(...) (CListBox) |
||||
AfxMessageBox |
MessageBox.Show |
||||
UINT n = AfxMessageBox(body, flags); |
DialogResult n = MessageBox.Show(body, caption, flags, icon); |
||||
See MessageBox | |||||
atoi |
Parse |
||||
CString s; int n = atoi(s); |
String s int n = int.Parse(s) |
||||
atol |
Parse |
||||
CString s; long n = atol(s); |
String s long n = long.Parse(s) |
||||
B |
|||||
BOOL |
Boolean bool |
||||
bool |
Boolean bool |
||||
BS_PUSHLIKE |
Drag a check box onto the form. Select its Appearance property as Button |
||||
BYTE |
byte |
||||
C |
|||||
CBitmap |
Bitmap (actually, a GDI+ bitmap) |
||||
CBrush |
Brush |
||||
CDC |
Graphics |
||||
CClientDC |
Graphics |
||||
CClientDC dc(&c_Ctl) |
Graphics g = c_Ctl.CreateGraphics(); |
||||
ceil |
Math.Ceiling |
||||
CenterWindow (CWnd) |
.StartPosition = FormStartPosition.CenterParent (can be set at design time) |
||||
CFileDialog |
Create an object (usually in the forms design template) of type SaveFileDialog or OpenFileDialog. Use the ShowDialog method to invoke it. | ||||
CFont |
Font |
||||
char (as a character type) |
char |
||||
Note however that char in C# means Unicode character and is not the same as byte or the concept in C/C++ known as char. Signedness is not a characteristic of the C# char type. It is conceptually closer to the C/C++ WCHAR type. | |||||
char (as a signed 8-bit integer) |
sbyte |
||||
CListBox::AddString(...) (CListBox) |
CListBox.Items.Add(...) (CListBox) |
||||
CListBox.GetLBText(n) (CListBox) |
CListBox.GetItemText(n) |
||||
COLOR_ constants |
See GetSysColor |
||||
COLORREF |
Color |
||||
COLORREF r = RGB(255,128,0); |
Color c = Color.FromArgb(255,128,0); |
||||
This actually oversimplifies, or over complicates, the problem. C# contains a number of "predefined color names", many with really obscure names and no good graphical representation in the Help system of what they look like. However, RGB(255,0,0) is the name .Red. The names appear to conform to the HTML naming conventions. You can also name system colors. See GetSysColor. | Argb stands for the 4-tuple, Alpha, Red, Green, Blue, and the overload with three arguments assumes Alpha (transparency) is 255 (completely opaque). | ||||
COLORREF r = RGB(255,0,0) |
Color c = Color.Red; |
||||
CPaintDC |
The .Graphics member of the System.Windows.Forms.PaintEventArgs argument to the OnPaint event | ||||
CPen |
Pen |
||||
CSize |
Size (integer values) SizeF (floating point values) |
||||
CSize sz = ...; int w = sz.cx; int h = sz.cy; |
SizeF sz = ...; int w = sz.Width; int h = sz.Height; |
||||
This is an oversimplification because the fields of a SizeF are floats and not integers! | |||||
CSpinCtrl (with CEdit buddy control) |
NumericUpDown |
||||
CString |
String |
||||
CString::Find(CString s) |
String.IndexOf(String s) |
||||
CString::Format |
Format |
||||
CString s; s.Format(fmt, val...) |
String s; s = Format(fmt, val...) |
||||
Formatting
specifications in C# are much more flexible than in traditional C. For
example, you can associate several format requests with the same argument,
and the formatting options are richer, including localized date, time, and
currency representations.
Instead of %, the format specifiers in C# are enclosed in {}, and are of the form {n:Fp} where n specifies the argument in the formatting string (the n: can be omitted in the ToString method call), F is a format specifier, and p is the precision. |
|||||
%d |
{n:d} or {n:D} |
||||
%02d |
{n:d2} or {n:D2} |
||||
%o |
octal is not supported | ||||
%ld |
{n:d} No special handling is required for int values | ||||
%I64d |
{n:d} No special handling is required for 64-bit int values | ||||
%x |
{n:x} |
||||
%X |
{n:X} |
||||
CWnd::GetClientRect() |
ClientRectangle (property) |
||||
CWnd::Invalidate() |
.Invalidate() |
||||
CWnd::InvalidateRect(&r) |
.Invalidate(...); |
||||
CWnd::ShowWindow(SW_SHOW) |
.Visible = true; |
||||
CWnd::ShowWindow(SW_HIDE); |
.Visible = false; |
||||
CString.GetLength(); |
String.Length; |
||||
D |
|||||
DeleteDC |
No need to; implicitly handled by the Graphics destructor during garbage collection. | ||||
DestroyWindow |
Close |
||||
DoModal |
.ShowDialog |
||||
double |
double |
||||
DrawItem (CListBox) |
DrawItem event |
||||
DWORD |
uint |
||||
E |
|||||
.Ellipse |
.DrawEllipse |
||||
CClientDC dc(&wnd); CRect r; CPen pen(PS_SOLID, n, RGB(0,0,0)); dc.SelectObject(&pen); dc.Ellipse(&r); |
Graphics g = wnd.CreateGraphics(); Rectangle r; Pen pen = new Pen(Color.Black, n); g.DrawEllipse(pen, r); |
||||
EndDialog(IDCancel) |
Close |
||||
EndDialog(IDOK) |
Close |
||||
exp |
Math.Exp |
||||
F |
|||||
.Find (CString) |
.IndexOf (String) |
||||
float |
float |
||||
.Format |
see CString::Format | ||||
G |
|||||
GetBValue |
.B |
||||
COLORREF c = ...; int b = GetBValue(c); |
Color c = ...; int b = c.B; |
||||
.GetClientRect() |
.ClientRectangle (property) |
||||
CRect r; wnd.GetClientRect(&r); |
Rectangle r = wnd.ClientRectangle; |
||||
GetCurSel (CComboBox) |
... = ComboBox.SelectedIndex; |
||||
GetDC |
Graphics |
||||
CDC * dc = wnd.GetDC(); |
Graphics g = wnd.CreateGraphics(); |
||||
GetFont |
.Font |
||||
CFont * f = wnd.GetFont(); |
Font f = wnd.Font; |
||||
GetGValue |
.G |
||||
COLORREF c = ...; int g = GetGValue(c); |
Color c = ...; int g = c.G; |
||||
.GetLBText(n) (CListBox) |
.GetItemText(n) |
||||
.GetLBText(n) (CComboBox) |
.GetSelectedItem.ToString(); |
||||
.GetLength() (CString) |
s.Length |
||||
.GetMiterLimit (CDC) |
... = Pen.MiterLimit; |
||||
GetModuleFileName |
System.Windows.Forms.Application.ExecutablePath |
||||
TCHAR name[MAX_PATH]; GetModuleFileName(NULL, name, MAX_PATH); |
String name = System.Windows.Forms. Application.ExecutablePath; |
||||
GetPos (CSpinCtrl) |
NumericUpDown.Value Note that the values of .Minimum, .Maximum, and .Value are of type decimal. This means that to assign a value which is a fractional value, you have to use a decimal constant. To use the values, you may have to cast them from decimal to int or float. |
||||
GetRange (CSpinCtrl) |
NumericUpDown.Maximum and NumericUpDown.Minimum Note that the values of .Minimum, .Maximum, and .Value are of type decimal. This means that to assign a value which is a fractional value, you have to use a decimal constant. To use the values, you may have to cast them from decimal to int or float. |
||||
GetRValue |
.R |
||||
COLORREF c = ...; int r = GetRValue(c); |
Color c = ...; int r = c.R; |
||||
.GetSize() (CArray) |
s.Length |
||||
GetSysColor |
Color.FromKnownColor(KnownColor.colorname) |
||||
COLORREF c = ::GetSysColor(COLOR_WINDOW) |
Color.KnownColor.Window |
||||
COLOR_3DDKSHADOW |
Color.KnownColor.ControlDarkDark (?) |
||||
COLOR_3DFACE |
Color.KnownColor.Control (?) |
||||
COLOR_3DHIGHLIGHT |
Color.KnownColor.ControlLight (?) |
||||
COLOR_3DHILIGHT |
Color.KnownColor.ControlLight (?) |
||||
COLOR_3DLIGHT |
Color.KnownColor.? |
||||
COLOR_ACTIVEBORDER |
Color.KnownColor.ActiveBorder |
||||
COLOR_ACTIVECAPTION |
Color.KnownColor.ActiveCaption |
||||
COLOR_APPWORKSPACE |
Color.KnownColor.AppWorkspace |
||||
COLOR_BACKGROUND |
Color.KnownColor.Desktop |
||||
COLOR_BTNFACE |
Color.KnownColor.Control (?) |
||||
COLOR_BTNHIGHLIGHT |
Color.KnownColor.ControlLight (?) |
||||
COLOR_BTNHILIGHT |
Color.KnownColor.ControlLight (?) |
||||
COLOR_BTNSHADOW |
Color.KnownColor.ControlDark (?) |
||||
COLOR_BTNTEXT |
Color.KnownColor.ControlText |
||||
COLOR_CAPTIONTEXT |
|||||
COLOR_DESKTOP |
Color.KnownColor.Desktop |
||||
COLOR_GRADIENTACTIVECAPTION |
|||||
COLOR_GRADIENTINACTIVECAPTION |
|||||
COLOR_GRAYTEXT |
Color.KnownColor.GrayText |
||||
COLOR_HIGHLIGHT |
Color.KnownColor.Highlight |
||||
COLOR_HIGHLIGHTTEXT |
Color.KnownColor.HighlightText |
||||
COLOR_HOTLIGHT |
Color.KnownColor.HotTrack (?) |
||||
COLOR_INACTIVEBORDER |
Color.KnownColor.InactiveBorder |
||||
COLOR_INACTIVECAPTION |
Color.KnownColor.InactiveCaption |
||||
COLOR_INACTIVECAPTIONTEXT |
Color.KnownColor.InactiveCaptionText |
||||
COLOR_INFOBK |
|||||
COLOR_INFOTEXT |
Color.KnownColor.InfoText |
||||
COLOR_MENU |
Color.KnownColor.Menu |
||||
COLOR_MENUTEXT |
Color.KnownColor.MenuText |
||||
COLOR_SCROLLBAR |
Color.KnownColor.ScrollBar |
||||
COLOR_WINDOW |
Color.KnownColor.Window |
||||
COLOR_WINDOWFRAME |
Color.KnownColor.WindowFrame |
||||
COLOR_WINDOWTEXT |
Color.KnownColor.WindowText |
||||
GetSystemMetrics |
System.Windows.Forms.SystemInformation |
||||
GetTextExtent |
MeasureString |
||||
CClientDC dc(&wnd); CSize sz; CString s = ...; sz = dc.GetTextExtent(s); |
Graphics g = wnd.CreateGraphics(); SizeF sz; String s = ...; sz = g.MeasureString(wnd.Font, s); |
||||
GetTickCount | DateTime.Now.Ticks | ||||
GetUserName |
System.Environment.UserName |
||||
GWL_DLGRESULT |
Closing event, DialogResult property |
||||
void CMyDialog::OK() { DWORD result = ...; SetWindowLong((HWND)this, GWL_DLGRESULT, result); CDialog::OnOK(); } |
select the Closing event for the form and type a name of the function you want, such as OnClosing private void OnClosing( Object sender, System.ComponentModel.CancelEventArgs e) { Object result = ...; e.DialogResult = result; } |
||||
H |
|||||
HBITMAP |
Bitmap (actually, a GDI+ bitmap) |
||||
HBRUSH |
Brush |
||||
HDC |
Graphics (see CDC) | ||||
HPEN |
Pen |
||||
I |
|||||
_I64_MAX | Int64.MaxValue | ||||
_I64_MIN | Int64.MinValue | ||||
int |
int |
||||
__int64 |
long |
||||
INT_MAX | Int32.MaxValue | ||||
INT_MIN | Int32.MinValue | ||||
.Invalidate() |
.Invalidate(); |
||||
.InvalidateRect |
.Invalidate(); |
||||
CRect r; wnd.InvalidateRect(&r) |
Rectangle r; wnd.Invalidate(r); |
||||
itoa |
ToString |
||||
%d |
3 |
int v; String s; s = i.ToString(); |
|||
%3d |
··3 (leading spaces) |
int v; String s; s = String.Format("{0,3:d}", v); |
|||
%03d |
003 |
int v = 3; String s; s = i.ToString("D3"); |
|||
%03d |
003 |
int v = 3; String s; s = i.ToString("000"); |
|||
K |
|||||
KillTimer |
place a timer object on the form
and give it a name timer.Stop(); |
||||
L |
|||||
LineTo |
DrawLine |
||||
CClientDC dc(&wnd); dc.SelectObject(&pen); dc.MoveTo( x0, y0); dc.LineTo( x1, y1); |
Graphics g = wnd.CreateGraphics(); g.DrawLine(pen, x0, y0, x1, y1); |
||||
CClientDC dc(&wnd); dc.SelectObject(&pen); CPoint p0(x0, y0); CPoint p1(x1, y1); dc.MoveTo( p0 ); dc.LineTo( p1 ); |
Graphics g = wnd.CreateGraphics(); Point p0 = new Point(x0, y0); Point p1 = new Point(x1, y1); g.DrawLine(pen, p0, p1); |
||||
::LoadCursor(NULL, cursorid) LoadStandardCursor(cursorid) (CWinApp) |
No equivalent. See OnSetCursor | ||||
long |
int |
||||
LONG |
int |
||||
LONGLONG |
long |
||||
M |
|||||
MessageBox |
MessageBox.Show |
||||
UINT n = MessageBox(body, caption, flags); |
DialogResult n = MessageBox.Show(body); DialogResult n = MessageBox.Show(body, caption); DialogResult n = MessageBox.Show(body, caption, buttons); DialogResult n = MessageBox.Show(body, caption, buttons, icon); DialogResult n = MessageBox.Show(body, caption, buttons, icon, defaultbutton); |
||||
buttons can be any of the following: | |||||
MB_ABORTRETRYIGNORE |
MessageBoxButtons.AbortRetryIgnore |
||||
MB_HELP |
no equivalent |
||||
MB_OK |
MessageBoxButtons.OK |
||||
MB_OKCANCEL |
MessageBoxButtons.OKCancel |
||||
MB_OKRETRY |
MessageBoxButtons.OKRetry |
||||
MB_YESNO |
MessageBoxButtons.YesNo |
||||
MB_YESNOCANCEL |
MessageBoxButtons.YesNoCancel |
||||
icon is optional and can be any of | |||||
MB_ASTERISK |
MessageBoxIcon.Asterisk Note: this code should be considered obsolete; .Information should be used instead |
||||
MB_ERROR |
MessageBoxIcon.Error |
||||
MB_EXCLAMATION |
MessageBoxIcon.Exclamation Note: this code should be considered obsolete; .Warning should be used instead |
||||
MB_HAND |
MessageBoxIcon.Hand Note: this code should be considered obsolete; .Error should be used instead |
||||
MB_INFORMATION |
MessageBoxIcon.Information |
||||
MessageBoxIcon.None |
|||||
MB_ICONQUESTION |
MessageBoxIcon.Question |
||||
MB_ICONSTOP |
MessageBoxIcon.Stop Note: this code should be considered obsolete; .Error should be used instead |
||||
MB_ICONWARNING |
MessageBoxIcon.Warning |
||||
MB_USERICON |
no equivalent |
||||
The default button can be one of the following | |||||
MB_DEFBUTTON1 |
MessageBoxDefaultButton.Button1 |
||||
MB_DEFBUTTON2 |
MessageBoxDefaultButton.Button2 |
||||
MB_DEFBUTTON3 |
MessageBoxDefaultButton.Button3 |
||||
MB_DEFBUTTON4 |
no equivalent |
||||
The return value can be one of the following | |||||
IDABORT |
DialogResult.Abort |
||||
IDCANCEL |
DialogResult.Cancel |
||||
IDIGNORE |
DialogResult.Ignore |
||||
IDNO |
DialogResult.No |
||||
There is no equivalent to this value in the standard API |
DialogResult.None |
||||
IDOK |
DialogResult.OK |
||||
IDRETRY |
DialogResult.Retry |
||||
IDYES |
DialogResult.Yes |
||||
MoveTo |
DrawLine |
||||
CClientDC dc(&wnd); dc.SelectObject(&pen); dc.MoveTo( x0, y0); dc.LineTo( x1, y1); |
Graphics g = wnd.CreateGraphics(); g.DrawLine(pen, x0, y0, x1, y1); |
||||
CClientDC dc(&wnd); CPoint p0(x0, y0); CPoint p1(x1, y1); dc.SelectObject(&pen); dc.MoveTo( p0 ); dc.LineTo( p1 ); |
Graphics g = wnd.CreateGraphics(); Point p0 = new Point(x0, y0); Point p1 = new Point(x1, y1); g.DrawLine(pen, p0, p1); |
||||
MoveWindow |
.Location (changes only the top-left origin) |
||||
// Move window to X, Y retaining size CRect r; ctl.GetWindowRect(&r); CSize sz(r.Width(), r.Height()); ctl.MoveWindow(X, Y, sz.cx, sz.cy); |
ctl.Location = new Point(X, Y); Note that accessing the .Location member returns a copy of the Point value, so changing the values in this will not change the size in the control itself, e.g., ctl.Location.x = X; // does not work changes only the value in the copy. You must assign a complete Point object to the .Location member. |
||||
MoveWindow |
.Size (changes only the size) .Width (changes only the width) .Height (changes only the height) |
||||
// Change window to size W,H in same place CRect r; ctl.GetWindowRect(&r); ScreenToClient(&r); ctl.MoveWindow(r.left, r.top, W, H); |
ctl.Size = new Size(X, Y); or you can change each dimension independently ctl.Width = W; ctl.Height = H; Note that accessing the .Size member returns a copy of the Size value, so changing the values in this will not change the size in the control itself, e.g., ctl.Size.Width = W; // does not work changes only the value in the copy. You must assign a complete Size object to the .Size member, or assign independently to the .Width or .Height members |
||||
N |
|||||
NULL |
null |
||||
O |
|||||
OnClose |
Closing event |
||||
void CMyWnd::OnClose() { if(MyQueryClose()) return; CWnd::OnClose(); } |
select the Closing event for the form and type a name of the function you want, such as OnClosing private void OnClosing( Object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = MyQueryClose(); } |
||||
OnDestroy |
Closed event |
||||
void CMyWnd::OnDestroy() { ... } |
select the Closing event for the form and type a name of the function you want, such as OnClosed private void OnClosing(Object sender, System.ComponentModel.EventArgs e) { ... } |
||||
OnVScroll(...) |
Create a ScrollEvent handler |
||||
switch(nSBCode) |
switch(e.Type) |
||||
SB_PAGEUP |
ScrollEventType.LargeDecrement |
||||
SB_PAGEDOWN |
ScrollEventType.LargeIncrement |
||||
SB_LINEUP |
ScrollEventType.SmallDecrement |
||||
SB_LINEDOWN |
ScrollEventType.SmallIncrement |
||||
SB_TOP |
ScrollEventType.First |
||||
SB_BOTTOM |
ScrollEventType.Last |
||||
SB_THUMBPOSITION |
ScrollEventType.ThumbPosition |
||||
SB_THUMBTRACK |
ScrollEventType.ThumbTrack |
||||
SB_ENDSCROLL |
ScrollEventType.EndPos |
||||
OnInitDialog |
Load event |
||||
OnLButtonDown |
MouseDown event |
||||
OnLButtonUp |
MouseUp event |
||||
OnMouseMove |
MouseMove event |
||||
OnMove |
LocationChanged event |
||||
OnPaint |
OnPaint event |
||||
OnSetCursor |
No equivalent; cursor is set using the Cursor property of the window whose cursor is to be set. See SetCursor | ||||
OnSize |
SizeChanged event |
||||
P |
|||||
PostMessage |
BeginInvoke |
||||
ON_MESSAGE(UWM_MYMESSAGE, OnMyMessage) LRESULT CMyClass::OnMyMessage( WPARAM, LPARAM) { ... do something return 0; } void CMyClass::DoSomething() { CString * s = new CString(_T("Message")); PostMessage(UWM_MYMESSAGE, (WPARAM)s); |
public delegate void MessageHandler(String s); class CMyClass { public void MyHandler(String s) { ... } } public void DoSomething() { String s = "Message"; MessageHandler handler = new MessageHandler(MyHandler); handler.BeginInvoke(handler); |
||||
As with many C# equivalences, this is an oversimplification of a far more sophisticated concept. For example, in C# you can actually receive a callback when the message is handled, or wait for the receiving thread to process it. | |||||
PostQuitMessage(0) |
Close() |
||||
pow |
Math.Pow |
||||
printf |
System.Console.Write System.Console.WriteLine |
||||
printf("%s", string); |
CString string; System.Console.Write(string); |
||||
printf("%s\n", string); |
CString string; System.Console.Write(string +"\n"); |
||||
CString string; System.Console.WriteLine(string); |
|||||
puts |
System.Console.Write System.Console.WriteLine |
||||
CString string; puts(string); |
CString string; System.Console.Write(string); |
||||
R |
|||||
.Rectangle |
.DrawRectangle |
||||
CClientDC dc(&wnd); CRect r; ...assign to r dc.SelectObject(&pen); dc.SelectStockObject(HOLLOW_BRUSH); dc.Rectangle(&r); |
Graphics g = wnd.CreateGraphics(); Rectangle r; ...assign to r g.DrawRectangle(pen, r); |
||||
ReleaseDC |
No need to; implicitly handled by the Graphics destructor during garbage collection. | ||||
.ResetContent |
listbox.Items.Clear() |
||||
.RestoreDC (CDC) |
.Restore (graphics) | ||||
CClientDC dc(&wnd); int n = dc.SaveDC(); ... dc.RestoreDC(n); |
Graphics g = wnd.CreateGraphics(); GraphicsState gs = g.Save(); ... g.Restore(gs); |
||||
.ReverseFind (CString) |
.LastIndexOf |
||||
RGB |
.FromArgb |
||||
COLORREF r = RGB(255,128,0); |
Color c = Color.FromArgb(255,128,0); |
||||
This actually oversimplifies, or over complicates, the problem. C# contains a number of "predefined color names" with really obscure names and no good graphical representation in the Help system of what they look like. The names correspond to the standard names used in HTML. However, as an example, RGB(255,0,0) is the name .Red | Argb stands for the 4-tuple, Alpha, Red, Green, Blue, and the overload with three arguments assumes Alpha (transparency) is 255 (completely opaque). | ||||
COLORREF r = RGB(255,0,0) |
Color c = Color.Red; or Color c = Color.FromArgb(255,0,0); |
||||
S |
|||||
.SaveDC (CDC) |
.Save (graphics) | ||||
CClientDC dc(&wnd); int n = dc.SaveDC(); ... dc.RestoreDC(n); |
Graphics g = wnd.CreateGraphics(); GraphicsState gs = g.Save(); ... g.Restore(gs); |
||||
SetCurSel (CComboBox) |
ComboBox.SelectedIndex = ...; |
||||
::SetCursor(AfxGetApp()->LoadStandardCursor(id)); |
.Cursor = Cursors.id |
||||
IDC_APPSTARTING |
Cursors.AppStarting |
||||
IDC_ARROW |
Cursors.Arrow |
||||
IDC_CROSS |
Cursors.Cross |
||||
no equivalent |
Cursors.Default |
||||
IDC_HAND |
Cursors.Hand |
||||
IDC_HELP |
Cursors.Help |
||||
no equivalent |
Cursors.HSplit |
||||
IDC_IBEAM |
Cursors.IBeam |
||||
IDC_NO |
Cursors.No |
||||
no equivalent |
Cursors.NoMove2D |
||||
no equivalent |
Cursors.NoMoveHoriz |
||||
no equivalent |
Cursors.NoMoveVert |
||||
no equivalent |
Cursors.PanEast |
||||
no equivalent |
Cursors.PanNE |
||||
no equivalent |
Cursors.PanNorth |
||||
no equivalent |
Cursors.PanNW |
||||
no equivalent |
Cursors.PanSE |
||||
no equivalent |
Cursors.PanSouth |
||||
no equivalent |
Cursors.PanSW |
||||
no equivalent |
Cursors.PanWest |
||||
IDC_SIZEALL |
Cursors.SizeAll |
||||
IDC_SIZENESW |
Cursors.SizeNESW |
||||
IDC_SIZENS |
Cursors.SizeNS |
||||
IDC_SIZESWNE |
Cursors.SizeSWNE |
||||
IDC_SIZEWE |
Cursors.SizeWE |
||||
IDC_UPARROW |
Cursors.UpArrow |
||||
no equivalent |
Cursors.VSplit |
||||
IDC_WAIT |
Cursors.WaitCursor |
||||
.SetMapMode (CDC) |
.PageScale (Graphics) |
||||
MM_TEXT |
GraphicsUnit.Pixel |
||||
MM_LOENGLISH MM_HIENGLISH |
GraphicsUnit.Inch |
||||
MM_LOMETRIC MM_HIMETRIC |
GraphicsUnit.Millimeter |
||||
MM_TWIPS (1/1440 inch) |
GraphicsUnit.Document (1/300 inch) |
||||
no equivalent |
GraphicsUnit.Display (1/72 inch) |
||||
MM_ISOTROPIC MM_ANISOTROPIC |
GraphicsUnit.World (closest approximation) |
||||
no equivalent |
GraphicsUnit.Display (1/75 inch) |
||||
.SetMiterLimit (CDC) |
Pen.MiterLimit = ...; |
||||
.SetPos (CSpinCtrl) |
NumericUpDown.Value = ... Note that the values of .Minimum, .Maximum, and .Value are of type decimal. This means that to assign a value which is a fractional value, you have to use a decimal constant. To use the values, you may have to cast them from decimal to int or float. |
||||
.SetRange (CSpinCtrl) |
NumericUpDown.Maximum = ... NumericUpDown.Minimum = ...; Note that the values of .Minimum, .Maximum, and .Value are of type decimal. This means that to assign a value which is a fractional value, you have to use a decimal constant. To use the values, you may have to cast them from decimal to int or float. |
||||
SetTimer |
place a timer object on the form and give it a name timer.Interval = ...; // can be set // at design time timer.Start(); |
||||
.SetViewPortExt/ .SetWindowExt (CDC) |
.PageScale (Graphics) |
||||
SetWindowPos |
.Location (changes only the top-left origin) .Size (changes width and height but not origin) .Width (changes only width) .Height (changes only height) |
||||
// Move window to X, Y retaining size ctl.SetWindowPos(NULL, X, Y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); |
ctl.Location = new Point(X, Y); Note that accessing the .Location member returns a copy of the Point value, so changing the values in this will not change the size in the control itself, e.g., ctl.Location.x = X; // does not work changes only the value in the copy. You must assign a complete Point object to the .Location member. |
||||
// Change size to W, H and don't move ctl.SetWindowPos(NULL, 0, 0, W, H, SWP_NOMOVE | SWP_NOZORDER); |
ctl.Size = new Size(W, H); or the values can be set independently ctl.Width = W; ctl.Height = H; Note that accessing the .Size member returns a copy of the Size value, so changing the values in this will not change the size in the control itself, e.g., ctl.Size.Width = W; // does not work changes only the value in the copy. You must assign a complete Size object to the .Size member, or assign independently to the .Width or .Height members |
||||
ShBrowseForFolder |
No good equivalent. KB article 306285 shows how to call
the underlying ShBrowseForFolder call in .NET 1.0; in .NET 1.1
there is a FolderBrowserDialog class, but it is not supported in
the Common Framework, meaning you can't use it if you expect to run on a
Pocket PC. Probably your best bet is to look at the Web site
http://www.wintoolzone.com/dotnetcom.aspx where there is an implementation that does not involve the shell API and does not require Common Framework (CF) support. |
||||
ShellExecute |
System.Diagnostics.Process.Start |
||||
SHRT_MAX | Int16.MaxValue | ||||
SHRT_MIN | Int16.MinValue | ||||
short |
short |
||||
.ShowWindow(SW_SHOW); |
.Visible = true; |
||||
.ShowWindow(SW_HIDE); |
.Visible = false; |
||||
sprintf |
ToString |
||||
%d |
3 |
int v; String s; s = i.ToString(); |
|||
%3d |
··3 (leading spaces) | No equivalent in C# | |||
%03d |
003 |
int v = 3; String s; s = i.ToString("D3"); |
|||
%03d |
003 |
int v = 3; String s; s = i.ToString("000"); |
|||
strcat |
+ (applied to String objects) |
||||
strcmp | .Equals |
String s1; String s2; if(s1.Equals(s)) |
|||
note that the comparison operators work also; so you can
write if(s1 < s2) or if(s1 == s2) or if(s1 > s2) as well as !=, <= and >=. |
|||||
strrchr |
LastIndexOf |
||||
strstr | .Contains |
String s; s = "Permissions"; if(s.Contains("miss")) ... contains the substring |
|||
strtol(LPCTSTR, NULL, 16) |
int.Parse(String, "x") // throws exception |
||||
strtol(LPCTSTR, NULL, 10) |
int.Parse(String, "d") // throws exception |
||||
strtol(LPCTSTR, NULL, 2) |
No equivalent |
||||
strtol(LPCTSTR, NULL, 8) |
No equivalent |
||||
T |
|||||
_T(...) |
Not needed as all strings and characters are implicitly Unicode strings and characters. | ||||
U |
|||||
UINT |
uint |
||||
ULONG |
uint |
||||
ULONGLONG |
ulong |
||||
unsigned |
uint |
||||
unsigned int |
uint |
||||
unsigned __int64 |
ulong |
||||
W |
|||||
WM_CLOSE |
Close |
||||
PostMessage(WM_CLOSE) |
Close() |
||||
WM_DESTROY |
Closed event |
||||
void CMyWnd::OnDestroy() { ... } |
select the Closing event for the form and type a name of the function you want, such as OnClosed private void OnClosed(Object sender, System.ComponentModel.EventArgs e) { ... } |
||||
WM_INITDIALOG |
Load event |
||||
WM_MOVE |
LocationChanged event |
||||
WM_SIZE |
SizeChanged event |
||||
WORD |
ushort |
I didn't discover all of this myself. I asked a lot of questions. Thanks to many contributors, especially those in the microsoft.public.dotnet.languages.csharp and the microsoft.public.dotnet.framework.windowsforms newsgroups. Particular contributors whose information I have used in this table, in alphabetical order, include
The views expressed in these essays are those of the author, and in no way represent, nor are they endorsed by, Microsoft.