SelectObject not supported exception

  • Thread starter Thread starter Miguel Angel Morales
  • Start date Start date
M

Miguel Angel Morales

I made a program on Compact Framework beta version, and
now I've installed the release version but the program
doesn't work.
I got a not supported exception in SelectObject Call.
Does anybody know how to solve it?
Thank you very much indeed.
 
Could you give us some more information. What are you trying to do? Do you
have a code snippet? The information you are giving now is not enough to
help you further.
 
Of course I can give you all the information you need to
help me (thank you in advance).

This is the code snippet where my application fails:
----------------------------------------
private byte[] createLogicFont(byte sizeLetter)
{
ArrayList logfont = new ArrayList();

logfont.Add ((byte)sizeLetter);//Height
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //Width
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfEscapement Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfOrientation Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)145);//lfWeight 400 Normal
logfont.Add ((byte)1); //3
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //lfItalic 0 false
logfont.Add ((byte)0); //lfUnderline 0 false

logfont.Add ((byte)0); //lfStrikeOut 0 false

logfont.Add ((byte)0); //

logfont.Add ((byte)255);//lfCharSet 255

logfont.Add ((byte)0);//lfOutPrecision 0

logfont.Add ((byte)0);//fClipPrecision 0

logfont.Add ((byte)0);//lfQuality 0

logfont.Add ((byte)0); //lfPitchAndFamily 0

//lfFaceName
logfont.Add ((byte)67); //C
logfont.Add ((byte)111); //o
logfont.Add ((byte)117); //u
logfont.Add ((byte)114); //r
logfont.Add ((byte)105); //i
logfont.Add ((byte)101); //e
logfont.Add ((byte)114); //r
logfont.Add ((byte)0);

byte[] exit= (byte[])logfont.ToArray(typeof
(byte));
return exit;
}


private void changefont(IntPtr hdc,byte sizefont)
{
byte[] LogicFont = createLogicFont(sizefont);
IntPtr font = CreateFontIndirect(LogicFont);

try {
SelectObject(hdc,font);

} catch (Exception e) {
MessageBox.Show (e.ToString ());
MessageBox.Show (e.Message );
}
}
----------------------------------------

The function "changefont" contains the "SelectObject"
call that fails. I checked (debugging) that before that
call, the handle "hdc" and the "font" are valid.
Thank you very much for your interest.
 
Hi, Miguel

Unfortunately, I cannot find a reference to SelectObject
in the .NET Framework documentation. It has apparently
been removed.

Flynn
-----Original Message-----
Of course I can give you all the information you need to
help me (thank you in advance).

This is the code snippet where my application fails:
----------------------------------------
private byte[] createLogicFont(byte sizeLetter)
{
ArrayList logfont = new ArrayList();

logfont.Add ((byte)sizeLetter);//Height
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //Width
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfEscapement Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfOrientation Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)145);//lfWeight 400 Normal
logfont.Add ((byte)1); //3
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //lfItalic 0 false
logfont.Add ((byte)0); //lfUnderline 0 false

logfont.Add ((byte)0); //lfStrikeOut 0 false

logfont.Add ((byte)0); //

logfont.Add ((byte)255);//lfCharSet 255

logfont.Add ((byte)0);//lfOutPrecision 0

logfont.Add ((byte)0);//fClipPrecision 0

logfont.Add ((byte)0);//lfQuality 0

logfont.Add ((byte)0); //lfPitchAndFamily 0

//lfFaceName
logfont.Add ((byte)67); //C
logfont.Add ((byte)111); //o
logfont.Add ((byte)117); //u
logfont.Add ((byte)114); //r
logfont.Add ((byte)105); //i
logfont.Add ((byte)101); //e
logfont.Add ((byte)114); //r
logfont.Add ((byte)0);

byte[] exit= (byte[])logfont.ToArray(typeof
(byte));
return exit;
}


private void changefont(IntPtr hdc,byte sizefont)
{
byte[] LogicFont = createLogicFont(sizefont);
IntPtr font = CreateFontIndirect(LogicFont);

try {
SelectObject(hdc,font);

} catch (Exception e) {
MessageBox.Show (e.ToString ());
MessageBox.Show (e.Message );
}
}
----------------------------------------

