Monday, April 19, 2010

Wake On LAN

General

How can I start a computer over the network?

There are two different procedures, depending on whether the target computer is in standby/sleep/suspend mode or whether it is actually powered down in hibernate mode or entirely off.

Note that you cannot wake a computer that is powered down over a WLAN connection. Only cable connections will do.

Waking from suspend mode

  1. On the target computer in Device Manager or in the network settings select the network card.
  2. Click on: Properties
  3. Click on the configure button of the network card.
  4. Select the tab: Power management
  5. Set a check mark at: Allow this device to bring the computer out of standby
  6. Click on: OK
  7. Continue to close all property pages.

Now your computer should wake up from standby whenever the network card is accessed by any other computer. If it doesn't, check any relevant BIOS settings, but usually there aren't any.

A frequent problem is that the computer is woken up by all and sundry network traffic, hence it may be better to send the computer to hibernate mode or shut it down and off altogether and use the following method.

Wake On LAN from hibernate or off mode

One way to do this is the following.

  1. Check whether the network adapter on the target computer supports Wake On LAN. Enable the function in the network adapter settings or in any special management program that came with it. If the network adapter is integrated on the motherboard, enable Wake On LAN in the motherboard BIOS.
  2. Download and install WakeOnLanGui from www.depicus.com.
  3. On the target computer, i.e. the one to be woken up, open a command line window and enter the command: ipconfig /all
  4. Note the physical address (sometimes called MAC = Media Access Control address) of your Ethernet adapter from the ipconfig output. Example: 00-E0-18-90-A5-44
  5. Run WakeOnLanGui on the computer from which you want to wake the other computer and type the physical address into the proper field in WakeOnLanGui. The other two fields are filled with 255.255.255.255, and the port is 7 or actually any other port. Port 9 is sometimes used.
  6. Shut down the computer to be waked.
  7. Test WakeOnLanGui.

If you need the opposite function as well, to shut down a computer remotely, have a look at:

http://www.microsoft.com/technet/sysinternals/utilities/PsShutdown.mspx

This program, PsShutdown, is a member of Sysinternals' PsTools group of freeware utilities.

To shut down and power down a remote computer named "computername", enter the command:

psshutdown -k -f -t 00 \\computername

-k indicates shutdown and powerdown, -f means force all running programs to close without asking, and -t 00 means don't wait (wait zero seconds) until shutting down the computer named computername. (Don't miss the required space between -t and 00.) This utility can do several other things as well, like sending the target computer into hibernate mode or rebooting it.

Wake On LAN over the Internet

One example of how to wake up a home or office computer across the Internet is the following.

Assume a home network with a WAN router, in this case a DrayTek Vigor 2500We with firmware version 2.54. The main problem is that you cannot use any of the usual methods to address the target computer. Since it is off, it has no IP address. False advice is widespread, because people keep forgetting this simple fact.

The router has to send a special broadcast packet into the Local Area Network, using the MAC (Media Access Control) address, rather than the IP address. With some routers you may be able to achieve this by sending a Wake-On-LAN packet to the router and forward the port to an internal broadcast address, for example: 192.168.0.255

With most routers, however, this still does not work. You have to rely on the router to have the special ability to forward or send a Wake-On-LAN broadcast packet. In the case of the DrayTek router this can only be achieved through the telnet interface, but unfortunately not yet through the Web interface. An example for the telnet command for this DrayTek router is:

wol up 00E01890A545

Of course you have to replace the hex number with the MAC address of your target computer, and for this particular router you cannot intersperse hyphens as usual. The router needs the address in the direct short hexadecimal format. Check your router's manual or help system for its specifics.


Wake On Lan Command Line

Wake on Lan Command Line

http://www.depicus.com/wake-on-lan/wake-on-lan-cmd.aspx

The command line tool wolcmd is run from a dos prompt. The syntax is as follows:

wolcmd [mac address] [ip address] [subnet mask] [port number]

you can also do a local broadcast if you are running on an internal network thus

wolcmd [macaddress] 255.255.255.255 255.255.255.255

where mac address is the unique address of the network card. To find the mac address of a card you can either open the computer and read the address on the sticker on the card or much easier is, if you are using TCP/IP, ping the machine from a dos prompt (ping [machine name or ip number] i.e. ping delphi or ping 10.43.43.43) then issue an arp command (arp -a) which will list the ip number and physical address of the card.

