Saturday, February 19, 2011

Windows .Net Framework 2.0 : Disable Close Button

Objective:
1. Disable the red Close button on the top right of Windows Form


How?
1. We will need to make use of user32.dll to do so.
2. Importing the win32 functions and setting up defines
using namespace System; using namespace System::Runtime::InteropServices; #define SC_CLOSE 0xF060 #define MF_GRAYED 0x1 namespace SysWin32 { [DllImport("user32.dll", CharSet = CharSet::Unicode)] IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("user32.dll", CharSet = CharSet::Unicode)] Int32 EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable); };

3. Add the following codes for the Form Load event
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { SysWin32::EnableMenuItem(SysWin32::GetSystemMenu(this->Handle, false), SC_CLOSE, MF_GRAYED); }

4. However, the Close button will becoming enabled again during the Form Resize event, so we will have to do the same for the Form Resize event
private: System::Void Form1_Resize(System::Object^ sender, System::EventArgs^ e) { SysWin32::EnableMenuItem(SysWin32::GetSystemMenu(this->Handle, false), SC_CLOSE, MF_GRAYED); }

5. Windows Form with Close button disabled

Windows .Net Framework 2.0 : Semi-transparent Panel

Objectives:
1. Windows Form with background image
2. Semi-transparent Panel overlaying the background image

How?
1. Use Microsoft Paint to create a png image
2. Use Microsoft Photo Editor to edit the png image
3. Use the Set Transparent Color tool to set the Transparency % for a particular color on the image file. This can be done for multiple colors. For example, I have set the pink color to be 100% transparent and the yellow color to be 10% transparent





4. Now we can use this newly created image as background for the Panel
5. Set the BackColor of the Panel to Transparent
6. Windows Form with Semi-transparent Panel