java swt 设置图标_java swt TeeItem 的setimage无法让图标出现在标题前面,图标不出现,请问怎么样ti添加前面的图标啊?...

importorg.eclipse.swt.SWT;importorg.eclipse.swt.events.SelectionAdapter;importorg.eclipse.swt.events.SelectionEvent;importorg.eclipse.swt.layout.FillLayout;importorg.ecli...

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.layout.FillLayout;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

import org.eclipse.swt.widgets.Tree;

import org.eclipse.swt.widgets.TreeItem;

import org.eclipse.wb.swt.ResourceManager;

public class TreeExample {

public static void main(String[] args) {

Display display = new Display ();

Shell shell = new Shell (display);

shell.setText("Tree Example");

shell.setBounds(100, 100, 200, 200);

shell.setLayout(new FillLayout());

final Tree tree = new Tree (shell, SWT.SINGLE);

for (int i = 1; i < 4; i++) {

TreeItem grandParent = new TreeItem (tree, 0);

grandParent.setText ("Grand Parent - " + i);

grandParent.setImage(ResourceManager.getPluginImage("MyProjectName", "icons/1.jpg"));//这里是添加标题前面的图标的代码

for (int j = 1; j < 4; j++) {

TreeItem parent = new TreeItem (grandParent, 0);

parent.setText ("Parent - " + j);

for (int k = 1; k < 4; k++) {

TreeItem child = new TreeItem (parent, 0);

child.setText ("Child - " + k);

}

}

}

tree.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent event) {

TreeItem[] selected = tree.getSelection();

if (selected.length > 0)

System.out.println("Selected: " + selected[0].getText());

}

});

shell.open();

while (!shell.isDisposed ()) {

if (!display.readAndDispatch ()) display.sleep ();

}

display.dispose ();

}

}

代码没有问题,只是不知道怎么在节点前面添加图标,因为像我代码那样添加图标无法显示,我也不知道为什么,请高手详细指点,本人是初学者。

展开

阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果你是在 Windows 平台上使用 C++ 编程语言,可以使用消息框函数 `MessageBox` 来创建一个弹框。要改变消息框中的图标颜色,你需要使用自定义消息框。以下是一个使用自定义消息框的示例代码,其中包括更改消息框图标颜色的代码: ```c++ //定义一个自定义消息框函数 int CustomMessageBox(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType, COLORREF iconColor) { HINSTANCE hInstance = GetModuleHandle(NULL); HICON hIcon = LoadIcon(hInstance, IDI_APPLICATION); HBRUSH hBrush = CreateSolidBrush(iconColor); //创建画刷,用于填充图标区域 int result = MessageBox(hWnd, lpText, lpCaption, uType); //获取消息框句柄 HWND hMsgBox = FindWindow(NULL, lpCaption); if (hMsgBox != NULL) { //获取消息框中图标句柄 HWND hIconBox = GetDlgItem(hMsgBox, 0x2); if (hIconBox != NULL) { //获取图标的位置和大小 RECT rc; GetWindowRect(hIconBox, &rc); int width = rc.right - rc.left; int height = rc.bottom - rc.top; //创建自定义图标 HDC hDC = GetDC(hIconBox); HDC hMemDC = CreateCompatibleDC(hDC); HBITMAP hBitmap = CreateCompatibleBitmap(hDC, width, height); HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); SelectObject(hMemDC, hBrush); Rectangle(hMemDC, 0, 0, width, height); IconDraw(hMemDC, hIcon, 0, 0, width, height, LR_DEFAULTCOLOR); SelectObject(hMemDC, hOldBitmap); ReleaseDC(hIconBox, hDC); DeleteDC(hMemDC); //设置自定义图标 SendMessage(hIconBox, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hBitmap); } } DeleteObject(hBrush); return result; } //使用自定义消息框函数 int main() { CustomMessageBox(NULL, L"Hello, World!", L"My Caption", MB_OK, RGB(255, 0, 0)); return 0; } ``` 在上面的代码中,我们首先定义了一个名为 `CustomMessageBox` 的自定义消息框函数。该函数的前四个参数与 `MessageBox` 函数相同,第五个参数 `iconColor` 是用于指定图标颜色的 RGB 值。在函数内部,我们首先调用 `MessageBox` 函数来创建一个消息框,并保存返回值(即用户点击的按钮)。然后,我们使用 `FindWindow` 函数查找消息框的句柄,然后使用 `GetDlgItem` 函数获取消息框中的图标句柄。接下来,我们获取图标的位置和大小,然后创建一个自定义的位图,使用 `IconDraw` 函数将原始图标绘制到自定义位图中,并使用 `SendMessage` 函数将自定义图标设置为消息框中的图标。最后,我们删除创建的画刷和位图,并返回 `MessageBox` 函数的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大梦想程序商店

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值