The function "changefont" contains the "SelectObject"
call that fails. I checked (debugging) that before that
call, the handle "hdc" and the "font" are valid.
Thank you very much for your interest.
-----Original Message-----
Could you give us some more information. What are you trying to do? Do you
have a code snippet? The information you are giving now is not enough to
help you further.

--
Regards,

Maarten Struys, eMVP
PTS Software bv
.
 
Miguel,

I assume that you P/Invoke to both CreateFontIndirect and SelectObject,
right? The code you gave us here does not have any particular problem that
would cause it to generate a NotSupported exception. However, you have some
problems in your LogicFont creation. First off, between lfStrikeOut and
lfCharSet you have an extra byte that is not supposed to be there. The other
problem I saw in your font is in the lfFaceName. You have to pass the name
of the font in Unicode. So this is how the createLogFont function should
look like:

private byte[] createLogicFont(byte sizeLetter)
{
ArrayList logfont = new ArrayList();

logfont.Add ((byte)sizeLetter);//Height
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //Width
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfEscapement Rotation (900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfOrientation Rotation (900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)145);//lfWeight 400 Normal
logfont.Add ((byte)1); //3
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //lfItalic 0 false
logfont.Add ((byte)0); //lfUnderline 0 false
logfont.Add ((byte)0); //lfStrikeOut 0 false
logfont.Add ((byte)255);//lfCharSet 255
logfont.Add ((byte)0);//lfOutPrecision 0
logfont.Add ((byte)0);//fClipPrecision 0
logfont.Add ((byte)0);//lfQuality 0
logfont.Add ((byte)0); //lfPitchAndFamily 0

//lfFaceName
logfont.Add ((byte)67); //C (67)
logfont.Add ((byte)0);
logfont.Add ((byte)111); //o (111)
logfont.Add ((byte)0);
logfont.Add ((byte)117); //u (117)
logfont.Add ((byte)0);
logfont.Add ((byte)114); //r (114)
logfont.Add ((byte)0);
logfont.Add ((byte)105); //i (105)
logfont.Add ((byte)0);
logfont.Add ((byte)101); //e (101)
logfont.Add ((byte)0);
logfont.Add ((byte)114); //r (114)
logfont.Add ((byte)0);
logfont.Add ((byte)32);
logfont.Add ((byte)0);
logfont.Add ((byte)78); //N (78)
logfont.Add ((byte)0);
logfont.Add ((byte)101); //e (101)
logfont.Add ((byte)0);
logfont.Add ((byte)119); //w (119)
logfont.Add ((byte)0);
logfont.Add ((byte)0);

byte[] exit= (byte[])logfont.ToArray(typeof(byte));
return exit;
}


Oh, I used a Courier New font to test, because I don't have Courier on my
device. I didn't change anything else in your code. Here are the P/Invoke
declarations that I used:


[DllImport("coredll.dll"]
internal static extern IntPtr CreateFontIndirect(byte[] lf);

[DllImport("coredll.dll"]
internal static extern IntPtr SelectObject(IntPtr hDC, IntPtr hGdiObj);

[DllImport("coredll.dll"]
internal static extern IntPtr GetCapture();

[DllImport("coredll.dll"]
internal static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("coredll.dll")]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("coredll.dll", EntryPoint="ExtTextOutW", SetLastError=true)]
internal static extern short TextOut(IntPtr hDC, short nXStart, short
nYStart, ushort nOptions, IntPtr rect, string lpString, ushort cbString,
IntPtr lpDx);


This is how I got a DC to the form (I tested writing some text immediately
on the form):


private void Form1_Load(object sender, System.EventArgs e)
{
this.Capture = true;

hWnd = Win32Window.GetCapture();
hDC = Core.GetDC(hWnd);

this.Capture = false;
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Core.ReleaseDC(hWnd, hDC);
}


I realize this is bad practise, keeping a DC during the life time of my app,
but it was just a simple test. Finally this is how I painted some
information in a new font on my form:


private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
string str = "Here is some text";
this.changefont(hDC, 15);
TextOut(hDC, 50, 140, 0, IntPtr.Zero, str, (ushort)str.Length,
IntPtr.Zero);
}


I hope this will help you further.

--
Regards,

Maarten Struys, eMVP
PTS Software bv
----
message Of course I can give you all the information you need to
help me (thank you in advance).

This is the code snippet where my application fails:
----------------------------------------
private byte[] createLogicFont(byte sizeLetter)
{
ArrayList logfont = new ArrayList();

logfont.Add ((byte)sizeLetter);//Height
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //Width
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfEscapement Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfOrientation Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)145);//lfWeight 400 Normal
logfont.Add ((byte)1); //3
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //lfItalic 0 false
logfont.Add ((byte)0); //lfUnderline 0 false

logfont.Add ((byte)0); //lfStrikeOut 0 false

logfont.Add ((byte)0); //

logfont.Add ((byte)255);//lfCharSet 255

logfont.Add ((byte)0);//lfOutPrecision 0

logfont.Add ((byte)0);//fClipPrecision 0

logfont.Add ((byte)0);//lfQuality 0

logfont.Add ((byte)0); //lfPitchAndFamily 0

//lfFaceName
logfont.Add ((byte)67); //C
logfont.Add ((byte)111); //o
logfont.Add ((byte)117); //u
logfont.Add ((byte)114); //r
logfont.Add ((byte)105); //i
logfont.Add ((byte)101); //e
logfont.Add ((byte)114); //r
logfont.Add ((byte)0);

byte[] exit= (byte[])logfont.ToArray(typeof
(byte));
return exit;
}


private void changefont(IntPtr hdc,byte sizefont)
{
byte[] LogicFont = createLogicFont(sizefont);
IntPtr font = CreateFontIndirect(LogicFont);

try {
SelectObject(hdc,font);

} catch (Exception e) {
MessageBox.Show (e.ToString ());
MessageBox.Show (e.Message );
}
}
----------------------------------------

The function "changefont" contains the "SelectObject"
call that fails. I checked (debugging) that before that
call, the handle "hdc" and the "font" are valid.
Thank you very much for your interest.
 
I found the problem:
The definition of SelectObject fot Beta version was:

public static extern object SelectObject(IntPtr hdc,
IntPtr objetoGDI);

and I have changed it to:

public static extern IntPtr SelectObject(IntPtr hdc,
IntPtr objetoGDI);

Thank you, again.
-----Original Message-----
Miguel,

I assume that you P/Invoke to both CreateFontIndirect and SelectObject,
right? The code you gave us here does not have any particular problem that
would cause it to generate a NotSupported exception. However, you have some
problems in your LogicFont creation. First off, between lfStrikeOut and
lfCharSet you have an extra byte that is not supposed to be there. The other
problem I saw in your font is in the lfFaceName. You have to pass the name
of the font in Unicode. So this is how the createLogFont function should
look like:

private byte[] createLogicFont(byte sizeLetter)
{
ArrayList logfont = new ArrayList();

logfont.Add ((byte)sizeLetter);//Height
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //Width
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfEscapement Rotation (900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfOrientation Rotation (900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)145);//lfWeight 400 Normal
logfont.Add ((byte)1); //3
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //lfItalic 0 false
logfont.Add ((byte)0); //lfUnderline 0 false
logfont.Add ((byte)0); //lfStrikeOut 0 false
logfont.Add ((byte)255);//lfCharSet 255
logfont.Add ((byte)0);//lfOutPrecision 0
logfont.Add ((byte)0);//fClipPrecision 0
logfont.Add ((byte)0);//lfQuality 0
logfont.Add ((byte)0); //lfPitchAndFamily 0

//lfFaceName
logfont.Add ((byte)67); //C (67)
logfont.Add ((byte)0);
logfont.Add ((byte)111); //o (111)
logfont.Add ((byte)0);
logfont.Add ((byte)117); //u (117)
logfont.Add ((byte)0);
logfont.Add ((byte)114); //r (114)
logfont.Add ((byte)0);
logfont.Add ((byte)105); //i (105)
logfont.Add ((byte)0);
logfont.Add ((byte)101); //e (101)
logfont.Add ((byte)0);
logfont.Add ((byte)114); //r (114)
logfont.Add ((byte)0);
logfont.Add ((byte)32);
logfont.Add ((byte)0);
logfont.Add ((byte)78); //N (78)
logfont.Add ((byte)0);
logfont.Add ((byte)101); //e (101)
logfont.Add ((byte)0);
logfont.Add ((byte)119); //w (119)
logfont.Add ((byte)0);
logfont.Add ((byte)0);

byte[] exit= (byte[])logfont.ToArray(typeof(byte));
return exit;
}


