想做个圆形的按钮控件,不知道怎么做.有知道的帮个忙详细说说.不胜感激. ( 积分: 100 )

  • 主题发起人 主题发起人 IvoryOfPig
  • 开始时间 开始时间
I

IvoryOfPig

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么没人回答呀?分少了吗?可我就剩这么点分了.
 
可以用圖片做啊~~~
 
创建椭圆形按钮控件程序如下:
type
TRbutton=class(TButton)
private
.......
protected
procedure CreateWnd;override;
........
........
procedure TRbutton.CreateWnd;//在控件一建立就设置
var
hRgn :THandle;
begin
inherited CreateWnd;
hRgn:=CreateEllipticRgn(0,0,Width,Height);//创建一个椭圆剪裁域
SetWindowRgn(Handle,hRgn,True);//将此控件设置为椭圆形
end;
 
有个相当不错的控件1stclass可以实现圆形按钮
 
不行的。这样做的控件很模糊,一点也不清晰。我以前也做过。我现在是想从头自己绘制一个控件。就像Button一样。
 
那ak2005的方法就不错嘛,干吗不用?
 
1stclass可以实现很多种形状,正在使用中 呵呵 很好用
 
楼上说得对 1stclass有实现这样的形状的按钮,而且还有源码的
 
同意ak_2005,使用Windows API函数CreateEllipticRgn,产生其他形状的函数也有。
 
program OwnDraw;

uses
Windows, Messages;

