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

Sunday, August 17, 2008

WinCE 6.0 : Loading device drivers programmatically

Windows CE drivers typically are built into the OS image itself. They are packaged into the OS through platform.bib and loaded by device manager during system boot up. Configuration of these drivers are kept in the registry through platform.reg.

However there can be instances when we want to load a driver programmatically. And we can do so using ActivateDeviceEx. In order to use ActivateDeviceEx, we would need to populate the registry with the appropriate device driver settings, example below.

ActivateDeviceEx
----------------

// prepare registry entries for loading device driver
DWORD ret;
HKEY hkResult;
DWORD dwDisposition;

ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
TEXT("Drivers\\BuiltIn\\BTH"),
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL,
&hkResult,
&dwDisposition);

TCHAR prefix[] = TEXT("BTH");
TCHAR dll[] = TEXT("\\Nand Disk\\Bluetooth\\Bth_Drv.dll");
DWORD index = 1;

ret = RegSetValueEx(hkResult,
TEXT("Index"),
0,
REG_DWORD,
(BYTE*)&index,
sizeof(index));
ret = RegSetValueEx(hkResult,
TEXT("Dll"),
0,
REG_SZ,
(BYTE*)dll,
sizeof(dll));
ret = RegSetValueEx(hkResult,
TEXT("Prefix"),
0,
REG_SZ,
(BYTE*)prefix,
sizeof(prefix));

RegCloseKey(hkResult);

// load device driver DLL using ActivateDeviceEx
HANDLE hBthDevice = INVALID_HANDLE_VALUE;

hBthDevice = ActivateDeviceEx(TEXT("Drivers\\BuiltIn\\BTH"), NULL, 0, NULL);

Sunday, August 3, 2008

WinCE 6.0 : Communication between device driver and application

I was doing a stream interface device driver development on Windows CE 6.0 when I came across a requirement that the typical stream interface device driver is unable to provide. What I am missing is a way for the device driver to notify the application of events within the device driver. After some searching in the internet and MSDN, I came across an API, DuplicateHandle, which is able to help me achieve what I needed.

Application
-----------
1. Create an event in the application.

HANDLE g_hALPS_Event;
g_hALPS_Event = CreateEvent(NULL, FALSE, FALSE, NULL);


2. Send the event handle down to the driver using DeviceIoControl call.

DWORD BytesReturned;
DeviceIoControl(g_hBthDll, IOCTL_BTH_REG_ALPS_EVENT, g_hALPS_Event, sizeof(g_hALPS_Event), NULL,
0, &BytesReturned, NULL);


3. Wait for the event object to be signaled.

while(TRUE)
{
WaitForSingleObject(g_hALPS_Event, INFINITE);
}


Device Driver
-------------
1. Declare a device driver side handle.

HANDLE g_hBTH_Event;


2. Define the IOCTL codes.

#define FILE_DEVICE_BTH 12341234
#define REG_ALPS_EVENT 0x000
#define IOCTL_BTH_REG_ALPS_EVENT CTL_CODE(FILE_DEVICE_BTH, REG_ALPS_EVENT, METHOD_BUFFERED,
FILE_ANY_ACCESS)


3. Within the device driver IOControl routine, use DuplicateHandle on the application handle that was passed down.

HANDLE hCallerProc = 0;
HANDLE hCurrentProc = 0;
hCallerProc = GetCallerProcess();
hCurrentProc = GetCurrentProcess();

switch (dwIoControlCode)
{
case IOCTL_BTH_REG_ALPS_EVENT:
g_hBTH_Event = (HANDLE)pBufIn;
DuplicateHandle(hCallerProc, (HANDLE)pBufIn, hCurrentProc, &g_hBTH_Event,
0, FALSE, DUPLICATE_SAME_ACCESS);
break;
}


4. Whenever the driver needs to notify the application, we would use the SetEvent API.

SetEvent(g_hBTH_Event);