tricks

 

Introduction

The following are some tricks and tips that I have explored across my projects. Just I want to share it with you all. May be you are all well known about something or all things in this article. But, just I am looking for beginners to take advantage of this article.

Drag & Drop option for your application

  • Add WS_EX_ACCEPTFILES style to your application’s extended styles.
  • Add WM_DROPFILES message handler to your application.
void CYourDialog::OnDropFiles(HDROP hDropInfo) 
{
    CString sFile;
    char *s=sFile.GetBufferSetLength(255);
    // Get number of files
    int numFiles=DragQueryFile(hDropInfo,0xFFFFFFFF,s,255); 
    sFile.ReleaseBuffer(); 
    for(int i=0;i<numFiles;i++)
    {
        s=sFile.GetBufferSetLength(255);
        DragQueryFile(hDropInfo,i,s,255);  // Get filename using the index
        sFile.ReleaseBuffer();
        MessageBox(sFile);
    }    
CDialog::OnDropFiles(hDropInfo);
}

Limiting MFC application to one instance

Put the following code in OnInitInstance().

BOOL bFound=FALSE;
HANDLE hMutexOneInstance =  CreateMutex(NULL,TRUE,_T("UniqueInstanceName"));
if(GetLastError() == ERROR_ALREADY_EXISTS)
       bFound = TRUE;
if(hMutexOneInstance) 
    ReleaseMutex(hMutexOneInstance);
if(bFound) return FALSE;

Getting the IP Address

Include the Winsock header file.

#include <WinSock.h>

Include the wsock32 library.

#pragma comment(lib,"wsock32.lib")
Collapse
BOOL CFun::GetIPAddress(CString &IPAddress)
{
  WORD wversion;
  WSADATA wsData;
  char name[255];
  PHOSTENT hostinfo;
  wversion=MAKEWORD(1,1);
  char *ip;
  if(WSAStartup(wversion,&wsData)==0)
  {
    if(gethostname(name,sizeof(name))==0)
    {
      if((hostinfo=gethostbyname(name))!=NULL)
      {
         int count=0;
         while(hostinfo->h_addr_list[count])
         {
           ip=inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[count]);
           CString st;st.Format("%s",ip);
           IPAddress+=st;
           ++count;
         }
       }
       else return FALSE;
     }
     else return FALSE;
   }
   else return FALSE;
   return TRUE;
 }

Moving a Captionless dialog

void CYourDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
    CDialog::OnLButtonDown(nFlags,point);
    PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
}

Creating a Hollow brush

CBrush *br=CBrush::FromHandle((HBRUSH)GetStockObject(HOLLOW_BRUSH));

Reading Bitmap from a file

HBITMAP hbitmap=(HBITMAP) ::LoadImage( AfxGetApp()->m_hInstance, 
                                       ”c://test.bmp”, 
                                       IMAGE_BITMAP, 
                                       LR_DEFAULTSIZE, 
                                       LR_DEFAULTSIZE, 
                                       LR_LOADFROMFILE);

Painting background with an image

BOOL CYourDialog::OnEraseBkgnd(CDC* pDC)
{
    CBitmap mybitmap;
    mybitmap.LoadBitmap(IDB_BITMAP1);
    CDC mymemdc; 
    mymemdc.CreateCompatibleDC(pDC);
    mymemdc.SelectObject(&mybitmap);
    PDC->BitBlt(0,0,100,100,&mymemdc,0,0,SRCCOPY);
    return 1;
}

Sending message to all windows

::SendMessage(HWND_BROADCAST,WM_CLOSE,0,0);

Registering an extension for your application

The following function can be used with any type of VC applications (Dialog based, SDI, MDI....). The core is, the following code will register its application directly to the Registry. Doc/View Architecture has its own way to register its document's extension (CWinApp::RegisterShellFileTypes(BOOL bCompat=FALSE)).