If the port number is not specified it will default to port 7

Running from a batch file

Simply add multiple entries thus:

wolcmd 009027a324fe 195.188.159.20 255.255.255.0 8900
wolcmd 009024a731da 212.86.64.31 255.255.255.0 8900

To run the batch file, say half an hour before everybody gets into work, use the AT command on a Windows NT computer.

The AT command schedules commands and programs to run on a computer at a specified time and date. The Schedule service must be running to use the AT command. The syntax is as follows:

AT [\\computername] [ [id] [/DELETE] | /DELETE [/YES]]
AT [\\computername] time [/INTERACTIVE]
[ /EVERY:date[,...] | /NEXT:date[,...]] "command"


\\computername Specifies a remote computer. Commands are scheduled on local computer if this parameter is omitted.
id Is an identification number assigned to a scheduled command.
/delete Cancels a scheduled command. If id is omitted, all the scheduled commands on the computer are cancelled.
/yes Used with cancel all jobs command when no further confirmation is desired.
time Specifies the time when command is to run.
/interactive Allows the job to interact with the desktop of the use who is logged on at the time the job runs.
/every:date[,...] Runs the command on each specified day(s) of the week month. If date is omitted, the current day of the month is assumed.
/next:date[,...] Runs the specified command on the next occurrence of the day (for example, next Thursday). If date is omitted, current day of the month is assumed.
"command" Is the Windows NT command, or batch program to be run.


Example

wolcmd 00:1D:7D:22:DC:F0 192.168.1.86 255.255.255.128

wolcmd 001D7D22DCF0 192.168.1.86 255.255.255.128


Tuesday, January 12, 2010

Keyboard Listener in WPF

Key board message Hooking in WPF, using C++/CLI

KeyboardListener.h
==================
#pragma once

using namespace System;
using namespace System::Diagnostics;
using namespace System::Runtime::InteropServices;
using namespace System::Runtime::CompilerServices;
using namespace System::Windows::Input;
using namespace System::Windows::Interop;

public ref class CKeyboardListener : System::IDisposable
{

public:
CKeyboardListener(void);
~CKeyboardListener(void);

public:
delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
static LowLevelKeyboardProc^ m_pLowLevelKeyboardProc;

public:
void DisposeHook(void);
static IntPtr SetHook( LowLevelKeyboardProc^ pLowLevelKeyboardProc );
static IntPtr GetWindowHandle(System::Windows::Window^ window );
static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam);

private:
static const int WH_KEYBOARD_LL = 13;
static const int WM_KEYDOWN = 0x0100;
static System::IntPtr m_hookID = IntPtr::Zero;

public :
[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static IntPtr SetWindowsHookEx(int idHook,LowLevelKeyboardProc^ lpfn, System::IntPtr hMod, unsigned int dwThreadId);

[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
//[return: MarshalAs(UnmanagedType::Bool)]
static bool UnhookWindowsHookEx(System::IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static System::IntPtr CallNextHookEx(System::IntPtr hhk, int nCode,
System::IntPtr wParam, System::IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet::Auto, SetLastError = true)]
static IntPtr GetModuleHandle(System::String^ lpModuleName);

[ DllImport("user32.dll") ]
static IntPtr GetForegroundWindow();
};

KeyboardListener.cpp
================
#include "KeyboardListener.h"
#include "MainApp.h"

CKeyboardListener::CKeyboardListener(void)
{
m_pLowLevelKeyboardProc = gcnew LowLevelKeyboardProc( &CKeyboardListener::HookCallback);
m_hookID = SetHook( m_pLowLevelKeyboardProc );
}

CKeyboardListener::~CKeyboardListener(void)
{

}

void CKeyboardListener::DisposeHook(void)
{
UnhookWindowsHookEx( m_hookID );
}

IntPtr CKeyboardListener::SetHook(LowLevelKeyboardProc^ pLowLevelKeyboardProc)
{
Process^ curProcess = nullptr;
ProcessModule^ curModule = nullptr;

while( curProcess = Process::GetCurrentProcess() )
{
while( curModule = curProcess->MainModule )
{
return SetWindowsHookEx(WH_KEYBOARD_LL, pLowLevelKeyboardProc,
GetModuleHandle(curModule->ModuleName), 0);
}
}

return System::IntPtr::Zero;
}

IntPtr CKeyboardListener::GetWindowHandle(System::Windows::Window^ window )
{
System::Windows::Interop::WindowInteropHelper^ pWindow;
pWindow = gcnew System::Windows::Interop::WindowInteropHelper(window);
return pWindow->Handle;
}

IntPtr CKeyboardListener::HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
int l_nWindowCnt = 0;
System::IntPtr l_hForeGndWnd = GetForegroundWindow();
MainApp^ l_pCurApp = (MainApp^) Application::Current;
int l_nVirtualKeyCode = 0;

l_nVirtualKeyCode = Marshal::ReadInt32(lParam);

if ( l_nVirtualKeyCode == 112 ) //If F1 key
{
for each (Window^ hWindow in l_pCurApp->Windows)
{
++l_nWindowCnt;

System::IntPtr l_hWindow = GetWindowHandle(hWindow);

if ( l_hForeGndWnd == l_hWindow )
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
((MainScreen^)l_pCurApp->mainwnd)->LoadHelp(); //Call the LoadHelp function from MainScreen class
return CallNextHookEx( m_hookID, nCode, wParam, lParam);
}
}
}
}

