我可以扩展ToolTip并创建我自己的RichToolTip,但我不明白我如何处理每行或每个单词的格式.我可以添加用户可以单击的控件吗?或图像等
我遇到了RichTextBox,但这是用C++(MFC)编写的,我想要一些C#或者我可以用于C#的东西.有什么建议?我可以使用任何开源组件吗?
public class RichToolTip : ToolTip
{
public RichToolTip()
{
this.OwnerDraw = true;
this.Draw += new DrawToolTipEventHandler(OnDraw);
}
public RichToolTip(System.ComponentModel.IContainer Cont)
{
this.OwnerDraw = true;
this.Draw += new DrawToolTipEventHandler(OnDraw);
}
private void OnDraw(object sender, DrawToolTipEventArgs e)
{
DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics,
e.AssociatedWindow, e.AssociatedControl, e.Bounds, e.ToolTipText.Substring(0, 5),
this.BackColor, this.ForeColor, new Font("Arial Unicode MS", 8.25f, FontStyle.Bold));
newArgs.DrawBackground();
newArgs.DrawBorder();
newArgs.DrawText(TextFormatFlags.TextBoxControl);
}
}
Run Code Online (Sandbox Code Playgroud)
我看到了一些围绕这个主题的讨论,并得出结论:这是不可能的。我应该使用线程,将其设为 STA,当我需要返回结果时,将主线程与创建的线程连接起来。这可以工作,但它不是一个理想的解决方案,因为使用委托我可以实现纯异步行为(使用回调)。所以,首先——就在我开始实现我自己的 Future 类(如 Java 中)之前;有没有更好的方法使用委托来实现这一目标?
private delegate String DelegateFoo(String[] input);
private String Foo(String[] input){
// do something with input
// this code need to be STA
// below code throws exception .. that operation is invalid
// Thread.CurrentThread.SetApartmentState(ApartmentState.STA)
return "result";
}
private void callBackFoo(IAsyncResult iar){
AsyncResult result = (AsyncResult)iar;
DelegateFoo del = (DelegateFoo)result.AsyncDelegate;
String result = null;
try{
result = del.EndInvoke(iar);
}catch(Exception e){
return;
}
DelegateAfterFooCallBack callbackDel = new DelegateAfterFooCallBack (AfterFooCallBack);
// call code which should execute in the …Run Code Online (Sandbox Code Playgroud) 参考文献:http: //msdn.microsoft.com/en-us/library/ms171728.aspx http://stackoverflow.com/questions/5408155/how-to-make-delegate-thread-sta
我想创建一个新的线程并使其成为STA因此我无法使用异步委托或BackgroudWorker(如引用链接1中所述)因此我最终创建了一个我自己的线程使其成为STA并附加回调以了解何时任务完成了.代码类似于下面的内容,即使我使用了需要的调用,我仍然会得到InvalidOperationException(偶尔会出现一次)
delegate UpdateEventHander(Object sender, EventArgs e);
class MyTask{
// to generate an event
public event UpdateEventHandler Finished;
public void Start(){
Result = // something that require the thread to be STA.
Finished(this, EventArgs.Empty);
}
public Result GetResult(){
return Result;
}
}
Class Foo : Form{
// It has many UI Controls obviously
public void doSomething(){
MyTask task = new MyTask();
task.Finished += new UpdateEventHander(CompletionHandler);
Thread thread = new Thread(new ThreadStart(task.Start));
thread.setAppartmetnState(AppartmentState.STA);
thread.start();
}
public void …Run Code Online (Sandbox Code Playgroud) 我注意到保存MailItem并释放是一项耗时的任务.那么,做以下事情是安全的吗?(下面的伪代码)
Thread 1 (main thread)
- Open 10 (different .msg files) - MailItems [List<MailItem> items]
- user works on them and want to save and close all of them with one click.
- On_save_All_click (runs on main thread)
- Do
- toBeClearedList.addAll(items);
- items.clear() [so that main thread cannot access those items]
- BG_Thread.ExecuteAsyn(toBeClearedList);
- End
Thread 2 (background thread) (input - List<MailItems>)
- foreach(MailItem item in input)
item.save();
System.Runtime.InteropServices.Marshal.ReleaseComObject(item)
- done
Run Code Online (Sandbox Code Playgroud)
我写了几个测试,看起来像它的工作; 只是想知道这样做是否安全?"在不同于创建它的线程中释放COM对象"
谢谢
Karephul
public void foo(Class<? extends Number> value) {
// compilation error
processNumber(value);
}
public void processNumber(Number num) {
// do something about this number.
}
Run Code Online (Sandbox Code Playgroud)
我想从Number(Integer,Double ..etc)的任何子类型调用"foo".任何人都可以向我解释我该怎么办?
foo(new Integer(5)); // compilation error
Run Code Online (Sandbox Code Playgroud) c# ×4
winforms ×2
com ×1
delegates ×1
generics ×1
java ×1
outlook ×1
richtextbox ×1
threadpool ×1
tooltip ×1