Collapse
BOOL RegisterExtension(CString sExt, CString sDescription)
{
    // sDescription is the description key for you application
    sDescription.Replace(" ","");            
    sExt.Replace(" ","");                // sExt – extension to register
    if(sExt.IsEmpty()) return FALSE;
    if(sDescription.IsEmpty()) return FALSE;

    sExt.Replace(".","");
    sExt="."+sExt;

    CString str=GetCommandLine(); // Getting the application name
    CString app;
    int index=str.Find(":",0);
    index=str.Find(":",index+1);
    if(index==-1) app=str;
    else app=str.Mid(0,index-1);

    app.Replace("/"",""); 
    app+=" /"%1/"";
    HKEY hkey;
    RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_QUERY_VALUE,&hkey);
    DWORD dw;
    RegCreateKeyEx(hkey, sExt.operator LPCTSTR() , 0L, NULL,
            REG_OPTION_NON_VOLATILE, 
            KEY_ALL_ACCESS, 
            NULL,&hkey, &dw );
    CString key=sDescription;
    RegSetValueEx(hkey,"",0,REG_SZ,(BYTE *)key.operator LPCTSTR(),
               key.GetLength());
    RegCloseKey(hkey);

    RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_QUERY_VALUE,&hkey);
    RegCreateKeyEx (hkey, key, 0L, NULL,
            REG_OPTION_NON_VOLATILE, 
            KEY_ALL_ACCESS, 
            NULL, &hkey, &dw);
    RegCreateKeyEx (hkey, "shell",  0L, NULL,
            REG_OPTION_NON_VOLATILE,
            KEY_ALL_ACCESS, 
            NULL,&hkey,  &dw);
    RegCreateKeyEx (hkey, "open", 0L, NULL,
            REG_OPTION_NON_VOLATILE,
            KEY_ALL_ACCESS, 
            NULL,&hkey, &dw);
    RegCreateKeyEx (hkey, "command", 0L, NULL,
            REG_OPTION_NON_VOLATILE,
            KEY_ALL_ACCESS, 
            NULL,&hkey, &dw);
    RegSetValueEx(hkey,"",0,REG_SZ, (LPBYTE)(LPCTSTR)app,app.GetLength());
    RegCloseKey(hkey);

    RegOpenKeyEx(HKEY_CLASSES_ROOT,key,0,KEY_QUERY_VALUE,&hkey);
    RegCreateKeyEx (hkey, "DefaultIcon", 0L, NULL,
            REG_OPTION_NON_VOLATILE, 
            KEY_ALL_ACCESS,
            NULL,&hkey, &dw);
    app.Replace(" /"%1/"",",0");
    RegSetValueEx(hkey,"",0,REG_SZ, (LPBYTE)(LPCTSTR)app,app.GetLength());
    RegCloseKey(hkey);
    return TRUE;
}

Getting screen resolution

void GetScreenResolution(int &xValue, int &yValue)
{
    xValue = GetSystemMetrics ( SM_CXSCREEN ) ;
    yValue = GetSystemMetrics ( SM_CYSCREEN ) ;
}

Getting path of executable for a file

char buff [MAX_PATH] ;
FindExecutable ( "C://myhtml.htm", NULL, buff ) ;

Changing color of a progress bar control

CProgressCtrl::SendMessage ( PBM_SETBARCOLOR, 0, RGB ( 255, 0, 0 ) ) ;

Setting a system wide cursor

Define OEMRESOURCE in stdafx.h.

#define OEMRESOURCE
HCURSOR hcur1 = AfxGetApp()->LoadCursor(IDC_YOURCURSOR);
HCURSOR hcur2 = CopyCursor(hcur1);
SetSystemCursor(hcur2,OCR_NORMAL);

Getting directory list in your combobox

m_combo.Dir ( DDL_DIRECTORY, "C://Windows//*.*" )  ; 
m_combo.SetCurSel ( 1 ) ;

Parsing your commandline information

CCommandLineInfo cmdInfo;
AfxGetApp()->ParseCommandLine(cmdInfo);
MessageBox(cmdInfo.m_strFileName)    // Your command line input filename

to be continued...

Dear Friends

I didn't describe any of the codes above. I have not enough time to spend for that. Just I have presented the code.

None of the articles can satisfy one's expectations. But, each article should be a seed for your technical growth. Thus, I believe that this would be a seed. Thank you all.

深度学习tricks是指在深度学习模型训练过程中使用的一些技巧和策略,旨在提高模型的性能和训练效果。以下是一些常用的深度学习tricks: 1. 数据增强(Data Augmentation):通过对原始数据进行随机变换和扩充,生成更多的训练样本,以增加模型的泛化能力。 2. 批归一化(Batch Normalization):在每个小批量数据上进行归一化操作,有助于加速模型的收敛速度,提高模型的稳定性和泛化能力。 3. 学习率调整(Learning Rate Schedule):根据训练的进程动态地调整学习率,例如使用学习率衰减或者学习率预热等策略,以提高模型的收敛性能。 4. 正则化(Regularization):通过添加正则化项,如L1正则化或L2正则化,限制模型的复杂度,防止过拟合。 5. 提前停止(Early Stopping):在训练过程中监控验证集上的性能指标,当性能不再提升时停止训练,以避免过拟合。 6. 参数初始化(Parameter Initialization):合适的参数初始化可以帮助模型更快地收敛和更好地泛化,常用的初始化方法包括Xavier初始化和He初始化等。 7. 梯度裁剪(Gradient Clipping):限制梯度的范围,防止梯度爆炸或梯度消失问题,提高模型的稳定性。 8. 集成学习(Ensemble Learning):通过结合多个模型的预测结果,可以提高模型的泛化能力和鲁棒性。 9. 迁移学习(Transfer Learning):利用已经训练好的模型在新任务上进行微调,可以加快模型的训练速度和提高模型的性能。 10. 深度网络结构设计:合理设计网络结构,包括层数、宽度、卷积核大小等,可以提高模型的表达能力和学习能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值