Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Last active May 19, 2018 10:51
Show Gist options
  • Save snipsnipsnip/7707017ae8f9b605566f2bacd5b1146a to your computer and use it in GitHub Desktop.
Save snipsnipsnip/7707017ae8f9b605566f2bacd5b1146a to your computer and use it in GitHub Desktop.
MFCの部品をWindows.Forms.Formに埋め込む ref: https://qiita.com/snipsnipsnip/items/19045907ae96121e8310
/** WinFormsのhwndにattachし、newしたcwndを返す */
extern "C" __declspec(dllexport) CWnd* PASCAL attach(HWND hwnd) {
auto w = std::make_unique<CWnd>();
if (!w->Attach(hwnd)) { return nullptr; }
// ... wに好きな部品を置く ...
return w.release();
}
/** attachで作ったcwndをdetachとdeleteする */
extern "C" __declspec(dllexport) void PASCAL detach(Pair *p) {
auto w = std::unique_ptr<CWnd>(cwnd);
w->Detach();
}
public class GuestControl : Control
{
/// <summary>アタッチしたMFCのCWnd</summary>
private IntPtr CWnd { get; }
[DllImport("attach")]
private static extern IntPtr attach(IntPtr hwnd);
[DllImport("attach")]
private static extern void detach(IntPtr hwnd);
public GuestControl()
{
CWnd = attach(Handle);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
detach(CWnd);
}
base.Dispose(disposing);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment