View previous topic :: View next topic |
Author |
Message |
tim
Joined: 24 Jul 2005 Posts: 26
|
Posted: Mon Jul 25, 2005 6:03 am Post subject: 3rd party controls |
|
|
I've never programmed using the Win32 API before, but since DFL encapsulates it, I've dived right into it. Here is my first control - a Label-subclass that automatically resizes itself to fit its text. Not sure if resources are handled properly, but it seems to work:
Code: |
import dfl.all;
import dfl.winapi;
import std.string;
import std.stdio;
class AutoLabel: Label
{
void text(char[] value)
{
if(value != Label.text) {
Label.text = value;
SIZE size;
HDC dc = GetDC(handle);
SelectObject(dc, font.handle);
Graphics g = new Graphics(dc);
GetTextExtentPoint32A(g.handle, toStringz(value), value.length, &size);
g.dispose();
ReleaseDC(handle, dc);
setBounds(left, top, size.cx, size.cy);
}
}
}
class MainForm: Form
{
AutoLabel label;
bool toggled = false;
this()
{
text = "AutoLabel demo";
setBounds(left, top, 600, 100);
label = new AutoLabel();
label.backColor = Color(0, 255, 0, 0);
label.font = new Font("Arial", 16.0);
label.parent = this;
toggle();
}
private void toggle()
{
toggled = !toggled;
if(toggled)
label.text = "This is short.";
else
label.text = "This is much longer. Don't you see?";
}
protected void onClick(EventArgs args)
{
toggle();
}
}
int main()
{
Application.run(new MainForm);
return 0;
}
|
I noticed the Win32 API differentiates between Unicode and ASCII functions. It seems DFL uses ASCII (char[] instead of wchar[]), maybe it should allow conditional compilation and support Unicode some day. Anyway, this calls the ASCII functions, which makes this control Win 9x-compatible. |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Jul 25, 2005 12:39 pm Post subject: Re: 3rd party controls |
|
|
tim wrote: | maybe it should allow conditional compilation and support Unicode some day. |
It does; take a peek in utf.d. The default action is to decide at runtime, so it supports both simultaneously, but there are compilation versions to make it static. It will convert D's char[], which is UTF-8, a form of Unicode, to the native string, either ANSI, or UTF-16 (wchar*). This is probably what the MS layer for unicode does.
Note that the Windows 'A' means ANSI, not ASCII.
About your code, you actually don't need to use any Win32 API for that. Control has createGraphics() and Graphics has measureString().
Also, Label will probably have an autoSize property in the future.
If you make any controls you'd like to share, consider posting to http://wiki.dprogramming.com/Dfl/AddOns |
|
Back to top |
|
|
Chris Miller
Joined: 27 Mar 2004 Posts: 514 Location: The Internet
|
Posted: Mon Jul 25, 2005 1:13 pm Post subject: Re: 3rd party controls |
|
|
Graphics has measureText(), rather. I'm reserving measureString() for a possible future feature using GDI+. |
|
Back to top |
|
|
tim
Joined: 24 Jul 2005 Posts: 26
|
Posted: Mon Jul 25, 2005 1:14 pm Post subject: |
|
|
Of course, ANSI. The runtime detection sounds very interesting! I really need to find a better source code editor that allows me to browse the DFL sources more easily.
I don't mind calling Win32 functionality directly, that of course doesn't make the code especially portable, but right now I'm more interested in exploring the Windows API.
Maybe the DFL docs should be available in a .CHM file ... |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|