return System::IntPtr::Zero;
}


And in MainApp.cpp we create an instance of the class "CKeyboardListener"

void MainApp::OnStartup( System::Windows::StartupEventArgs^ e )
{
Application::OnStartup(e);
mainwnd = gcnew MainScreen(ResourceAssembly->Location);
m_pKeyboardListener = gcnew CKeyboardListener(); //Start the key board hooking

mainwnd->Show();
}

void MainApp::OnExit( System::Windows::ExitEventArgs^ e )
{
m_pKeyboardListener->DisposeHook();
m_pKeyboardListener = nullptr;
}



Friday, December 11, 2009

Custom Command Button in WPF

Hi,
Here I create custom WPF command button using C++/CLI.

//.h file
#pragma once

using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;

ref class MyButton: public System::Windows::Controls::Button
{
public:
MavisButton();
~MavisButton();
void SetControl();
};

//.cpp file

#include "MyButton.h"

MyButton::MyButton()
{
this->SetControl();
}

MyButton::~MyButton()
{

}

void MyButton::SetControl()
{
System::Windows::Controls::ControlTemplate^ pControlTemplate;

System::Drawing::ColorConverter^ pColorConverter;
System::Windows::Media::RadialGradientBrush^ pGradient_HoverGlassBackgroundFill;
System::Windows::Media::LinearGradientBrush^ pHighlightFill;
System::Windows::Media::LinearGradientBrush^ pBorderStroke;

System::Windows::Setter^ pSetter;
System::Windows::Style^ pStyle;

System::Windows::Trigger^ tg_IsMouseOver;
System::Windows::Trigger^ tg_IsPressed;

System::Windows::Setter^ str_IsPressed;
System::Windows::Setter^ str_IsMouseOver;


//-----------------------------------------------GlassBackgroundFill-------------------------------------------------------------

pColorConverter = gcnew System::Drawing::ColorConverter();
System::Windows::Media::LinearGradientBrush^ gradient_GlassBackgroundFill = gcnew System::Windows::Media::LinearGradientBrush();
gradient_GlassBackgroundFill->StartPoint = System::Windows::Point(0, 0);
gradient_GlassBackgroundFill->EndPoint = System::Windows::Point(0, 1);
gradient_GlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FF4B58" )), 0) );
gradient_GlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#A91E28" )), 1) );

//--------------------------------------------------HoverGlassBackgroundFill-------------------------------------------------------------

pGradient_HoverGlassBackgroundFill = gcnew System::Windows::Media::RadialGradientBrush();
pGradient_HoverGlassBackgroundFill->Center = System::Windows::Point(0.5, 0.5);

pGradient_HoverGlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#a91e28" )), 0 ));
pGradient_HoverGlassBackgroundFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#ff4b58" )), 1 ));

//-------------------------------------------HighlightFill------------------------------------------------------------

pHighlightFill = gcnew System::Windows::Media::LinearGradientBrush();
pHighlightFill->StartPoint = System::Windows::Point(0, 0);
pHighlightFill->EndPoint = System::Windows::Point(0, 1);

pHighlightFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFFFFF" )), 0 ));
pHighlightFill->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#00000000" )), 1 ));

//-----------------------------------------------------BorderStroke-------------------------------------------------------
pBorderStroke = gcnew System::Windows::Media::LinearGradientBrush();
pBorderStroke->StartPoint = System::Windows::Point(0, 0);
pBorderStroke->EndPoint = System::Windows::Point(0, 1);

pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#c7c7c7" )), 0 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#b0b0b0" )), 1 ));

//--------------------------------------------------------------------------------------------------

pControlTemplate = gcnew System::Windows::Controls::ControlTemplate();
pControlTemplate->TargetType = (gcnew System::Windows::Controls::Button)->GetType();

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryGrid = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Grid)->GetType() );
pFrameworkElementFactoryGrid->Name ="Grid";
pFrameworkElementFactoryGrid->SetValue( System::Windows::Controls::Grid::RenderTransformOriginProperty, System::Windows::Point(0.5, 0.5));