// 窗体过程
function WndProc(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
// 填充三角形
procedure Triangle(hWndDc: HDC; var Pt: array of TPoint);
begin
SelectObject(hWndDc, GetStockObject(BLACK_BRUSH));
Polygon(hWndDc, Pt[0], 3);
SelectObject(hWndDc, GetStockObject(WHITE_BRUSH));
end;
const
ID_SMALLER = 1;
ID_LARGER = 2;
// #define BTN_WIDTH (8 * cxChar)
// #define BTN_HEIGHT (4 * cyChar)
{$J+}
hWndSmaller: Integer = 0;
hWndLarger: Integer = 0;
cxClient: Integer = 0;
cyClient: Integer = 0;
cxChar: Integer = 0;
cyChar: Integer = 0;
{$J-}
var
cx, cy: Integer;
pdis: PDrawItemStruct;
Pt: array[0..2] of TPoint;
Rc: TRect;
begin
Result := 0;
// 区分不同的消息
case Msg of
WM_CREATE: {-- 根据系统字体尺寸建立两个按钮 --}
begin
cxChar := LOWORD(GetDialogBaseUnits());
cyChar := HIWORD(GetDialogBaseUnits());
hWndSmaller := CreateWindow('button', nil,
WS_CHILD or WS_VISIBLE or BS_OWNERDRAW,
0, 0, 8 * cxChar, 4 * cyChar,
hWnd, ID_SMALLER, hInstance, nil);
hWndLarger := CreateWindow('button', nil,
WS_CHILD or WS_VISIBLE or BS_OWNERDRAW,
0, 0, 8 * cxChar, 4 * cyChar,
hWnd, ID_LARGER, hInstance, nil);
end;

WM_SIZE: {-- 根据新的窗体尺寸调整两个按钮 --}
begin
cxClient := LOWORD(lParam);
cyClient := HIWORD(lParam);
MoveWindow(hWndSmaller, cxClient div 2 - 3 * 8 * cxChar div 2,
cyClient div 2 - 4 * cyChar div 2, 8 * cxChar, 4 * cyChar, TRUE);
MoveWindow(hWndLarger, cxClient div 2 + 8 * cxChar div 2,
cyClient div 2 - 4 * cyChar div 2, 8 * cxChar, 4 * cyChar, TRUE);
end;

WM_COMMAND: {-- 根据所按按钮调整主窗体尺寸 --}
begin
GetWindowRect(hWnd, Rc);
case wParam of (* 由于BN_CLICKED=0,所以直接判断. *)
ID_SMALLER:
begin
Inc(Rc.Left, (cxClient div 20));
Dec(Rc.Right, (cxClient div 20));
Inc(Rc.Top, (cyClient div 20));
Dec(Rc.Bottom, (cyClient div 20));
end;

ID_LARGER:
begin
Dec(Rc.Left, (cxClient div 20));
Inc(Rc.Right, (cxClient div 20));
Dec(Rc.Top, (cyClient div 20));
Inc(Rc.Bottom, (cyClient div 20));
end;
end;
MoveWindow(hWnd, Rc.Left, Rc.Top, Rc.Right - Rc.Left, Rc.Bottom - Rc.Top, TRUE);
end;

WM_DRAWITEM: {-- 绘制BS_OWNERDRAW按钮 --}
begin
pdis := PDrawItemStruct(lParam);
// 填充背景
FillRect(pdis.hDC, pdis.rcItem, GetStockObject(WHITE_BRUSH));
// 绘制边框
FrameRect(pdis.hDC, pdis.rcItem, GetStockObject(BLACK_BRUSH));
// 绘制三角
cx := pdis.rcItem.Right - pdis.rcItem.Left;
cy := pdis.rcItem.Bottom - pdis.rcItem.Top;
case pdis.CtlID of
ID_SMALLER:
begin
Pt[0].X := 3 * cx div 8; Pt[0].Y := 1 * cy div 8;
Pt[1].X := 5 * cx div 8; Pt[1].Y := 1 * cy div 8;
Pt[2].X := 4 * cx div 8; Pt[2].Y := 3 * cy div 8;
Triangle (pdis.hDC, Pt);
Pt[0].X := 7 * cx div 8; Pt[0].Y := 3 * cy div 8;
Pt[1].X := 7 * cx div 8; Pt[1].Y := 5 * cy div 8;
Pt[2].X := 5 * cx div 8; Pt[2].Y := 4 * cy div 8;
Triangle (pdis.hDC, Pt);
Pt[0].X := 5 * cx div 8; Pt[0].Y := 7 * cy div 8;
Pt[1].X := 3 * cx div 8; Pt[1].Y := 7 * cy div 8;
Pt[2].X := 4 * cx div 8; Pt[2].Y := 5 * cy div 8;
Triangle (pdis.hDC, Pt);
Pt[0].X := 1 * cx div 8; Pt[0].Y := 5 * cy div 8;
Pt[1].X := 1 * cx div 8; Pt[1].Y := 3 * cy div 8;
Pt[2].X := 3 * cx div 8; Pt[2].Y := 4 * cy div 8;
Triangle (pdis.hDC, Pt);
end;
ID_LARGER:
begin
Pt[0].X := 5 * cx div 8; Pt[0].Y := 3 * cy div 8;
Pt[1].X := 3 * cx div 8; Pt[1].Y := 3 * cy div 8;
Pt[2].X := 4 * cx div 8; Pt[2].Y := 1 * cy div 8;
Triangle(pdis.hDC, Pt);
Pt[0].X := 5 * cx div 8; Pt[0].Y := 5 * cy div 8;
Pt[1].X := 5 * cx div 8; Pt[1].Y := 3 * cy div 8;
Pt[2].X := 7 * cx div 8; Pt[2].Y := 4 * cy div 8;
Triangle(pdis.hDC, Pt);
Pt[0].X := 3 * cx div 8; Pt[0].Y := 5 * cy div 8;
Pt[1].X := 5 * cx div 8; Pt[1].Y := 5 * cy div 8;
Pt[2].X := 4 * cx div 8; Pt[2].Y := 7 * cy div 8;
Triangle(pdis.hDC, pt);
Pt[0].X := 3 * cx div 8 ; Pt[0].Y := 3 * cy div 8;
Pt[1].X := 3 * cx div 8 ; Pt[1].Y := 5 * cy div 8;
Pt[2].X := 1 * cx div 8 ; Pt[2].Y := 4 * cy div 8;
Triangle(pdis.hDC, Pt);
end;
end;
// 反转颜色
if (pdis.itemState and ODS_SELECTED) <> 0 then InvertRect(pdis.hDC, pdis.rcItem);
// 虚线边框
if (pdis.itemState and ODS_FOCUS) <> 0 then
begin
Inc(pdis.rcItem.Left, cx div 16);
Inc(pdis.rcItem.Top, cy div 16);
Dec(pdis.rcItem.Right, cx div 16);
Dec(pdis.rcItem.Bottom, cy div 16);
DrawFocusRect(pdis.hDC, pdis.rcItem);
end;
end;

WM_DESTROY:
PostQuitMessage(0); // 放置WM_QUIT消息

else
Result := DefWindowProc(hWnd, Msg, wParam, lParam); // 标准处理
end;
end;

const
AppName = 'OwnDraw';

var
hWnd: LongWord;
Msg: TMsg;
WndClass: TWndClass;

begin
// 填充结构体
WndClass.style := CS_HREDRAW or CS_VREDRAW;
WndClass.lpfnWndProc := @WndProc;
WndClass.cbClsExtra := 0;
WndClass.cbWndExtra := 0;
WndClass.hInstance := hInstance;
WndClass.hIcon := LoadIcon(0, IDI_APPLICATION);
WndClass.hCursor := LoadCursor(0, IDC_ARROW);
WndClass.hbrBackground := GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName := nil;
WndClass.lpszClassName := AppName;

// 注册窗体类
if (RegisterClass(WndClass) = 0) then
begin
MessageBox(0, 'This program requires Windows NT!', AppName, MB_ICONERROR);
Exit;
end;

// 建立窗体
hWnd := CreateWindow(AppName, 'Owner-Draw Button Demo',
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
0, 0, hInstance, nil);

// 显示窗体
ShowWindow(hWnd, CmdShow);
UpdateWindow(hWnd);

// 消息循环
while GetMessage(Msg, 0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end.
 
多人接受答案了。
 
后退
顶部