在WinMain中嵌Console視窗

2021-09-01 09:32:51 字數 4435 閱讀 8991

很多時候,除錯gui程式是很不方便的,通常的做法是使用messagebox,但是作為乙個模態視窗,它經常產生不必要的訊息,比如killfocus, setfocus或者paint,從而影響除錯的執行過程。當然,使用vc的偵錯程式也不錯,但是這樣也很容易造成視窗切換從而產生干擾訊息。

因此,如果能像在控制台程式裡那樣使用cin/cout物件或printf族函式,會使得除錯過程方便得多。而通常,windows是不會為gui程式產生單獨的命令列視窗的。所以我們是看不到使用標準輸入輸出流輸出的東西的。既然系統不提供,那就自己動手「造」出乙個來吧!

下面是乙個簡單的控制台視窗物件,它可以為你的程式建立乙個命令列視窗,並將stdout,stdin和stderr重定向到這個命令列視窗。在程式中建立乙個這樣的物件之後,就可以直接使用cin/cout/*printf來操縱這個新的命令列視窗了!

.h檔案

#ifndef _custom_console_

#define _custom_console_

#include

#include

#include

#include

class console

;#endif

.cpp檔案

#include "***.h"

bool console::i***istent = false;

console::console()

console::console(lpctstr lpsztitle, short consoleheight, short consolewidth)

void console::attach(short consoleheight, short consolewidth)

}可以在winmain裡建立這個物件,若在main裡建立這個物件,則同樣會出現乙個新的控制台視窗。

#ifdef _debug // 當然,在release版裡同樣可以使用

console notused;

#endif

// unincmd.cpp : 定義應用程式的入口點。

//#include "stdafx.h"

#include "unincmd.h"

#include

#include

#include

#include

using namespace std;

#define max_loadstring 100

// 全域性變數:

hinstance hinst;        // 當前例項

tchar  sztitle[max_loadstring];     // 標題欄文字

tchar  szwindowclass[max_loadstring];   // 主視窗類名

handle      hin  = null;

handle  hout = null;

handle      herr = null;

// 此**模組中包含的函式的前向宣告:

atom    myregisterclass(hinstance hinstance);

bool    initinstance(hinstance, int);

lresult callback wndproc(hwnd, uint, wparam, lparam);

lresult callback about(hwnd, uint, wparam, lparam);

void outputstring(lpctstr szstring);

int apientry _twinmain(hinstance hinstance,

hinstance hprevinstance,

lptstr    lpcmdline,

int       ncmdshow)

allocconsole();

dword dwbyteswritten;

char *msg1 = "第一條訊息\n";

hin = getstdhandle(std_input_handle);

hout = getstdhandle(std_output_handle);

herr = getstdhandle(std_error_handle);

setconsoletitle("內嵌cmd");

file *hf = null;

char buf[2];

int hcrt = 0;

//輸出

hcrt = _open_osfhandle((long)hout,_o_text);

hf = _fdopen(hcrt,"w");    //表示可寫

setvbuf(hf,buf,_ionbf,1);

*stdout = *hf;

//輸入

int hcirt = 0;

hcirt = _open_osfhandle((long)hin,_o_text);

file    *hfin = null;

hfin = _fdopen(hcirt,"r");

setvbuf(hfin,buf,_ionbf,1);

*stdin = *hfin;

//錯誤

int hert = 0;

hert = _open_osfhandle((long)herr,_o_text);

file   *hferr = null;

hferr = _fdopen(hert,"w");

setvbuf(hferr,buf,_ionbf,1);

*stderr = *hferr;

cout<<"microsoft windows media player"// 主訊息迴圈:

while (getmessage(&msg, null, 0, 0))

}freeconsole();

return (int) msg.wparam;}//

//  函式:myregisterclass()

////  目的:註冊視窗類。

////  注釋:

////    僅當希望在已新增到 windows 95 的

//    「registerclas***」函式之前此**與 win32 系統相容時,

//    才需要此函式及其用法。呼叫此函式

//    十分重要,這樣應用程式就可以獲得關聯的

//   「格式正確的」小圖示。

//atom myregisterclass(hinstance hinstance)

////   函式:initinstance(handle, int)

////   目的:儲存例項控制代碼並建立主視窗

////   注釋:

////        在此函式中,我們在全域性變數中儲存例項控制代碼並

//        建立和顯示主程式視窗。

//bool initinstance(hinstance hinstance, int ncmdshow)

showwindow(hwnd, ncmdshow);

updatewindow(hwnd);

return true;}//

//  函式:wndproc(hwnd, unsigned, word, long)

////  目的:處理主視窗的訊息。

////  wm_command - 處理應用程式選單

//  wm_paint - 繪製主視窗

//  wm_destroy - 傳送退出訊息並返回

////

lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam)

break;

case wm_paint:

hdc = beginpaint(hwnd, &ps);

// todo: 在此新增任意繪圖**...

endpaint(hwnd, &ps);

break;

case wm_destroy:

postquitmessage(0);

break;

default:

return defwindowproc(hwnd, message, wparam, lparam);

}return 0;

}// 「關於」框的訊息處理程式。

lresult callback about(hwnd hdlg, uint message, wparam wparam, lparam lparam)

break;

}return false;

}void outputstring(lpctstr szstring)

// 新增控制台視窗控制器的寫法

bool   ctrlhandler(dword   fdwctrltype)  

}  

void   main(void)  

MFC中的WinMain函式

winmain是windows程式的入口點函式,與dos程式的入口點函式main的作用相同,當winmain函式結束或返回時,windows應用程式結束。winmain函式的原型如下 int winapi winmain hinstance hinstance,hinstance hprevinst...

嵌在web app中的Quartz突然啟動不了了

可是最近發現job沒有執行,而且啟動日誌中也沒有上述資訊了 tomcat會經常被我重啟 更奇怪的是,本地的開發環境中job可以正常執行,log中也有記錄.和配置我都比較過,完全一致.我試著在web.xml中把quartzinitializer的啟動延遲 code quartzinitializer ...

MFC框架中消失的WinMain

似乎微軟就是不想讓人清楚 明白的了解mfc框架將winmain 封裝的過程。微軟在mfc中是這樣隱藏呼叫winmain 的 1.在tchar.h檔案中定義了乙個巨集 define twinmain winmain 這樣mfc源 在編譯的時候,twinmain就會被替換為winmain 這樣作業系統就...