//--------------------------------------------------------------------------------------------------------------

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBorder = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Border)->GetType() );
pFrameworkElementFactoryBorder->Name = "Outline";
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::CornerRadiusProperty, gcnew System::Windows::CornerRadius(12.0));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BackgroundProperty, System::Windows::Media::Brushes::Red);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderBrushProperty, pBorderStroke ) ;
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderThicknessProperty, gcnew System::Windows::Thickness(1));

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBackground = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Shapes::Rectangle)->GetType() );
pFrameworkElementFactoryBackground->Name = "GlassBackground";
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::MarginProperty , System::Windows::Thickness(1, 1, 2, 2));
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::RadiusXProperty, 9.0 );
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::RadiusYProperty, 9.0 ) ;
pFrameworkElementFactoryBackground->SetValue( System::Windows::Shapes::Rectangle::FillProperty, gradient_GlassBackgroundFill );

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryHighlight = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Shapes::Rectangle)->GetType() );
pFrameworkElementFactoryHighlight->Name = "Highlight";
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::MarginProperty , System::Windows::Thickness(1, 1, 2, 2));
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::RadiusXProperty, 9.0 );
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::RadiusYProperty, 9.0 ) ;
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::OpacityProperty, 1.0 ) ;
pFrameworkElementFactoryHighlight->SetValue( System::Windows::Shapes::Rectangle::FillProperty, pHighlightFill );

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryContentPresenter = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::ContentPresenter)->GetType() );
pFrameworkElementFactoryContentPresenter->SetValue ( System::Windows::Controls::ContentPresenter::VerticalAlignmentProperty, System::Windows::VerticalAlignment::Center );
pFrameworkElementFactoryContentPresenter->SetValue ( System::Windows::Controls::ContentPresenter::HorizontalAlignmentProperty, System::Windows::HorizontalAlignment::Center );

pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryBorder );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryBackground );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryHighlight );
pFrameworkElementFactoryGrid->AppendChild( pFrameworkElementFactoryContentPresenter );

pControlTemplate->VisualTree = pFrameworkElementFactoryGrid;


//------------------------------------------------ControlTemplate->Triggers->IsMouseOver---------------------------------------------------------

tg_IsMouseOver = gcnew System::Windows::Trigger();
tg_IsMouseOver->Property = System::Windows::UIElement::IsMouseOverProperty;
tg_IsMouseOver->Value = true;

str_IsMouseOver = gcnew System::Windows::Setter();
str_IsMouseOver->TargetName = "GlassBackground";
str_IsMouseOver->Property = System::Windows::Shapes::Rectangle::FillProperty;
str_IsMouseOver->Value = pGradient_HoverGlassBackgroundFill;
tg_IsMouseOver->Setters->Add( str_IsMouseOver );

pControlTemplate->Triggers->Add ( tg_IsMouseOver );

//------------------------------------------------ControlTemplate.Triggers.IsPressed---------------------------------------------------------

tg_IsPressed = gcnew System::Windows::Trigger();
tg_IsPressed->Property = System::Windows::Controls::Button::IsPressedProperty;
tg_IsPressed->Value = true;

str_IsPressed = gcnew System::Windows::Setter();
str_IsPressed->TargetName = "GlassBackground";
str_IsPressed->Property = System::Windows::Shapes::Rectangle::FillProperty;
str_IsPressed->Value = System::Windows::Media::Brushes::Red;
tg_IsPressed->Setters->Add( str_IsPressed );

pControlTemplate->Triggers->Add ( tg_IsPressed );

this->Template = pControlTemplate;

//Style
pStyle = gcnew System::Windows::Style();
pStyle->TargetType = (gcnew System::Windows::Controls::Button)->GetType();

