A
active
Guess I'm looking for someone who likes to work difficult puzzles.
I can't seem to ever retrieve a palette handle from the clipboard.
Below is a simple test program that demonstrates the problem.
The program uses windows api to get a handle to a palette from the
clipboard.
The palette needs to be put there by another program - maybe Photoshop.
The returned handle is always zero.
Is Windows really capable of using the clipboard to pass a palette?
The crux of the code is this:
Private Sub button2_Click(ByVal sender As ...
Const CF_PALETTE As Integer = 9
If OpenClipboard(Me.Handle) Then
If IsClipboardFormatAvailable(CF_PALETTE) Then
Hpal = GetClipboardData(CF_PALETTE)
If Hpal.ToInt32 = 0 Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("GetClipboardData exited with Hpal=0 ...snip
To run the below code simply replace the code in a form with it and run the
form
Imports System.Drawing.Imaging
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents button1 As System.Windows.Forms.Button
Private WithEvents button2 As System.Windows.Forms.Button
Private WithEvents button3 As System.Windows.Forms.Button
Private components As System.ComponentModel.IContainer
Private Hpal As IntPtr ' HPALETTE
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Public Declare Auto Function GetClipboardData Lib "user32.dll" (ByVal
wFormat As Integer) As IntPtr
Public Declare Auto Function IsClipboardFormatAvailable Lib "user32.dll"
(ByVal wFormat As Integer) As Boolean
Public Declare Auto Function OpenClipboard Lib "user32.dll" (ByVal hWnd As
IntPtr) As Boolean
Public Declare Auto Function CloseClipboard Lib "user32.dll" () As Boolean
Public Sub New()
InitializeComponent()
End Sub 'New
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.button1 = New System.Windows.Forms.Button
Me.button2 = New System.Windows.Forms.Button
Me.button3 = New System.Windows.Forms.Button
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'button1
'
Me.button1.Location = New System.Drawing.Point(186, 124)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(130, 24)
Me.button1.TabIndex = 2
Me.button1.Text = "Copy File Palette"
Me.ToolTip1.SetToolTip(Me.button1, "Copy Palette from a GIF file to the
clipboard")
'
'button2
'
Me.button2.Location = New System.Drawing.Point(13, 124)
Me.button2.Name = "button2"
Me.button2.Size = New System.Drawing.Size(156, 24)
Me.button2.TabIndex = 2
Me.button2.Text = "Retrieve Clipboard Palette"
Me.ToolTip1.SetToolTip(Me.button2, "Retrieve a palette from the clipboard")
'
'button3
'
Me.button3.Location = New System.Drawing.Point(88, 167)
Me.button3.Name = "button3"
Me.button3.Size = New System.Drawing.Size(130, 24)
Me.button3.TabIndex = 2
Me.button3.Text = "Exit"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(12, 24)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(304, 47)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = "Palette can be placed on clipboard by using the 'Save
Palette"" button or by some " & _
"external program such as Photoshop"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(13, 85)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(302, 20)
Me.TextBox2.TabIndex = 4
'
'Timer1
'
Me.Timer1.Enabled = True
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(328, 204)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.button1)
Me.Controls.Add(Me.button2)
Me.Controls.Add(Me.button3)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub 'InitializeComponent
#End Region
<STAThread()> Shared Sub Main()
Application.Run(New Form1)
End Sub 'Main
Private Sub button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button1.Click
'Place a Palette on the clipboard
'TO DO
End Sub 'button1_Click
Private Sub button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button2.Click
Const CF_PALETTE As Integer = 9
If OpenClipboard(Me.Handle) Then
If IsClipboardFormatAvailable(CF_PALETTE) Then
Hpal = GetClipboardData(CF_PALETTE)
If Hpal.ToInt32 = 0 Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("GetClipboardData exited with Hpal=0 and error code=" &
ex.NativeErrorCode, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Dim NoErr As Boolean = CloseClipboard()
If Not NoErr Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("Close Clipboard exited with error code=" &
ex.NativeErrorCode, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show("No Palette on Clipboard")
Dim NoErr As Boolean = CloseClipboard()
If Not NoErr Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("Close Clipboard exited with error code=" &
ex.NativeErrorCode, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
Else
Dim exc As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("Open Clipboard exited with error code=" &
exc.NativeErrorCode, exc.Message, MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
End Sub 'button2_Click
Private Sub button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button3.Click
Application.Exit()
End Sub 'button3_Click
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
Dim DataO As DataObject = CType(Clipboard.GetDataObject(), DataObject)
If DataO.GetDataPresent(DataFormats.Palette, False) Then
Dim cp As ColorPalette = CType(DataO.GetData(DataFormats.Palette),
ColorPalette)
TextBox2.Text = "There is a Palette on the Clipboard"
Else
TextBox2.Text = "The Clipboard does not contain a Palette"
End If
End Sub
End Class 'Form1
I can't seem to ever retrieve a palette handle from the clipboard.
Below is a simple test program that demonstrates the problem.
The program uses windows api to get a handle to a palette from the
clipboard.
The palette needs to be put there by another program - maybe Photoshop.
The returned handle is always zero.
Is Windows really capable of using the clipboard to pass a palette?
The crux of the code is this:
Private Sub button2_Click(ByVal sender As ...
Const CF_PALETTE As Integer = 9
If OpenClipboard(Me.Handle) Then
If IsClipboardFormatAvailable(CF_PALETTE) Then
Hpal = GetClipboardData(CF_PALETTE)
If Hpal.ToInt32 = 0 Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("GetClipboardData exited with Hpal=0 ...snip
To run the below code simply replace the code in a form with it and run the
form
Imports System.Drawing.Imaging
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Private WithEvents button1 As System.Windows.Forms.Button
Private WithEvents button2 As System.Windows.Forms.Button
Private WithEvents button3 As System.Windows.Forms.Button
Private components As System.ComponentModel.IContainer
Private Hpal As IntPtr ' HPALETTE
Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Timer1 As System.Windows.Forms.Timer
Public Declare Auto Function GetClipboardData Lib "user32.dll" (ByVal
wFormat As Integer) As IntPtr
Public Declare Auto Function IsClipboardFormatAvailable Lib "user32.dll"
(ByVal wFormat As Integer) As Boolean
Public Declare Auto Function OpenClipboard Lib "user32.dll" (ByVal hWnd As
IntPtr) As Boolean
Public Declare Auto Function CloseClipboard Lib "user32.dll" () As Boolean
Public Sub New()
InitializeComponent()
End Sub 'New
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.button1 = New System.Windows.Forms.Button
Me.button2 = New System.Windows.Forms.Button
Me.button3 = New System.Windows.Forms.Button
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'button1
'
Me.button1.Location = New System.Drawing.Point(186, 124)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(130, 24)
Me.button1.TabIndex = 2
Me.button1.Text = "Copy File Palette"
Me.ToolTip1.SetToolTip(Me.button1, "Copy Palette from a GIF file to the
clipboard")
'
'button2
'
Me.button2.Location = New System.Drawing.Point(13, 124)
Me.button2.Name = "button2"
Me.button2.Size = New System.Drawing.Size(156, 24)
Me.button2.TabIndex = 2
Me.button2.Text = "Retrieve Clipboard Palette"
Me.ToolTip1.SetToolTip(Me.button2, "Retrieve a palette from the clipboard")
'
'button3
'
Me.button3.Location = New System.Drawing.Point(88, 167)
Me.button3.Name = "button3"
Me.button3.Size = New System.Drawing.Size(130, 24)
Me.button3.TabIndex = 2
Me.button3.Text = "Exit"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(12, 24)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(304, 47)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = "Palette can be placed on clipboard by using the 'Save
Palette"" button or by some " & _
"external program such as Photoshop"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(13, 85)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(302, 20)
Me.TextBox2.TabIndex = 4
'
'Timer1
'
Me.Timer1.Enabled = True
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(328, 204)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.button1)
Me.Controls.Add(Me.button2)
Me.Controls.Add(Me.button3)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub 'InitializeComponent
#End Region
<STAThread()> Shared Sub Main()
Application.Run(New Form1)
End Sub 'Main
Private Sub button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button1.Click
'Place a Palette on the clipboard
'TO DO
End Sub 'button1_Click
Private Sub button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button2.Click
Const CF_PALETTE As Integer = 9
If OpenClipboard(Me.Handle) Then
If IsClipboardFormatAvailable(CF_PALETTE) Then
Hpal = GetClipboardData(CF_PALETTE)
If Hpal.ToInt32 = 0 Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("GetClipboardData exited with Hpal=0 and error code=" &
ex.NativeErrorCode, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Dim NoErr As Boolean = CloseClipboard()
If Not NoErr Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("Close Clipboard exited with error code=" &
ex.NativeErrorCode, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show("No Palette on Clipboard")
Dim NoErr As Boolean = CloseClipboard()
If Not NoErr Then
Dim ex As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("Close Clipboard exited with error code=" &
ex.NativeErrorCode, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End If
Else
Dim exc As Win32Exception = New Win32Exception(Marshal.GetLastWin32Error())
MessageBox.Show("Open Clipboard exited with error code=" &
exc.NativeErrorCode, exc.Message, MessageBoxButtons.OK,
MessageBoxIcon.Error)
End If
End Sub 'button2_Click
Private Sub button3_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles button3.Click
Application.Exit()
End Sub 'button3_Click
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Timer1.Tick
Dim DataO As DataObject = CType(Clipboard.GetDataObject(), DataObject)
If DataO.GetDataPresent(DataFormats.Palette, False) Then
Dim cp As ColorPalette = CType(DataO.GetData(DataFormats.Palette),
ColorPalette)
TextBox2.Text = "There is a Palette on the Clipboard"
Else
TextBox2.Text = "The Clipboard does not contain a Palette"
End If
End Sub
End Class 'Form1