|
Is there any way of doing it? I am trying to write some simple class for showing modeless dialog box and just cannot understand what is wrong with it. While it is pretty easy with DialogBoxParam - there seems to be no way with CreateDialog . My goal is to pass somehow a pointer to a class to call my dlgProc. The way i am doing it with modal dlgbox:
INT_PTR DoModal(UINT nResourceId)
{
::InitCommonControls();
return ::DialogBoxParam(::GetModuleHandle(NULL),
MAKEINTRESOURCE(nResourceId),
NULL, DlgProcModal, reinterpret_cast<LPARAM>(this));
}
static INT_PTR __stdcall DlgProcModal(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
....
if( uMsg == WM_INITDIALOG )
{
if( !pWnd )
{
pWnd = reinterpret_cast<CBaseDlgWindow *>(lParam);
::SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)pWnd);
pWnd->m_hWnd = hwndDlg;
}
}
}
The way i am trying to do it with modeless and it is just not working:
INT_PTR DoModeless(UINT nResourceId)
{
::InitCommonControls();
if( m_hWnd = ::CreateDialog(::GetModuleHandle(NULL), MAKEINTRESOURCE(nResourceId), NULL, DlgProcModeless) )
{
m_bModeless = true;
::SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);
MSG msg = { 0x00 };
while (::GetMessage(&msg, m_hWnd, 0, 0))
{
if (!::IsDialogMessage(m_hWnd, &msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
}
::DestroyWindow(m_hWnd);
return 0;
}
static INT_PTR __stdcall DlgProcModeless(
HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam
)
{
LONG_PTR lResult = ::GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
CBaseDlgWindow * pWnd = reinterpret_cast<CBaseDlgWindow *>(lResult);
...
}
What am i doing wrong?
Thanks in advance.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
First, you probably have a main message loop somewhere that dispatches messages to every window (and dialog), not only to a specified window:
std::vector<HWND> g_Dialogs;
MSG msg;
while (::GetMessage(&msg, NULL, 0, 0))
{
std::vector<HWND> dialogs(g_Dialogs);
bool was_dialog_message = false;
for (size_t i=0,e=dialogs.size(); i<e; ++i)
{
if (::IsWindow(dialogs[i]) && ::IsDialogMessage(dialogs[i], &msg))
{
was_dialog_message = true;
break;
}
}
if (!was_dialog_message)
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
When you want a modeless dialog, you just create it with CreateDialog() , put its handle to g_Dialogs and thats it. You don't have to run a local message loop like with modal messageboxes, especially not with a specified single window handle (m_hWnd). A modeless dialog works almost like a normal window that you created with CreateWindowEx() , you just have to handle it a bit specially with IsDialogMessage() . In a modeless multi-windowed gui program its always enough one message loop - the main message loop pumps the messages for all modeless windows.
EDIT: warning: while you are iterating over the g_Dialogs vector, the contents of the vector might change because you dispatch messages to dialogs that can respond to those messages by creating/deleting dialogs! To avoid bugs caused by this you either use a custom container that can be modified during iteration or solve it somehow else. One good solution can be copying the vector before iteration and iterating on the copied vector, still some HWNDs might become invalid during iteration so before IsDialogMessage() it might be wise to call IsWindow() on the handles.
modified 19-Aug-12 19:12pm.
|
|
|
|
|
Hey, thanks for your reply. My problem was that i couldn't find a way to put the whole thing into nice C++ class of my own. But, problem was solved by using CreateDialogParam instead. So i can pass a pointer to the dialog class to a main message loop so i can call my own virtual methods inside children classes.
Cheers.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I though that your problem is the messagebox because your loop was inside the DoModeless() method. Anyway, your previous hooking is also fine, you just miss a few messages that come before the return of your CreateDialog() call and you have to handle uninitialized GWLP_USERDATA cases as well. Even if you initialize userdata from WM_INITDIALOG it might be better to handle uninitialized cases, who knows what the future brings?
|
|
|
|
|
I'm a Chinese , want to find a friend who use C/C++
,and i want to comunicate with him ,anything
my email and MSN is mqy_bs@Hotmail.com,E-mail me if you want
|
|
|
|
|
You should really read this set of guidelines[^] for using these forums. If you have trouble with English then the General Chinese Forum[^] has members who can help you.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
The following is a fragment of code:
sockaddr_in from,a;
WSADATA wsdata;
BOOL optval = TRUE;
u_short uPort = m_nPort;
if(WSAStartup(0x0202,&wsdata) != 0)
{
CString strErr;
strErr.Format(_T("WSAStartup Init failed! Error code:%d"),WSAGetLastError());
AddNote(strErr);
_endthreadex(0);
return FALSE;
}
a.sin_family = AF_INET;
a.sin_addr.s_addr = 0;
a.sin_port = htons(uPort);
from.sin_family = AF_INET;
from.sin_addr.s_addr = INADDR_BROADCAST;
from.sin_port = htons(uPort);
int fromlength=sizeof(SOCKADDR);
pThread->m_socket = socket(AF_INET,SOCK_DGRAM,0);
setsockopt(pThread->m_socket,SOL_SOCKET,SO_BROADCAST,(char FAR *)&optval,sizeof(optval));
bind(pThread->m_socket,(sockaddr *)&a,sizeof(sockaddr_in));
char buf[BUF_MAX_LEN] = {0x00};
int nLen = 0;
char *pszIPClient = NULL;
u_short uPortClient = 0;
THREADBOARD *pThreadBoard = NULL;
CString strInit = _T("");
strInit.Format(_T("Port: %d Now waiting for clients..."),uPort);
AddNote(strInit);
while(true)
{
nLen = recvfrom(pThread->m_socket,buf,BUF_MAX_LEN,0,(struct sockaddr FAR *)&from,(int FAR *)&fromlength);
if(nLen > 0)
{
}
}
Thanks for your reply !
|
|
|
|
|
You can do this by using raw sockets but I was raw-socketing only on linux, dont know how it works on windows (if it works at all...). I searched for an example and found this discussion: http://bytes.com/topic/c/answers/900770-how-determine-origin-incoming-message-uincast-multicast-broadcast[^]
One of the answers contains source code, read along that thread. Note: I havent tested their code.
EDIT: The trick is simple: the sendto address is in the header of the datagram you received, but your recvfrom returns just the payload from that and the address of the sender. You need a method to reach other fields of the received UDP packet header, one way is using raw sockets.
modified 18-Aug-12 11:22am.
|
|
|
|
|
First [this].
Then, Packet Filtering , found [here].
Also [this].
|
|
|
|
|
|
Programs under windows need administrator privilege must get user's permission when they start. And they can also run with administrator privilege by right click the EXE and select "Run as Administrator". But, before the EXE start, a message box will show up. It's really disgusting.
Now I want my application to run as the Administrator, and I don't want the popup message box when user click the EXE file.
Now I wonder whether AdjustTokenPrivileges can help me achieve this.
Any one can help me?
modified 18-Aug-12 10:13am.
|
|
|
|
|
This has nothing to do with C++/MFC, please choose a forum more appropriate to the subject. You may find that the most appropriate forum is Google itself.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
So you want your program to run as an admin without notifying the user, or getting the admin's permission?
Sounds like malware to me.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
1. This program displays a running counter starting from 0 till infinity. The counter starts/resets by pressing enter and stops by pressing some other key. Use as many define directives and macros to achieve this task. The counter when played should be like a real counter (no separate line should be used for the
next counter increment). Hint: use clrscr() command inside the for loop.
|
|
|
|
|
Please try and do your own homework. If you get stuck on a particular part of the code then post a question here, but do not expect others to provide an entire solution without any effort on your part.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
It was not homework.I saw that question in book and Dont know how to solve it.So i thought to ask The Experts to help me.
|
|
|
|
|
Experts will help you, but you are expected to make some effort for yourself first.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Try not to re-post the question. Have patience to get answers.
|
|
|
|
|
Why is there a "1." preceding the text? Were you going to ask a second question? What's with the "Hint:" at the end?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
roza123 wrote: Use as many define directives and macros to achieve this task.
Whoever wrote that exercise should be fired.
Asking for bad coding practice at that level is just asking for troubles.
Watched code never compiles.
|
|
|
|
|
It's from a book according to OP; goodness knows who wrote it.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
Challenge accepted.
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27
#define KB_F8 66
#define KB_ENTER 13
int KB_code=0;
int ch=0;
int ON_OFF_SWITCH=-1;
unsigned long int counter=0;
void gotoxy(int x, int y);
void clrscr();
void do_keyboard_processing_stuff(void);
int main(void)
{
while(KB_code != KB_ESCAPE )
{
if (ON_OFF_SWITCH==1) counter++;
gotoxy(22,10);
cout<<"Infinity Counter = "<<counter<<" ";
do_keyboard_processing_stuff();
}
cout<<"\n\n";
return 0;
}
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
return;
}
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
void do_keyboard_processing_stuff(void)
{
if (kbhit())
{
KB_code = getch();
if (KB_code!=KB_ENTER) ON_OFF_SWITCH=-ON_OFF_SWITCH;
switch (KB_code)
{
case KB_ESCAPE:
break;
case KB_LEFT:
break;
case KB_RIGHT:
break;
case KB_UP:
break;
case KB_DOWN:
break;
case KB_ENTER:
clrscr();
counter=0;
ON_OFF_SWITCH=-1;
break;
}
}
}
|
|
|
|
|
You're missing a few macros in there!!!
Watched code never compiles.
|
|
|
|
|
You're right. I totally missed the macros part.
|
|
|
|
|