//Margin
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::MarginProperty, gcnew System::Windows::Thickness(1.0));
pStyle->Setters->Add( pSetter );

//SnapsToDevicePixelsProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::SnapsToDevicePixelsProperty, true);
pStyle->Setters->Add( pSetter );

//OverridesDefaultStyleProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::OverridesDefaultStyleProperty, true);
pStyle->Setters->Add( pSetter );

//MinHeightProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::MinHeightProperty, 16.0 );
pStyle->Setters->Add( pSetter );

//MinWidthProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::MinWidthProperty, 16.0 );
pStyle->Setters->Add( pSetter );

//FontFamilyProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::FontFamilyProperty, gcnew System::Windows::Media::FontFamily("Verdana") );
pStyle->Setters->Add( pSetter );

//FontSizeProperty
System::Windows::FontSizeConverter^ pFontSizeConverter = gcnew System::Windows::FontSizeConverter();
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::FontSizeProperty, (double)pFontSizeConverter->ConvertFromString("11px"));
pStyle->Setters->Add( pSetter );

//ForegroundProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Button::ForegroundProperty, System::Windows::Media::Brushes::White);
pStyle->Setters->Add( pSetter );

//TemplateProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Control::TemplateProperty, pControlTemplate );
pStyle->Setters->Add( pSetter );

this->Style = pStyle;

}



How to use this contorl in your application

MavisButton^ m_pOkButton = gcnew MavisButton();
m_pOkButton->Width = 80;
m_pOkButton->Height = 25;
m_pOkButton->Content = "OK";
m_pOkButton->Click += gcnew System::Windows::RoutedEventHandler( this, &AboutScreen::OKButton_Click );
Canvas::SetTop( m_pOkButton, 360 );
Canvas::SetLeft( m_pOkButton, 260 );

m_MainCanvas->Children->Add ( m_pOkButton );

Custom ToolTip in WPF

Hi,

I create a static class for set a tooltip style in VC++, using WPF and C++/CLI. Using this class we can set the custom tooltip style for our WPF application.

Eg:

System::Windows::Controls::ToolTip^ pToolTip = gcnew System::Windows::Controls::ToolTip();

pToolTip->Style = mvdata::CToolTipStyle::ToopTipStyle( "Tool tip Text" );
System::Windows::Controls::YourControl->ToolTip = pToolTip;
//pSubMenuItem->ToolTip = pToolTip;


#pragma once
#include "stdafx.h"

//Class for SetToolTip Style

namespace myTooltipStyle
{
public ref class CToolTipStyle
{
private:
static CToolTipStyle()
{

}

public:
static System::Windows::Style^ ToopTipStyle(System::String^ pToolTipText)
{
System::Windows::Style^ pToopTipStyle;
System::Windows::Setter^ pSetter;

System::Windows::Controls::ControlTemplate^ pControlTemplate;
System::Windows::Media::LinearGradientBrush^ pBorderStroke;
System::Windows::Media::LinearGradientBrush^ pBackground;

//-----------------------------------------------------Background-------------------------------------------------------
pBackground = gcnew System::Windows::Media::LinearGradientBrush();
pBackground->StartPoint = System::Windows::Point(0.5, 0);
pBackground->EndPoint = System::Windows::Point(0.5, 1);

pBackground->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#CF181818" )), 0 ));
pBackground->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#BE1C1C1C" )), 1 ));

//-----------------------------------------------------BorderStroke-------------------------------------------------------
pBorderStroke = gcnew System::Windows::Media::LinearGradientBrush();
pBorderStroke->StartPoint = System::Windows::Point(0.5, 0);
pBorderStroke->EndPoint = System::Windows::Point(0.5, 1);

pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop(( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#80FFFFFF" )), 0 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#7FFFFFFF" )), 1 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFF18D" )), 0.344 ));
pBorderStroke->GradientStops->Add( gcnew System::Windows::Media::GradientStop( ( System::Windows::Media::Color)( System::ComponentModel::TypeDescriptor::GetConverter( System::Windows::Media::Color::typeid )->ConvertFromString( "#FFFFF4AB" )), 0.647 ));

//--------------------------------------------------------------------------------------------------

pControlTemplate = gcnew System::Windows::Controls::ControlTemplate();
pControlTemplate->TargetType = (gcnew System::Windows::Controls::ToolTip)->GetType();

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryBorder = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::Border)->GetType() );
pFrameworkElementFactoryBorder->Name = "Outline";
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::CornerRadiusProperty, gcnew System::Windows::CornerRadius(7.0));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::HorizontalAlignmentProperty, System::Windows::HorizontalAlignment::Center);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::VerticalAlignmentProperty, System::Windows::VerticalAlignment::Top);
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::PaddingProperty, System::Windows::Thickness(5));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderThicknessProperty, gcnew System::Windows::Thickness(3));
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BackgroundProperty, pBackground );
pFrameworkElementFactoryBorder->SetValue( System::Windows::Controls::Border::BorderBrushProperty, pBorderStroke ) ;

