WindowInteropHelper Not Returning Handle in Powershell Script
i trying create wpf splash screen windows powershell use when installing software. trying hide close button on form
because don't want give user ability close form. have read not wpf functionality , must done through windows api there several c# examples no powershell ones. bolded lines script starts fall apart not getting handle form. appreciated.
thanks
add-type -assemblyname presentationframework
$getwindowlong = @'
[dllimport("user32.dll", entrypoint="getwindowlong")]
public static extern intptr getwindowlongptr32(
intptr hwnd,
int nindex);
'@
$getwindowlongptr = @'
[dllimport("user32.dll", entrypoint="getwindowlongptr")]
public static extern intptr getwindowlongptr64(
intptr hwnd,
int nindex);
'@
$setwindowlong = @'
[dllimport("user32.dll", entrypoint="setwindowlong")]
public static extern int setwindowlong32(
handleref hwnd,
int nindex,
int dwnewlong);
'@
$setwindowlongptr = @'
[dllimport("user32.dll", entrypoint="setwindowlongptr")]
private static extern intptr setwindowlongptr64(
handleref hwnd,
int nindex,
intptr dwnewlong);
'@
function getwindowlongptr([intptr]$hwnd,$nindex)
{
if ([intptr]::size -eq 8)
{
#for 64 bit os's
$fgetwindowlongptr64 = add-type -memberdefinition $getwindowlong -name "win32utils1" -namespace "getwindowlong64" -passthru
return $fgetwindowlongptr64::getwindowlongptr64($hwnd, $nindex);
}
else
{
# 32 bit os's
$fgetwindowlongptr32 = add-type -memberdefinition $getwindowlong -name "win32utils2" -namespace "getwindowlong32" -passthru
return $fgetwindowlongptr32::getwindowlongptr32($hwnd, $nindex);
}
}
function setwindowlongptr( [intptr]$hwnd, $nindex, $dwnewlong)
{
if ([intptr]::size -eq 8)
{
#for 64 bit os's
$fsetwindowlong64 = add-type -memberdefinition $setwindowlong -name "win32utils3" -namespace "setwindowlong64" -passthru
return $fsetwindowlong64::setwindowlongptr64($hwnd, $nindex,$dwnewlong);
}
else
{
# 32 bit os's
$fsetwindowlong32 = add-type -memberdefinition $setwindowlong -name "win32utils4" -namespace "setwindowlong32" -passthru
return $fsetwindowlong32::setwindowlong32($hwnd, $nindex,$dwnewlong);
}
}
function eventhandler_formload()
{
$gwl_style = -16
$ws_sysmenu = 0x80000
set-variable gwl_style -option readonly
set-variable ws_sysmenu -option readonly
#here think having trouble handle returning 0
$myhandlehelper = new-object system.windows.interop.windowinterophelper($form)
$hwnd = $myhandlehelper.handle
$test = getwindowlongptr($hwnd,$gwl_style)
setwindowlongptr($hwnd,$gwl_style, $ws_sysmenu)
}
# ugly splash screen moment
[xml]$xaml = @'
<window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:name="window"
title="mainwindow"
width="500" height="150" topmost="true">
</window>
'@
$reader=(new-object system.xml.xmlnodereader $xaml)
$form=[windows.markup.xamlreader]::load( $reader )
$handler_load = eventhandler_formload
$form_loaded =$form.add_loaded($handler_load)
$form.showdialog() | out-null
add-type –assemblyname presentationframework add-type –assemblyname presentationcore add-type –assemblyname windowsbase $code = @" [dllimport("user32.dll", setlasterror = true)] public static extern int getwindowlong(intptr hwnd, int nindex); [dllimport("user32.dll")] public static extern int setwindowlong(intptr hwnd, int nindex, int dwnewlong); "@ add-type -memberdefinition $code -name win32util -namespace system $gwl_style = -16 $ws_sysmenu = 0x80000 # ugly splash screen moment [xml]$xaml = @' <window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:name="window" title="mainwindow" width="500" height="150" topmost="true"> </window> '@ $reader=(new-object system.xml.xmlnodereader $xaml) $form=[windows.markup.xamlreader]::load($reader) $form_loaded =$form.add_loaded({ $hwnd = (new-object system.windows.interop.windowinterophelper($this)).handle [system.win32util]::setwindowlong($hwnd, $gwl_style, ([system.win32util]::getwindowlong($hwnd, $gwl_style)-band -bnot $ws_sysmenu )) }) $form.showdialog() | out-null
Windows Server > Windows PowerShell
Comments
Post a Comment