Oh, I used a Courier New font to test, because I don't have Courier on my
device. I didn't change anything else in your code. Here are the P/Invoke
declarations that I used:


[DllImport("coredll.dll"]
internal static extern IntPtr CreateFontIndirect(byte[] lf);

[DllImport("coredll.dll"]
internal static extern IntPtr SelectObject(IntPtr hDC, IntPtr hGdiObj);

[DllImport("coredll.dll"]
internal static extern IntPtr GetCapture();

[DllImport("coredll.dll"]
internal static extern IntPtr GetDC(IntPtr hWnd);

[DllImport("coredll.dll")]
internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);

[DllImport("coredll.dll", EntryPoint="ExtTextOutW", SetLastError=true)]
internal static extern short TextOut(IntPtr hDC, short nXStart, short
nYStart, ushort nOptions, IntPtr rect, string lpString, ushort cbString,
IntPtr lpDx);


This is how I got a DC to the form (I tested writing some text immediately
on the form):


private void Form1_Load(object sender, System.EventArgs e)
{
this.Capture = true;

hWnd = Win32Window.GetCapture();
hDC = Core.GetDC(hWnd);

this.Capture = false;
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
Core.ReleaseDC(hWnd, hDC);
}


I realize this is bad practise, keeping a DC during the life time of my app,
but it was just a simple test. Finally this is how I painted some
information in a new font on my form:


private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
string str = "Here is some text";
this.changefont(hDC, 15);
TextOut(hDC, 50, 140, 0, IntPtr.Zero, str, (ushort) str.Length,
IntPtr.Zero);
}


I hope this will help you further.

--
Regards,

Maarten Struys, eMVP
PTS Software bv
message Of course I can give you all the information you need to
help me (thank you in advance).

This is the code snippet where my application fails:
----------------------------------------
private byte[] createLogicFont(byte sizeLetter)
{
ArrayList logfont = new ArrayList();

logfont.Add ((byte)sizeLetter);//Height
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //Width
logfont.Add ((byte)0);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfEscapement Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)132); //lfOrientation Rotation
(900) 90º
logfont.Add ((byte)3);
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)145);//lfWeight 400 Normal
logfont.Add ((byte)1); //3
logfont.Add ((byte)0);
logfont.Add ((byte)0);

logfont.Add ((byte)0); //lfItalic 0 false
logfont.Add ((byte)0); //lfUnderline 0 false

logfont.Add ((byte)0); //lfStrikeOut 0 false

logfont.Add ((byte)0); //

logfont.Add ((byte)255);//lfCharSet 255

logfont.Add ((byte)0);//lfOutPrecision 0

logfont.Add ((byte)0);//fClipPrecision 0

logfont.Add ((byte)0);//lfQuality 0

logfont.Add ((byte)0); //lfPitchAndFamily 0

//lfFaceName
logfont.Add ((byte)67); //C
logfont.Add ((byte)111); //o
logfont.Add ((byte)117); //u
logfont.Add ((byte)114); //r
logfont.Add ((byte)105); //i
logfont.Add ((byte)101); //e
logfont.Add ((byte)114); //r
logfont.Add ((byte)0);

byte[] exit= (byte[])logfont.ToArray(typeof
(byte));
return exit;
}


private void changefont(IntPtr hdc,byte sizefont)
{
byte[] LogicFont = createLogicFont(sizefont);
IntPtr font = CreateFontIndirect(LogicFont);

try {
SelectObject(hdc,font);

} catch (Exception e) {
MessageBox.Show (e.ToString ());
MessageBox.Show (e.Message );
}
}
----------------------------------------

The function "changefont" contains the "SelectObject"
call that fails. I checked (debugging) that before that
call, the handle "hdc" and the "font" are valid.
Thank you very much for your interest.
-----Original Message-----
Could you give us some more information. What are you trying to do? Do you
have a code snippet? The information you are giving now is not enough to
help you further.

--
Regards,

Maarten Struys, eMVP
PTS Software bv


.
 
Back
Top