System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryStackPanal = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::StackPanel)->GetType() );
pFrameworkElementFactoryStackPanal->Name = "StackPanal";


System::Windows::FrameworkElementFactory^ pFrameworkElementFactoryTextBlock = gcnew System::Windows::FrameworkElementFactory( (gcnew System::Windows::Controls::TextBlock)->GetType() );
pFrameworkElementFactoryStackPanal->Name = "TextBlock";
pFrameworkElementFactoryTextBlock->SetValue( System::Windows::Controls::TextBlock::ForegroundProperty, System::Windows::Media::Brushes::White);
pFrameworkElementFactoryTextBlock->SetValue( System::Windows::Controls::TextBlock::TextProperty, pToolTipText );

pFrameworkElementFactoryStackPanal->AppendChild( pFrameworkElementFactoryTextBlock );
pFrameworkElementFactoryBorder->AppendChild( pFrameworkElementFactoryStackPanal );

pControlTemplate->VisualTree = pFrameworkElementFactoryBorder;


pToopTipStyle = gcnew System::Windows::Style();
pToopTipStyle->TargetType = (gcnew System::Windows::Controls::ToolTip)->GetType();


//OverridesDefaultStyleProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::ToolTip::OverridesDefaultStyleProperty, true);
pToopTipStyle->Setters->Add( pSetter );


//HasDropShadowProperty
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::ToolTip::HasDropShadowProperty, true);
pToopTipStyle->Setters->Add( pSetter );

//Template
pSetter = gcnew System::Windows::Setter( System::Windows::Controls::Control::TemplateProperty, pControlTemplate );
pToopTipStyle->Setters->Add( pSetter );


return pToopTipStyle;

}
};

}

Thursday, June 26, 2008

Multicolumn treeview in C++/CLI using WPF

This is a sample code. You can modify it and try to create the controls as dynamically. It display a tree like this.
=====================
- Nod1.1 Node1.2
Node1..1 Node 1.2.2
- Node2.1 Node 2.2
Node2.1.1 Node 2.2.2
=====================



using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;


