Wednesday, July 23, 2008

Introduction into threading in .NET (this article)

http://www.codeproject.com/KB/threads/ThreadingDotNet.aspx

Introduction

Im afraid to say that I am just one of those people that unless I am doing something, I am bored. So now that I finally feel I have learnt tha basics of WPF, it is time to turn my attention to other matters.

I have a long list of things that demand my attention such as (WCF/WF/CLR Via C# version 2 book), but I recently went for (and got, but turned it down in the end) which required me to know a lot about threading. Whilst I consider myself to be pretty good with threading, I thought yeah I'm ok at threading but I could always be better. So as a result of that I have decided to dedicate myself to writing a series of articles on threading in .NET. This series will undoubtely owe much to an excellent Visual Basic .NET Threading Handbook that I bought that is nicely filling the MSDN gaps for me and now you.

I suspect this topic will range from simple to medium to advanced, and it will cover a lot of stuff that will be in MSDN, but I hope to give it my own spin also.

I dont know the exact schedule, but it may end up being something like


Thursday, July 10, 2008

Simple Window Template

  1. #include


  2. // Step 4: the Window Procedure
  3. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  4. {
  5. switch(msg)
  6. {
  7. case WM_CREATE:

  8. break;
  9. case WM_CLOSE:
  10. DestroyWindow(hwnd);
  11. break;
  12. case WM_DESTROY:
  13. PostQuitMessage(0);
  14. break;
  15. default:
  16. return DefWindowProc(hwnd, msg, wParam, lParam);
  17. }
  18. return 0;
  19. }

  20. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  21. LPSTR lpCmdLine, int nCmdShow)
  22. {
  23. WNDCLASSEX wc;
  24. HWND hwnd;
  25. MSG Msg;
  26. static char appName[] = "Your Application";

  27. //Step 1: Registering the Window Class
  28. wc.cbSize = sizeof(WNDCLASSEX);
  29. wc.style = CS_HREDRAW | CS_VREDRAW;
  30. wc.lpfnWndProc = WndProc;
  31. wc.cbClsExtra = 0;
  32. wc.cbWndExtra = 0;
  33. wc.hInstance = hInstance;
  34. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  35. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  36. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  37. wc.lpszMenuName = NULL;
  38. wc.lpszClassName = appName;
  39. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

  40. if(!RegisterClassEx(&wc))
  41. {
  42. MessageBox(NULL, "Window Registration Failed!", "Error!",
  43. MB_ICONERROR | MB_OK);
  44. return 0;
  45. }

  46. // Step 2: Creating the Window
  47. hwnd = CreateWindowEx(
  48. WS_EX_CLIENTEDGE,
  49. appName,
  50. "Your Window Title",
  51. WS_OVERLAPPEDWINDOW,
  52. CW_USEDEFAULT, CW_USEDEFAULT, 400, 400,
  53. NULL, NULL, hInstance, NULL);

  54. if(hwnd == NULL)
  55. {
  56. MessageBox(NULL, "Window Creation Failed!", "Error",
  57. MB_ICONERROR | MB_OK);
  58. return 0;
  59. }

  60. ShowWindow(hwnd, nCmdShow);
  61. UpdateWindow(hwnd);

  62. // Step 3: The Message Loop
  63. while(GetMessage(&Msg, NULL, 0, 0) > 0)
  64. {
  65. TranslateMessage(&Msg);
  66. DispatchMessage(&Msg);
  67. }
  68. return int(Msg.wParam);
  69. }

Tuesday, July 8, 2008

How to set window topmost in .net 2003 for CE?

this.BringToFront and this.Focus() do not work for CE. Have to use C++ way of TOPMOST and SetWindowPos(..) as following:

[DllImport("coredll.dll", EntryPoint="GetCapture", SetLastError=true)]
private static extern IntPtr GetCapture();
[DllImport("coredll.dll", EntryPoint="SetWindowPos", SetLastError=true)]
private static extern bool SetWindowPos(
IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
uint uFlags);

public IntPtr GetControlHandle(Control ctl)
{
ctl.Capture = true;
IntPtr hWnd = GetCapture();
ctl.Capture = false;
return hWnd;
}



///
/// Set a form to the top most position.
///

/// The form to set topmost.
/// Returns true if successful, false otherwise.
public bool SetWindowTopMost(Form frm)
{
return SetWindowTopMost(frm, true);
}

///
/// Set a form to the top most position.
///

/// The form to set topmost.
/// True to set the form topmost, false to set the form to no topmost.
/// Returns true if successful, false otherwise.
public bool SetWindowTopMost(Form frm, bool bTopMost)
{
// Get the handle to the form entered.
IntPtr hWnd = GetControlHandle(frm);
IntPtr hWndInsertAfter = IntPtr.Zero;

if (bTopMost)
{
// Set the topmost flag.
hWndInsertAfter = new IntPtr(HWND_TOPMOST);
}
else
{
// Set the no topmost flag.
hWndInsertAfter = new IntPtr(HWND_NOTOPMOST);
}

return SetWindowPos(hWnd, hWndInsertAfter, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}