ref class FirstWindow : Window
{
Canvas^ maincanvas;

TextBox^ textbox_a1;
TextBox^ textbox_a2;

TextBox^ textbox_b1;
TextBox^ textbox_b2;


TextBox^ textbox_c1;
TextBox^ textbox_c2;

TextBox^ textbox_d1;
TextBox^ textbox_d2;


TreeView ^g_TreeView;

TreeViewItem^ g_TreeViewItem;
TreeViewItem^ g_TreeViewItem1;
TreeViewItem^ g_TreeViewItem2;

Grid^ g_Grid_a1;
ColumnDefinition^ g_ColumnDefinition_a1; ColumnDefinition^ g_ColumnDefinition_a2;
RowDefinition^ g_RowDefinitions_a1; RowDefinition^ g_RowDefinitions_a2;

Grid^ g_Grid_b1;
ColumnDefinition^ g_ColumnDefinition_b1; ColumnDefinition^ g_ColumnDefinition_b2;
RowDefinition^ g_RowDefinitions_b1; RowDefinition^ g_RowDefinitions_b2;


Grid^ g_Grid_c1;
ColumnDefinition^ g_ColumnDefinition_c1; ColumnDefinition^ g_ColumnDefinition_c2;
RowDefinition^ g_RowDefinitions_c1; RowDefinition^ g_RowDefinitions_c2;

Grid^ g_Grid_d1;
ColumnDefinition^ g_ColumnDefinition_d1; ColumnDefinition^ g_ColumnDefinition_d2;
RowDefinition^ g_RowDefinitions_d1; RowDefinition^ g_RowDefinitions_d2;


public:
FirstWindow(void)
{
Title = "Multi column TreeView ";
Width = 400;
Height = 400;
ResizeMode = System::Windows::ResizeMode::NoResize;

InitControls();
}
void InitControls(void)
{

textbox_a1 = gcnew TextBox();
textbox_a1->Width = 50;
textbox_a1->Height = 20;
textbox_a1->Text ="Node 1";

textbox_a2 = gcnew TextBox();
textbox_a2->Width = 50;
textbox_a2->Height = 20;
textbox_a2->Text ="god";


textbox_b1 = gcnew TextBox();
textbox_b1->Width = 50;
textbox_b1->Height = 20;
textbox_b1->Text ="Node 1";

textbox_b2 = gcnew TextBox();
textbox_b2->Width = 50;
textbox_b2->Height = 20;
textbox_b2->Text ="god";


textbox_c1 = gcnew TextBox();
textbox_c1->Width = 50;
textbox_c1->Height = 20;
textbox_c1->Text ="Node 1";

textbox_c2 = gcnew TextBox();
textbox_c2->Width = 50;
textbox_c2->Height = 20;
textbox_c2->Text ="god";

textbox_d1 = gcnew TextBox();
textbox_d1->Width = 50;
textbox_d1->Height = 20;
textbox_d1->Text ="Node 1";

textbox_d2 = gcnew TextBox();
textbox_d2->Width = 50;
textbox_d2->Height = 20;
textbox_d2->Text ="god";



g_ColumnDefinition_a1 = gcnew ColumnDefinition();
g_RowDefinitions_a1 = gcnew RowDefinition();

g_ColumnDefinition_a2 = gcnew ColumnDefinition();
g_RowDefinitions_a2 = gcnew RowDefinition();

g_ColumnDefinition_b1 = gcnew ColumnDefinition();
g_RowDefinitions_b1 = gcnew RowDefinition();

g_ColumnDefinition_b2 = gcnew ColumnDefinition();
g_RowDefinitions_b2 = gcnew RowDefinition();

g_ColumnDefinition_c1 = gcnew ColumnDefinition();
g_ColumnDefinition_c2 = gcnew ColumnDefinition();

g_RowDefinitions_c1 = gcnew RowDefinition();
g_RowDefinitions_c2 = gcnew RowDefinition();


g_ColumnDefinition_d1 = gcnew ColumnDefinition();
g_ColumnDefinition_d2 = gcnew ColumnDefinition();
g_RowDefinitions_d1 = gcnew RowDefinition();
g_RowDefinitions_d2 = gcnew RowDefinition();



g_Grid_a1 = gcnew Grid();
g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a1);
g_Grid_a1->ColumnDefinitions->Add(g_ColumnDefinition_a1);
g_Grid_a1->ColumnDefinitions->Add(g_ColumnDefinition_a2);
g_Grid_a1->ShowGridLines=true;
g_Grid_a1->Height=20;
g_Grid_a1->Width =100;
g_Grid_a1->SetColumn(textbox_a1,0);
g_Grid_a1->SetColumn(textbox_a2,2);
g_Grid_a1->Children->Add(textbox_a1);
g_Grid_a1->Children->Add(textbox_a2);



g_Grid_b1 = gcnew Grid();
g_Grid_b1->RowDefinitions->Add(g_RowDefinitions_b1);
g_Grid_b1->ColumnDefinitions->Add(g_ColumnDefinition_b1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_b1->ColumnDefinitions->Add(g_ColumnDefinition_b2);
g_Grid_b1->ShowGridLines=true;
g_Grid_b1->Height=20;
g_Grid_b1->Width =100;
g_Grid_b1->SetColumn(textbox_b1,0);
g_Grid_b1->SetColumn(textbox_b2,2);
g_Grid_b1->Children->Add(textbox_b1);
g_Grid_b1->Children->Add(textbox_b2);





g_Grid_c1 = gcnew Grid();
g_Grid_c1->RowDefinitions->Add(g_RowDefinitions_c1);
g_Grid_c1->ColumnDefinitions->Add(g_ColumnDefinition_c1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_c1->ColumnDefinitions->Add(g_ColumnDefinition_c2);
g_Grid_c1->ShowGridLines=true;
g_Grid_c1->Height=20;
g_Grid_c1->Width =100;
g_Grid_c1->SetColumn(textbox_c1,0);
g_Grid_c1->SetColumn(textbox_c2,2);
g_Grid_c1->Children->Add(textbox_c1);
g_Grid_c1->Children->Add(textbox_c2);


g_Grid_d1 = gcnew Grid();
g_Grid_d1->RowDefinitions->Add(g_RowDefinitions_d1);
g_Grid_d1->ColumnDefinitions->Add(g_ColumnDefinition_d1);
// g_Grid_a1->RowDefinitions->Add(g_RowDefinitions_a2);
g_Grid_d1->ColumnDefinitions->Add(g_ColumnDefinition_d2);
g_Grid_d1->ShowGridLines=true;
g_Grid_d1->Height=20;
g_Grid_d1->Width =100;
g_Grid_d1->SetColumn(textbox_d1,0);
g_Grid_d1->SetColumn(textbox_d2,2);
g_Grid_d1->Children->Add(textbox_d1);
g_Grid_d1->Children->Add(textbox_d2);

g_TreeView = gcnew TreeView();
g_TreeView->Width= 300;
g_TreeView->Height = 200;

g_TreeViewItem1 = gcnew TreeViewItem();
g_TreeViewItem1->Header = g_Grid_b1;
//g_TreeViewItem1->Items->Add("Sub Sub Node God");
g_TreeViewItem1->Items->Add(g_Grid_c1);


g_TreeViewItem = gcnew TreeViewItem();
//g_TreeViewItem->Header = "root";
g_TreeViewItem->Header = g_Grid_d1;
g_TreeViewItem->Items->Add(g_Grid_a1);
g_TreeViewItem->Items->Add(g_TreeViewItem1);
g_TreeView->Items->Add(g_TreeViewItem);
Canvas::SetTop(g_TreeView,10);
Canvas::SetLeft(g_TreeView,10);


maincanvas = gcnew Canvas();
maincanvas->Children->Add(g_TreeView);
Content = maincanvas;
}
};

Friday, June 22, 2007

Count occurence of character (i.e. line breaks) in a MySQL field

Unfortunately, there is no inbuilt MySQL function to count the occurance of a character in string. By counting the number of characters in the original string, then temporarily 'deleting' the character you want to count and counting the string again, subtracting the first count from the second, you get the number of occurances:

SELECT LENGTH('foobarfoobarfoobar') - LENGTH(REPLACE('foobarfoobarfoobar', 'b', '')) AS `occurances`
--> 3

Obviously, in this example 'b' is the string you want to count the number of occurances of.

You can use the same principle to count the number of lines in a field, by adding one to the difference:

Thursday, June 21, 2007

Connecting C / C++ with MYSQL

This code help you to create a connection with C/C++ and MYSQL

Create a header " .h" file and write these steps in that file

#include "/usr/include/mysql/mysql.h"
#include
using namespace std;

MYSQL mysql;
MYSQL_RES *Result;
MYSQL_ROW FirstTblRow, SecTblRow, ThrdTblRow, TblField ;

int C_Connect_MYSQL( char *hostname, char *username, char *passwd, char *dbname )
{
mysql_init(&mysql);

if(!mysql_real_connect(&mysql, hostname, username, passwd, dbname, 0, NULL, 0))
{
printf("%d: %s \n", mysql_errno(&mysql), mysql_error(&mysql));
printf("Connection Refused by MYSQL\n");
return 1;
}
else{
printf("Connection OK by MYSQL\n");
return 0;
}
}

MYSQL_RES * Exectue_Query ( char *Exec_Qry )
{

if(mysql_query(&mysql, Exec_Qry)) {
cout << Exec_Qry << endl;
cout << "Error in Query!"< }
else {
Result = mysql_store_result(&mysql);
}
return Result;
}


MYSQL_RES * MySqlListField ( char *TableName )
{
Result = mysql_list_fields ( &mysql, TableName, NULL );
return Result;
}



After this create the ".c" file and write the following steps in main() function

char *Qry[] = "select * from YOUR_TABLE_NAME;";

int ret = C_Connect_MYSQL( MY_HOSTNAME, MY_USERNAME, MY_PASSWORD, MY_DATABASE);

if ( ret == 1 ) exit(0);

FirstTableName = Exectue_Query( Qry );

if ( mysql_num_rows(FirstTableName) > 0 ) {

while( (FirstTblRow = mysql_fetch_row( FirstTableName )))
{
cout << TblField[1] ? TblField[1] : "NULL" << endl;
}
}